1. set特性
set 容器内的元素会被自动排序。
set 中不能有两个元素有相同的key值。
不能通过 set 的迭代器去修改 set 元素,原因是修改元素会破坏 set 组织。
当对容器中的元素进行插入或者删除时,操作之前的所有迭代器在操作之后依然有效。
2. 定义和初始化
首先加入头文件
#include <set>
定义和初始化
set<int> a; //定义一个int类型的集合a
set<int> b(a); //定义集合b,用集合a初始化集合b
set<int> b(a.begin(), a.end()); //定义集合b,将集合a中的所有元素作为集合b的初始值
int n[] = { 1, 2, 3, 4, 5 };
set<int> a(n, n + 5); // 定义集合a,用数组n的前5个元素作为集合a的初值
3. set操作
size相关操作
容器大小:st.size()
容器最大容量:st.max_size()
容器判空:st.empty()
查找key的元素个数:st.count(key)
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
set<int> st;
for (int i = 0; i<6; i++)
{
st.insert(i);
}
cout << st.size() << endl; // 输出:6
cout << st.max_size() << endl; // 输出:214748364
cout << st.count(2) << endl; // 输出:1
if (st.empty())
cout << "元素为空" << endl; // 未执行
return 0;
}
添加元素
在容器中插入一个元素:st.insert(x)
在某个位置插入一个元素:st.insert(iterator it, x)
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
set<int> st;
// 在容器中插入元素
st.insert(4);
// 任意位置插入一个元素
set<int>::iterator it = st.begin();
st.insert(it, 2);
// 遍历显示
for (it = st.begin(); it != st.end(); it++)
cout << *it << " "; // 输出:2 4
cout << endl;
return 0;
}
删除元素
删除容器中x值的元素:st.erase(x)
删除容器中迭代器所指位置的元素:st.erase(iterator it)
删除某个位置范围之间(first,last)的元素:st.erase(iterator first, iterator last)
清空所有元素:st.clear()
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
set<int> st;
for (int i = 0; i < 8; i++)
st.insert(i);
// 删除容器中值为elem的元素
st.erase(4);
// 删除最开始位置的一个元素
set<int>::iterator it = st.begin();
st.erase(it);
// 删除[first,last]之间的元素
st.erase(st.begin(), ++st.begin());
// 遍历显示
for (it = st.begin(); it != st.end(); it++)
cout << *it << " "; // 输出:2 3 5 6 7
cout << endl;
// 清空所有元素
st.clear();
// 判断set是否为空
if (st.empty())
cout << "集合为空" << endl; // 输出:集合为空
return 0;
}
查找元素
查找值为x的元素:st.find(x)
若存在,则返回元素的迭代器,不存在返回st.end()
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
set<int> st;
for (int i = 0; i < 6; i++)
st.insert(i);
// 通过find(key)查找键值
set<int>::iterator it;
it = st.find(2);
cout << *it << endl; // 输出:2
return 0;
}
交换容器
交换两个类型相同的容器:st1.swap(st2)
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
set<int> st1;
st1.insert(1);
st1.insert(2);
st1.insert(3);
set<int> st2;
st2.insert(4);
st2.insert(5);
st2.insert(6);
// 交换两个容器的元素
st1.swap(st2);
// 遍历显示
cout << "交换后的st1: ";
set<int>::iterator it;
for (it = st1.begin(); it != st1.end(); it++)
cout << *it << " "; // 输出:4 5 6
cout << endl;
// 遍历显示
cout << "交换后的st2: ";
for (it = st2.begin(); it != st2.end(); it++)
cout << *it << " "; // 输出:1 2 3
cout << endl;
return 0;
}
迭代器
- 开始迭代器指针:
st.begin();
- 末尾迭代器指针:
st.end();
// 指向最后一个元素的下一个位置 - 指向常量的开始迭代器指针:
st.cbegin();
// 意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。 - 指向常量的末尾迭代器指针:
st.cend();
- 反向迭代器指针,指向最后一个元素:
st.rbegin();
- 反向迭代器指针,指向第一个元素的前一个元素:
st.rend();
- 返回最后一个 key<=keyElem 元素的迭代器:
st.lower_bound(keyElem);
- 返回第一个 key>keyElem 元素的迭代器:
st.upper_bound(keyElem);
- 返回容器中 key 与 keyElem 相等的上下限的两个迭代器,这两个迭代器被放在对组(pair)中:
st.equal_range(keyElem);
#include <iostream>
#include <set>
using namespace std;
int main(int argc, char* argv[])
{
set<int> st;
st.insert(1);
st.insert(2);
st.insert(3);
cout << *(st.begin()) << endl; // 输出:1
cout << *(--st.end()) << endl; // 输出:3
cout << *(st.cbegin()) << endl; // 输出:1
cout << *(--st.cend()) << endl; // 输出:3
cout << *(st.rbegin()) << endl; // 输出:3
cout << *(--st.rend()) << endl; // 输出:1
cout << *(st.lower_bound(2)) << endl; // 输出:2
cout << *(st.upper_bound(2)) << endl; // 输出:3
pair<set<int>::iterator, set<int>::iterator> t_pair = st.equal_range(2);
cout << *(t_pair.first) << endl; // 输出:2
cout << *(t_pair.second) << endl; // 输出:3
cout << endl;
return 0;
}