| 1 |
#include <iostream> |
| 2 |
#include <string> |
| 3 |
#include <list> |
| 4 |
using namespace std; |
| 5 |
// see the merge() example |
| 6 |
void showlist(list<string> &target) |
| 7 |
{ |
| 8 |
for |
| 9 |
( |
| 10 |
list<string>::iterator from = target.begin(); |
| 11 |
from != target.end(); |
| 12 |
++from |
| 13 |
) |
| 14 |
cout << *from << " "; |
| 15 |
|
| 16 |
cout << '\n'; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
int main() |
| 21 |
{ |
| 22 |
string |
| 23 |
array[] = |
| 24 |
{ |
| 25 |
"charlie", |
| 26 |
"alpha", |
| 27 |
"bravo", |
| 28 |
"alpha" |
| 29 |
}; |
| 30 |
|
| 31 |
list<string> |
| 32 |
target |
| 33 |
( |
| 34 |
array, array + sizeof(array) |
| 35 |
/ sizeof(string) |
| 36 |
); |
| 37 |
|
| 38 |
cout << "Initially we have:\n"; |
| 39 |
showlist(target); |
| 40 |
|
| 41 |
target.sort(); |
| 42 |
cout << "After sort() we have:\n"; |
| 43 |
showlist(target); |
| 44 |
|
| 45 |
target.unique(); |
| 46 |
cout << "After unique() we have:\n"; |
| 47 |
showlist(target); |
| 48 |
} |
| 49 |
/* |
| 50 |
Generated output: |
| 51 |
|
| 52 |
Initially we have: |
| 53 |
charlie alpha bravo alpha |
| 54 |
After sort() we have: |
| 55 |
alpha alpha bravo charlie |
| 56 |
After unique() we have: |
| 57 |
alpha bravo charlie |
| 58 |
*/ |