set构造函数
set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数:
set(const set &st);//拷贝构造函数
set赋值操作
set& operator=(const set &st);//重载等号操作符
swap(st);//交换两个集合容器
set大小操作
size();//返回容器中元素的数目
empty();//判断容器是否为空
set插入和删除操作
insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的元素。
set查找操作
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
示例
#include<iostream>
#include<set>
#include<string>
using namespace std;
void printSet(set<int> s) {
for (set<int>::iterator i = s.begin(); i != s.end(); ++i) {
cout << (*i) << endl;
}
}
void test01() {
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(50);
s1.insert(40);
printSet(s1);
if (s1.empty()) {
cout << "set容器为空" << endl;
}
else {
cout << "set容器不为空 大小为: " << s1.size() << endl;
}
s1.erase(40);
printSet(s1);
}
void test02() {
set<int> s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(50);
s1.insert(40);
set<int>::iterator pos = s1.find(30);
if (pos != s1.end()) {
cout << (*pos) << endl;
}
cout << s1.count(40) << endl;
//返回大于等于30的值
set<int>::iterator lb = s1.lower_bound(30);
if (lb != s1.end()) {
cout << (*lb) << endl;
}
//返回大于30的值
set<int>::iterator ub = s1.upper_bound(30);
if (lb != s1.end()) {
cout << (*ub) << endl;
}
//返回30,40
pair<set<int>::iterator, set<int>::iterator> ret = s1.equal_range(30);
if (ret.second != s1.end() && ret.second != s1.end())
{
cout << (*ret.first) << endl;
cout << (*ret.second) << endl;
}
}
void test03() {
pair<string, int> p1("Tom",21);
cout <<"name:" << p1.first << " age:" << p1.second << endl;
pair<string, int> p2 = make_pair("Jerry", 18);
cout << "name:" << p2.first << " age:" << p2.second << endl;
}
void test04() {
set<int>s;
pair<set<int>::iterator, bool>ret = s.insert(10);
if (ret.second) {
cout << "添加成功" << endl;
}
else {
cout << "添加失败" << endl;
}
ret = s.insert(10);
if (ret.second) {
cout << "添加成功" << endl;
}
else {
cout << "添加失败" << endl;
}
multiset<int> ms;
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator i = ms.begin();i!=ms.end();++i) {
cout << (*i) << endl;
}
}
class compareInt
{
public:
//利用仿函数,重载()
bool operator()(int a, int b)const
{
//降序
return a > b;
}
};
void p1(const set<int, compareInt>& s)
{
for (set<int, compareInt>::const_iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test05()
{
set<int, compareInt> s1;
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
p1(s1);
}
int main() {
test01();
cout << "---------" << endl;
test02();
cout << "---------" << endl;
test03();
cout << "---------" << endl;
test04();
cout << "---------" << endl;
test05();
system("pause");
return EXIT_SUCCESS;
}
执行结果
10
20
30
40
50
set容器不为空 大小为: 5
10
20
30
50
---------
30
1
30
40
30
40
---------
name:Tom age:21
name:Jerry age:18
---------
添加成功
添加加失败
10
10
---------
40 30 20 10
请按任意键继续. . .