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 2 5030
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/
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: gbgbgbgb |
last post by:
Hi,
I have a definition
bool operator<(string s_s, string s_t)
{
....
}
and a variable
list<string> concomp;
|
by: aquanutz |
last post by:
Ok, I have a list of strings (list<string> stringList) that I want to
sort alphabetcially, only "sort(stringList.begin(), stringList.end());
) does not work. Any insight would be helpful. Thanks!
|
by: Abhi |
last post by:
In the following hypothetical example I want to build a generic list
of unique string items.
How should I implement the pred function so that it returns true/false
if target string exists in the...
|
by: buzzweetman |
last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the
list of keys out of it as a List<string>, to pass to a another method
that expects a List<string>.
I often do the following:
...
|
by: Mark Rae |
last post by:
Hi,
Is it possible to create a case-insensitive List<stringcollection?
E.g.
List<stringMyList = new List<string>;
MyList.Add("MyString");
So that:
|
by: rsa_net_newbie |
last post by:
Hi there,
I have a Managed C++ object (in a DLL) which has a method that is
defined like ...
Generic::List<String^>^ buildList(String^ inParm)
Now, when I compile it, I get "warning C4172:...
|
by: Assimalyst |
last post by:
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the...
|
by: parez |
last post by:
Hi,
I am trying to serialize List<List<string>.
With the following code
public List<List<string>DataRows { get; set; }
|
by: banangroda |
last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I
can't figure out why. Here is the code:
/*
5-1. Design and implement a program to produce a permuted index. A
permuted
index...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |