set基本概念
简介:
- 所有元素都会在插入时自动被排序
本质:
- set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器中有重复的元素
set构造和赋值
功能描述:创建set容器以及赋值
构造:
set<T> st;
//默认构造函数:set(const set &st);
//拷贝构造函数
赋值:
set& operator=(const set &st);
//重载等号操作符
set大小和交换
功能描述:
- 统计set容器大小以及交换set容器
函数原型:
size();
//返回容器中元素的数目empty();
//判断容器是否为空swap(st);
//交换两个集合容器
set插入和删除
功能描述:
- set容器进行插入数据和删除数据
函数原型:
insert(elem);
//在容器中插入元素。clear();
//清除所有元素erase(pos);
//删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end);
//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(elem);
//删除容器中值为elem的元素。
set查找和统计
功能描述:
- 对set容器进行查找数据以及统计数据
函数原型:
find(key);
//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();count(key);
//统计key的元素个数
set和multiset区别
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
pair对组创建
功能描述:
- 成对出现的数据,利用对组可以返回两个数据
两种创建方式:
pair<type, type> p ( value1, value2 );
pair<type, type> p = make_pair( value1, value2 );
#include <iostream>
#include <set>
#include <string>
using namespace std;
void printset(set<int>& s)
{
for(set<int>::iterator it = s.begin(); it != s.end(); ++it){
cout << *it << " ";
}
cout << endl;
}
// test func of size
void test1()
{
set<int> s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
printset(s1);
if(s1.empty())
{
cout << "s1 is empty" << endl;
} else {
cout << "s1 is not empty" << endl;
cout << "size of s1 is :" << s1.size() << endl;
}
}
// test func of swap
void test2()
{
set<int> s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
set<int> s2;
s2.insert(5);
s2.insert(6);
s2.insert(7);
s2.insert(8);
cout << "before exchange: " << endl;
printset(s1);
printset(s2);
cout << endl;
s1.swap(s2);
cout << "after exchange: " << endl;
printset(s1);
printset(s2);
}
// test func of find and count
void test3()
{
set<int> s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
set<int>::iterator pos = s1.find(6);
if(pos != s1.end())
cout << "find it! pos: " << *pos << endl;
else
cout << "cannot find this element!" << endl;
int num = s1.count(1);
cout << "num = " << num << endl;
}
//test func of pair
void test4()
{
pair<string, int> p(string("tom"), 20);
cout << "name: " << p.first << " " << " age: " << p.second << endl;
pair<string, int> p2 = make_pair("jerry", 10);
cout << "name: " << p2.first << " " << " age: " << p2.second << endl;
}
// func of difference between set and multiset
void test5()
{
set<int> s;
pair<set<int>::iterator, bool>
ret = s.insert(10);
if(ret.second){
cout << "first successful insertion!" << endl;
} else {
cout << "first unsuccessful insertion!" << endl;
}
ret = s.insert(10);
if(ret.second){
cout << "second successful insertion!" << endl;
} else {
cout << "second unsuccessful insertion!" << endl;
}
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for(multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main()
{
// test1();
// test2();
// test3();
// test4();
test5();
return 0;
}
set和multiset区别
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,表示插入是否成功
- multiset不会检测数据,因此可以插入重复数据
set容器排序
- set容器默认排序规则为从小到大,如何改变排序规则
示例一 set存放内置数据类型
#include <iostream>
#include <set>
using namespace std;
class compare
{
public:
bool operator()(int v1, int v2){
return v1 > v2;
}
};
void test()
{
set<int> s1;
s1.insert(1);
s1.insert(2);
s1.insert(3);
s1.insert(4);
s1.insert(5);
for(set<int>::iterator i = s1.begin(); i != s1.end(); ++i)
{
cout << *i << " ";
}
cout << endl;
set<int, compare> s2;
s2.insert(1);
s2.insert(2);
s2.insert(3);
s2.insert(4);
s2.insert(5);
for(set<int>::iterator i = s2.begin(); i != s2.end(); ++i) {
cout << *i << " ";
}
cout << endl;
}
int main()
{
test();
return 0;
}
示例二 set存放自定义数据类型
#include <iostream>
#include <set>
#include <string>
using namespace std;
class Persons
{
public:
Persons(string name, int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class compare
{
public:
bool operator()(const Persons & p1, const Persons & p2)
{
return p1.m_age > p2.m_age;
}
};
void test()
{
set<Persons, compare> s;
Persons p1("jimmy", 1);
Persons p2("wang", 2);
Persons p3("liu", 3);
Persons p4("tao", 4);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for(set<Persons, compare>::iterator it = s.begin(); it != s.end(); it++){
cout << "name: " << it->m_name << " age: " << it->m_age << endl;
}
}
int main()
{
test();
return 0;
}