473,583 Members | 3,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list <string> algorithms

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("experto nasia.txt", ios::in);
list <string> word_list;
list <string>::itera tor 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::_L ist_iterator<st d::string,
std::string&, std::string*>, std::_List_iter ator<std::strin g,
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::_L ist_iterator<st d::string,
std::string&, std::string*>, std::_List_iter ator<std::strin g,
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

Jul 22 '05 #1
2 5060
s wrote in news:3FE07FA6.2 020009@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("experto nasia.txt", ios::in);
list <string> word_list;
list <string>::itera tor 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/
Jul 22 '05 #2
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("experto nasia.txt", ios::in);
list <string> word_list;
list <string>::itera tor 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.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2514
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
3
8485
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!
3
15160
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 generic list...is not clear to me. Any suggestions? System.Collections.Generic.List<string> list = new System.Collections.Generic.List<string>();...
6
46168
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: <BEGIN CODE> List<stringkeyNameList = new List<string>(); foreach (string keyName in this.myDictionary.Keys)
4
23939
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:
4
2931
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: returning address of local variable or temporary". In good old 'C', that would indicate that a 'static' was missing from the declaration of the...
2
5870
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 dictionary essentially creates the following array: Key Value
4
3944
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; }
3
3431
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 is one in which each phrase is indexed by every word in the phrase. */
0
7895
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8182
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8327
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8193
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6579
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.