STL之set UVa10815

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <sstream>
 6 #include <vector>
 7 #include <set>
 8 
 9 using namespace std;
10 
11 set<string> arr;
12 
13 int main()
14 {
15     string str,re;
16     while(cin>>str)
17     {
18         int num=0;
19         int len=str.length();
20         for(int i=0;i<len;i++)
21         {
22             if(isalpha(str[i]))
23             {
24                 str[i]=tolower(str[i]);
25             }
26             else
27                 str[i]=' ';
28         }
29         stringstream ss(str);
30         while(ss>>re)
31             arr.insert(re);
32     }
33     for(set<string>::iterator it=arr.begin();it!=arr.end();it++)
34     {
35         cout<<*it<<endl;
36     }
37     return 0;
38 }
View Code

set

set中存储的是有从小到大顺序的,没有重复元素的集合

想要访问需要类似于

1 for(set<string>::iterator it=arr.begin();it!=arr.end();it++)
2     {
3         cout<<*it<<endl;
4     }
View Code

这样的指针

可用函数:

size();  //返回大小

find(n);  //找到该元素,并返回该元素的指针it,如果未找到则返回arr.end();

erase(it);  //删除it所指向的元素

it++

;  //只允许指针加加,不允许指针加n

it--;  //只允许指针减减,不允许指针减n

multiset

multiset与set不同在于multiset不除重,集合中可以存在相同元素

原文地址:https://www.cnblogs.com/wsruning/p/4699447.html