Syntax:
void unique();
void unique( BinPred pr );
The function unique() removes all duplicate elements from the list. Equality is tested using the == operator, unless pr is specified as a replacement. The ordering of the elements in a list should not change after a call to unique().
unique() runs in linear time.
E.g: JOJ 2274
Write a program that reads a single line of input, not greater than 10000 characters long, consisting of a number of lower case words separated by spaces, the number of letters of a word no more than 100, and print the words, one per line, in alphabetical order. Print each word only once regardless of how many times it appears in the input.
Sample Input
how green is my
valley
Sample Output
green
how
is
my
valley
My Solution:
#include <iostream>
#include <string>
#include <algorithm>
#include <list>
using namespace std;
void Print(string& s)
{
cout<<s<<endl;
}
int main(int argc, char** argv)
{
list<string> slist;
string s;
while(cin>>s)slist.push_back(s);
slist.sort();
slist.unique();//The order of sort() and unique() can NOT be changed!
for_each(slist.begin(),slist.end(),Print);
return 0;
}
Comments