list <string> algorithms

Discussion in 'C++' started by s, Dec 17, 2003.

  1. s

    s Guest

    I'm getting compile errors on the following code:

    <code>
    #include <iostream>
    #include <fstream>
    #include <list>
    #include <string>

    using namespace std;

    int main(void)
    {
    ifstream infile("expertonasia.txt", ios::in);
    list <string> word_list;
    list <string>::iterator word_list_it;
    string word;
    int word_count = 0;
    bool found_match;

    while(infile)
    {
    infile>>word;
    word_list_it = word_list.find(word_list.begin(), word_list.end(),
    word);
    if( word_list_it != word_list.end() ) //new word
    {
    word_list.push_back(word);
    }
    }
    infile.close();

    //Any sorting?
    word_list.sort(word_list.begin(), word_list.end());

    }

    </code


    Produces the following compile errors:
    <compile>
    post.c++: In function `int main()':
    post.c++:20: no matching function for call to `std::list<std::string,
    std::allocator<std::string> >::find(std::_List_iterator<std::string,
    std::string&, std::string*>, std::_List_iterator<std::string,
    std::string&,
    std::string*>, std::string&)'
    post.c++:29: no matching function for call to `std::list<std::string,
    std::allocator<std::string> >::sort(std::_List_iterator<std::string,
    std::string&, std::string*>, std::_List_iterator<std::string,
    std::string&,
    std::string*>)'
    /usr/include/c++/3.2/bits/stl_list.h:879: candidates are: void
    std::list<_Tp,
    _Alloc>::sort() [with _Tp = std::string, _Alloc =
    std::allocator<std::string>]
    </compile>



    Can I use the find and sort algorithms on a list of string's???

    Thanks,
    Stephen
    s, Dec 17, 2003
    #1
    1. Advertising

  2. s wrote in news:3FE07FA6.2020009@home:

    > I'm getting compile errors on the following code:
    >
    > <code>
    > #include <iostream>
    > #include <fstream>
    > #include <list>
    > #include <string>


    #include <algorithm>

    >
    > using namespace std;
    >
    > int main(void)
    > {
    > ifstream infile("expertonasia.txt", ios::in);
    > list <string> word_list;
    > list <string>::iterator word_list_it;
    > string word;
    > int word_count = 0;
    > bool found_match;
    >
    > while(infile)
    > {
    > infile>>word;


    replace:

    > word_list_it = word_list.find(word_list.begin(),
    > word_list.end(), word);


    with:

    word_list_it = find( word_list.begin(), word_list.end(), word);

    > if( word_list_it != word_list.end() ) //new word
    > {
    > word_list.push_back(word);
    > }
    > }
    > infile.close();
    >
    > //Any sorting?


    replace:

    > word_list.sort(word_list.begin(), word_list.end());


    with:

    word_list.sort();
    >
    > }
    >



    >
    > Can I use the find and sort algorithms on a list of string's???
    >


    You appear to be confusing external algorithms (that is template
    function's from <algorithm> ) and member function's.

    Note that you can't use std::sort( begin, end ) an a list as it's
    iterator's don't meet std::sort's requirments, hence the member
    function sort() in std::list<>.


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Dec 17, 2003
    #2
    1. Advertising

  3. s

    red floyd Guest

    s wrote:

    Rob took care of your syntax errors, so I'll fix one of your semantic errors:
    > I'm getting compile errors on the following code:
    >
    > <code>
    > #include <iostream>
    > #include <fstream>
    > #include <list>
    > #include <string>
    >
    > using namespace std;
    >
    > int main(void)
    > {
    > ifstream infile("expertonasia.txt", ios::in);
    > list <string> word_list;
    > list <string>::iterator word_list_it;
    > string word;
    > int word_count = 0;
    > bool found_match;
    >
    > while(infile)
    > {
    > infile>>word;
    > word_list_it = word_list.find(word_list.begin(), word_list.end(),
    > word);
    > if( word_list_it != word_list.end() ) //new word

    if ( word_list_it == word_list.end() ) // new word
    > {
    > word_list.push_back(word);
    > }
    > }
    > infile.close();
    >
    > //Any sorting?
    > word_list.sort(word_list.begin(), word_list.end());
    >
    > }


    find() returns container.end() when not found... you had the sense of the comparison backwards.
    red floyd, Dec 17, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas
    Replies:
    0
    Views:
    498
    Andreas
    Dec 2, 2003
  2. abhinav

    encryption algorithms

    abhinav, Dec 26, 2004, in forum: VHDL
    Replies:
    2
    Views:
    630
  3. Brian
    Replies:
    6
    Views:
    238
    Clever Monkey
    Apr 29, 2008
  4. Chris M. Thomasson

    automated test for sub-string search algorithms...

    Chris M. Thomasson, Feb 20, 2010, in forum: C Programming
    Replies:
    3
    Views:
    516
    Chris M. Thomasson
    Feb 23, 2010
  5. Phrogz

    String Hashing Algorithms

    Phrogz, May 10, 2005, in forum: Ruby
    Replies:
    15
    Views:
    319
    Clifford Heath
    May 12, 2005
Loading...

Share This Page