list <string> algorithms

S

s

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
 
R

Rob Williscroft

s wrote in
I'm getting compile errors on the following code:

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

#include said:
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.
 
R

red floyd

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top