sort list<string> stringList??

A

aquanutz

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!
 
P

Peter_Julian

| 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::eek:stream_iterator<std::string>( std::cout,
"\n") );
std::cout << std::endl;

return 0;
}

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

Neil Cerutti

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.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top