set用法

/*
set的用法:单一元素,自动排序
*/ 

#include <iostream>
#include <set>
using namespace std;
set<int> setTest;

int main(){
    int a[]={8,3,2,5,1,3,8,9,17,3};
    //将数组a里面的元素存入集合 
    for(int i=0;i<10;i++)
    {
        setTest.insert(a[i]);//添加数据 
    }
    //遍历输出,使用迭代器iterator 
    //无序方式输入,输出时已经从小到大排好序了,并且重复元素没有输出 
    for(set<int>::iterator it=setTest.begin();it!=setTest.end();++it)
    {
        cout<<*it<<" ";
    }
    cout<<endl;
    return 0;
}
//输出:1 2 3 5 8 9 17
//结果说明:重复元素只存储一次,像3和8
//并且会默认从小到大排序

例子:

输入一个文本,找出所有不同的单词(连续的最序列),按字典序从小到大输出,单词不区分大小写。
样例输入:
Today was my friend’s birthday, and I was invited to her birthday party.
The party was so great and the theme was red color. I saw many pictures of Hello Kitty.
They were so lovely. We sang the song to her and played a lot of funny games.
At last, we sent our best wishes to her. We had a great time.
The sign read:”Disneyland Left.”
样例输出:(前五个)
a
and
at
best
birthday

/*
分析:
1、大写变小写,其它符号变空格
2、存入set 
*/
#include <iostream>
#include <set>
#include <string>
#include <sstream> 
using namespace std;
set<string> dict;//string集合 

int main(){
    //freopen("in.txt","r",stdin); 
    string s,buf;//s用于读入单词,buf做缓存,解决read:"Disneyland是一个单词的问题 
    while(cin>>s){
        for(int i=0;i<s.length();i++){
            if(isalpha(s[i])) //判断是否是字符 
                s[i]=tolower(s[i]);//是字符就变成小写 
            else //不是字符就变成空格 
                s[i]=' ';
        }
        stringstream ss(s);//字符串流,作用和inputstream很相似
        //引入stringstream是为了防止把read:"Disneyland 这样的形式看出一个单词 
        while(ss>>buf) dict.insert(buf);//插入到dict集合中 
    } 
    //迭代器遍历,注意迭代器是指针 
    for(set<string>::iterator it=dict.begin();it!=dict.end();it++)
    {
        cout<<*it<<endl;
    }

    return 0;
}

stringstream
stringstream 是字符串流,相当于一个大的缓冲区,很多类型的数据都可以存储,也可以从中读出,就像文件流一样。
stringstream ss(s); //定义了一个字符串流,并用一个字符串初始化
ss << i;// 将i读入到ss中去
ss >> i;// 将ss中的单词逐个赋值到i
cout << ss.str();// 以字符串的形式输出ss中的所有内容

iterator
iterator是迭代器,类似于指针。set<string>::iterator it;it 是一个set类型的迭代器。 用*it取值

set集合中元素已从小到大排好序。这里是按单词的首字母排序。

总结:

构造set集合主要目的是为了快速检索,不可直接去修改键值。

常用操作:
1.元素插入:insert()
2.中序遍历:类似vector遍历(用迭代器)
3.反向遍历:利用反向迭代器reverse_iterator。
例:
set< int> s;
……
set< int>::reverse_iterator rit;
for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。
set< int> s;
s.erase(2); //删除键值为2的元素
s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
set< int> s;
set< int>::iterator it;
it=s.find(5); //查找键值为5的元素
if(it!=s.end()) //找到
cout < < *it << endl;
else //未找到
cout<<”未找到”;
6.自定义比较函数
(1)元素不是结构体:
例:

    //自定义比较函数myComp,重载“()”操作符
    struct myComp
    {
        bool operator()(const your_type &a,const your_type &b)
         [
            return a.data-b.data>0;
        }
    }
    set< int,myComp>s;
    ......
    set< int,myComp>::iterator it;

(2)如果元素是结构体,可以直接将比较函数写在结构体内。
    例:

    struct Info
    {
        string name;
        float score;
        //重载“<”操作符,自定义排序规则
        bool operator < (const Info &a) const
        {
            //按score从大到小排列
            return a.score<score;
        }
    }
    set< Info> s;
    ......
    set< Info>::iterator it;

set的元素 会按照操作符<排序
对于字符串,会按照首字母排序

猜你喜欢

转载自blog.csdn.net/qq_40828914/article/details/80657356
今日推荐