SET
Features:
The elements are unique, orderly, and efficient in insertion.
Basic functions:
begin()-returns an iterator to the first element
clear()-clear all elements
count()-returns the number of elements of a certain value
empty()-If the collection is empty, return true
end()-returns an iterator to the last element
equal_range()-return two iterators of the upper and lower bounds equal to the given value in the set
erase()-delete elements in the set
find()-returns an iterator pointing to the found element
get_allocator()-returns the allocator of the collection
insert()-insert an element in the collection
lower_bound() – returns an iterator to the first element greater than (or equal to) a certain value
key_comp()-returns a function for comparing values between elements
max_size()-returns the maximum limit of elements that the collection can contain
rbegin()-returns a reverse iterator pointing to the last element in the collection
rend()-returns a reverse iterator pointing to the first element in the collection
size()-the number of elements in the collection
swap()-swap two collection variables
upper_bound()-returns an iterator of elements greater than a certain value
value_comp()-returns a function used to compare values between elements
Compare:
(1) If the element is a structure, you can directly write the comparison function in the structure.
(2) The element is not a structure, and the comparison function is customized.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<cctype>
#include<sstream>
using namespace std;
struct example1{
//结构体重载‘<’,使本身有序。
int x;
int y;
bool operator < (const example1 &a)const
{
return a.x<x;//从大到小排列
}
};
struct example2{
int x;
int y;
};
struct cmp//自定义比较函数myComp,重载“()”操作符
{
bool operator()(const example2 &a,const example2 &b)
{
return a.x-b.x>0;//从大到小排列
}
};
int main()
{
set<example1>set1;
example1 ex1[5]={
{
5,1},{
3,1},{
4,1},{
1,1},{
2,1}};
for(int i=0;i<5;i++)
set1.insert(ex1[i]);
set<example1>::iterator it;
cout<<"自排序结构体:"<<endl;
for(it=set1.begin();it!=set1.end();it++)
cout<<(*it).x<<","<<(*it).y<<" ";
cout<<endl;
set<example2,cmp>set2;
example2 ex2[5]={
{
5,1},{
6,1},{
4,1},{
1,1},{
2,1}};
for(int i=0;i<5;i++)
set2.insert(ex2[i]);
set<example2,cmp>::iterator it2;
cout<<"自定义比较结构体,重载():"<<endl;
for(it2=set2.begin();it2!=set2.end();it2++)
cout<<(*it2).x<<","<<(*it2).y<<" ";
cout<<endl;
return 0;
}
Output:
Example: Andy's First Dictionary UVA-10815
Description
Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is,well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case.Words with only one letter are also to be considered. Further more, your program must be case insensltive.For example, words like “Apple”, “apple” or “APPLE” must be considered the same.
Input
The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Inputis terminated by EOF.
The input file is no more than 5000 lines of text. The input line can contain up to 200 characters. EOF terminates the input.
Output
Your output should give a list of different words that appears in the input text, one in a line. Thewords should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.
Output Please give a list of different words that appear in the input text, one word in one line. These words should all be in lower case, in alphabetical order. The number of different words in the file does not exceed 5000.
Sample Input
Adventures in Disneyland
Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: “Disneyland Left.”
So they went home.
Sample Output
a
adventuresblondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when
AC code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<set>
#include<cctype>
#include<sstream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
string s,buf;
set<string>dict;
while(cin>>s)
{
for(int i=0;i<s.length();i++)
{
if(!isalpha(s[i])) s[i]=' ';//把非字母字符转化空格
else s[i]=tolower(s[i]);//其它字母转化为小写字母
}
stringstream ss(s);
//利用stringstream将带有空格的字符串的拆分成一个个字符,类似于java的split()
while(ss>>buf) dict.insert(buf);
}
set<string>::iterator it;
for(it=dict.begin();it!=dict.end();it++)
cout<<*it<<endl;
return 0;
}
Note: There are many useful functions in #include, similar to isalpha()//Is it a letter, tolower(),
toupper(), etc. For details, please refer to: cctype header file
followed by #include input and output stream: use it It realizes the removal of spaces, and it can also realize the conversion between various data types in C++. For details, please refer to: sstream header file