473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sort list<string> stringList??

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!

Oct 21 '05 #1
3 8464
Ok, had wrong code... needed:
stringList.sort();
instead. Thanks.

Oct 21 '05 #2

<aq******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| 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!
|

Both list and string are not defined in the standard. Try std::string
and std::list.

Why use a container like a std::list if a std::set does this for you
transparently using a predicate (default pred: std::less<>)?

#pragma warning(disable:4786)
#include <string>
#include <set>
#include <iterator>
#include <algorithm>
#include <ostream>
#include <iostream>

int main()
{
/* default:
std::set< std::string,
std::less<std::string>,
std::allocator<std::string> > stringset;
*/
std::set< std::string > stringset;
stringset.insert("string d");
stringset.insert("string a"); // sorted with std::less<std::string>
stringset.insert("string c"); // sorted ...
stringset.insert("string b"); // sorted ...

std::copy( stringset.begin(),
stringset.end(),
std::ostream_iterator<std::string>( std::cout,
"\n") );
std::cout << std::endl;

return 0;
}

/*
string a
string b
string c
string d
*/

Oct 22 '05 #3
On 2005-10-22, Peter_Julian <pj@antispam.codigo.ca> wrote:
Why use a container like a std::list if a std::set does this
for you transparently using a predicate (default pred:
std::less<>)?


It depends on his requirements:

stringset.insert("string d");
stringset.insert("string a"); // sorted with std::less<std::string>
stringset.insert("string a"); // doesn't get inserted at all.

Moreover, he may not need or want it to be sorted all the time.

--
Neil Cerutti
Oct 24 '05 #4

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

Similar topics

3
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...
6
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: ...
4
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
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:...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
12
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses...
2
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...
4
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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 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.