C++-SET使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++-SET使用

chaiyinlei   2020-03-09 我要评论

 HDU2000《set测试使用案例》

#include<iostream>
#include<set>
#include<string.h>
using namespace std;
int main(){
char s[3];
while(~scanf("%s",s)){
set<char>se;
for(int i=0;i<strlen(s);i++){
se.insert(s[i]);
}
int flag=0;
set<char>::iterator it;
for(it =se.begin();it!=se.end();it++){
if(!flag) cout<<*it,flag=1;
else cout<<" "<<*it;
}
cout<<endl;
}
return 0;
}

 

 

 

   使用set前,需要在程序的头文件中包含声明#include <set>;它包含了set和multiset两种容器额定义;

<1>创建set对象;

创建set对象时,需要指定元素的类型,这一点与其他容器一样,下面的程序详细说明了如何创建一个集合对象:

#include <iostream>

#include <set>

using namespace std;

int main(int argc, char *argv[])

{

    //定义元素类型为int的集合对象s,当前没有任何元素;

    //元素的排列采用默认的比较规则,当然,可以自定义比较规则函数;

    set<int>s;

    return 0;

}

<2>.set元素的插入;

采用insert()方法把元素插入到集合中去,插入的具体规则在默认的比较规则下,是按元素值从小到大插入,如果自己指定了比较规则函数,则按自定义比较规则函数插入。

使用前向迭代器对集合中序遍历,其结果正好是元素排序的结果;

下例说明了insert()方法的使用方法:

运行结果为:

1 6 8 12

#include <iostream>

#include <set>

using namespace std;

int main(int argc, char *argv[])

{

    //定义元素类型为int的集合对象a, 当前没有任何元素;

    set <int> s;

    //插入了5个元素但由于8有重复,所以第二次插入的8并没有执行;

    s.insert(8); //第一次插入8,可以执行;

    s.insert(1);

    s.insert(12);

    s.insert(6);

    s.insert(8); //第二次插入8,重复元素,不会插入;

    //中序遍历集合中的元素

    set <int> :: iterator it;   //定义迭代器;

    for(it=s.begin(); it != s.end(); it++) {

        cout << *it << " ";

    }

    cout << endl;

    return 0;

}

<3>.set元素的反向遍历;    

使用反向迭代器reverse iterator可以反向遍历集合,输出的结果正好是集合元素的反向排序的结果,他需要用到rbegin()和rend()两个方法,他们分别给出了反向遍历的开始位置和借宿位置。

下例详细的说明了如何对集合进行反向遍历:

运行结果为:

12 8 6 1

The Code Follows:

#include <iostream>

#include <set>

using namespace std;

int main(int argc, char *argv[])

{

    //定义元素类型为int的集合对象a, 当前没有任何元素;

    set <int> s;

    //插入了5个元素但由于8有重复,所以第二次插入的8并没有执行;

    s.insert(8); //第一次插入8,可以执行;

    s.insert(1);

    s.insert(12);

    s.insert(6);

    s.insert(8); //第二次插入8,重复元素,不会插入;

    //反向遍历集合中的元素

    set <int> :: reverse_iterator rit;    //定义反向迭代器;

    for(rit=s.rbegin(); rit != s.rend(); rit++) {

        cout << *rit << " ";

    }

    cout << endl;

    return 0;

}

<4>.set元素的删除与容器清空;

与插入元素的处理一样,集合就高效的删除处理功能,并自动重新调整内部的红黑树的平衡;

删除的对象可以是某个迭代器位置上的元素,等于某键值的元素,一个区间上的元素和清空集合;

运行结果为:

12 8 1

0

The Code Follows:

#include <iostream>

#include <set>

using namespace std;

int main(int argc, char *argv[])

{

    //定义元素类型为int的集合对象a, 当前没有任何元素;

    set <int> s;

    //插入了5个元素但由于8有重复,所以第二次插入的8并没有执行;

    s.insert(8); //第一次插入8,可以执行;

    s.insert(1);

    s.insert(12);

    s.insert(6);

    s.insert(8); //第二次插入8,重复元素,不会插入;

    //删除键值为6的元素;

    s.erase(6);

    //反向遍历集合洲哥的元素;

    set <int> :: reverse_iterator rit;    //定义反向迭代器;

    for(rit=s.rbegin(); rit != s.rend(); rit++) {

        cout << *rit << " ";

    }

    cout << endl;

    // 清空集合 ;

    s.clear();

    cout << s.size() << endl;

    return 0;

}

<5>. Set 元素的检索;

使用find()方法对集合进行搜索,如果找到查找的键值,则返回该键值的迭代器位置,否则,返回集合最后一个元素后面的一个位置,即end();

下例程序详细讲述了如何使用find()方法对集合进行检索:

运行结果为:

6

Not find it!

#include <iostream>

#include <set>

using namespace std;

int main(int argc, char *argv[])

{

    //定义元素类型为int的集合对象a, 当前没有任何元素;

    set <int> s;

    //插入了5个元素但由于8有重复,所以第二次插入的8并没有执行;

    s.insert(8); //第一次插入8,可以执行;

    s.insert(1);

    s.insert(12);

    s.insert(6);

    s.insert(8); //第二次插入8,重复元素,不会插入;

    //查找键值为6的元素;

    set <int> ::iterator it;    //定义前向迭代器;

    it = s.find(6);

    if(it != s.end()) cout << *it << endl;

    //查找键值为20的元素;

    it = s.find(20);

    if(it != s.end()) {  // 找到

        cout << *it << endl;

    }else {   //没找到;

        cout << "Not find it!" << endl;

    }

    return 0;

}

<6>.set自定义比较函数;    

使用insert()方法将元素插入到集合中去的时候,集合会根据设定的比较函数将该元素放到盖房的节点上去,在定义集合的时候,如果没有指定比较函数,那么采用默认的比较函数,即按键值由大到小的顺序插入元素,在很多情况下,需要自己编写比较函数;

编写比较函数有两种方法:

1>.如果元素不是结构体,那么,可以编写比较函数。下例程序编写的比较规则是要求按键值由大到小的顺序将元素插入到集合中;

运行结果:

12 8 6 1

The Code Follows:

#include <iostream>

#include <set>

using namespace std;

struct mycmp   //自定义比较函数mycmp, 重载()运算符;

{

    bool operator() (const int &a, const int &b)

    {

        return a > b;

    }

};

int main(int argc, char *argv[])

{

    //定义元素类型为int的集合对象a, 当前没有任何元素;

    //采用的比较函数是mycmp;

    set <int, mycmp> s;

    //插入了5个元素但由于8有重复,所以第二次插入的8并没有执行;

    s.insert(8); //第一次插入8,可以执行;

    s.insert(1);

    s.insert(12);

    s.insert(6);

    s.insert(8); //第二次插入8,重复元素,不会插入;

    set <int> ::iterator it;    //定义前向迭代器;

    for(it=s.begin(); it != s.end(); it++) {

        cout << *it << " ";

    }

    cout << endl;

    return 0;

}

2>. 如果元素是结构体,那么,可以直接把比较函数写在结构体内,下例程序详细说明了具体操作:

运行结果为:

Jack 80.5

Nacy 60.5

Tomi 20.5

#include <iostream>

#include <set>

#include <string>

using namespace std;

struct Info

{

    string name;

    float score;

    //重载 “ < ” 操作符,自定义排序准则;

    bool operator < (const Info &a) const

    {

         // 按 score 由大到小排列。如果要由小到大排列 , 使用 “ > ” 号即可 ;

        return a.score < score;

    }

};

int main()

{

    // 定义元素类型为 Info 结构体的集合对象 s, 当前没有任何元素 ;

    set <Info> s;

    // 定义 Info 结构体的对象 ;

    Info info;

    // 插入三个元素 ;

    info.name = "Jack";

    info.score = 80.5;

    s.insert(info);

    info.name = "Tomi";

    info.score = 20.5;

    s.insert(info);

    info.name = "Nacy";

    info.score = 60.5;

    s.insert(info);

    set <Info> :: iterator it;  //定义前向迭代器;

    for(it=s.begin(); it != s.end(); it++) {

        cout << (*it).name << " " << (*it).score << endl;

    }

    return 0;

}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们