STL的基本使用之关联容器:set和multiSet的基本使用

STL的基本使用之关联容器:set和multiSet的基本使用

  1. 简介

    • set 和 multiSet 内部都是使用红黑树来实现,会自动将元素进行排序。两者不同在于set 不允许重复,而multiSet 允许重复
  2. 头文件 #include < set >

  3. 构造函数及析构函数

  4. 非变动性操作函数

    • 运算符重载
    • 查找操作函数
    • 赋值操作
    • 迭代器操作
  5. 插入删除操作

  6. 范例如下

     	#include <iostream>
     	#include <set>
     	using namespace std;
     	int main ()
     	{
     	  set<int> c;
     	  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<int>(cout," "))
     	}
    
  7. 运行截图

posted @ 2016-04-09 16:31  一夜星辰  阅读(376)  评论(0编辑  收藏  举报