Sorting alphabetically the lines in a text file

Hello everybody!
Can anyone help me with this thing : I want to know how I can sort alphabetically the lines in a text file, by that I mean what method can I use, being a beginner
Thanks!
use the getline() command to get lines from a file.
Store the lines in an array.
Look up "Bubble sort" and perform that on the array.
I would learn about the STL (standard template library).
In the example below I am using a STL container (vector), STL algorithms (for_each and sort) and STL iterators with for_each and sort (v.begin() and v.end()). Also at the end of the for_each I am using a new feature of C++0x/C11 (lambdas) to print out the values.

1
2
3
4
5
6
7
8
9
10
11
12
vector<string> v;
v.push_back("zzz");
v.push_back("abc");
v.push_back("xyz");

cout << "Original order" << endl;
for_each( v.begin(), v.end(), [](const string& s) { cout << s << endl; } );

sort( v.begin(), v.end() );

cout << "Sorted order" << endl;
for_each( v.begin(), v.end(), [](const string& s) { cout << s << endl; } );



Original order
zzz
abc
xyz

Sorted order
abc
xyz
zzz

Topic archived. No new replies allowed.