STL的基本使用之关联容器:set和multiSet的基本使用
STL的基本使用之关联容器:set和multiSet的基本使用
-
简介
- set 和 multiSet 内部都是使用红黑树来实现,会自动将元素进行排序。两者不同在于set 不允许重复,而multiSet 允许重复
-
头文件 #include < set >
-
构造函数及析构函数
![]()
-
非变动性操作函数
- 运算符重载
![]()
- 查找操作函数
![]()
- 赋值操作
![]()
- 迭代器操作
![]()
- 运算符重载
-
插入删除操作
![]()
-
范例如下
include
include
using namespace std;
int main ()
{
setc;
c.insert(1); c.insert(2); c.insert(4); c.insert(5); c.insert(6);
cout<<"count:"<<c.size()<<endl;
cout<<"find(4):"<<*c.find(4)<<endl;
cout << "lower_bound(3): " << *c.lower_bound(3) << endl;
cout << "upper_bound(3): " << *c.upper_bound(3) << endl;
cout << "equal_range(3): "
<< *c.equal_range(3).first << " "
<< *c.equal_range(3).second << endl;cout << endl;
cout << "lower_bound(5): " << *c.lower_bound(5) << endl;
cout << "upper_bound(5): " << *c.upper_bound(5) << endl;
cout << "equal_range(5): "
<< *c.equal_range(5).first << " "
<< *c.equal_range(5).second << endl;c.insert(c.lower_bound(3), 3);
c.erase(5);
copy(c.begin(), c.end(), ostream_iterator(cout," "))
} -
运行截图
![]()








浙公网安备 33010602011771号