zoukankan      html  css  js  c++  java
  • 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. 运行截图

  • 相关阅读:
    The Chinese Postman Problem HIT
    Chinese Postman Problem Aizu
    矩阵游戏 HYSBZ
    最大获利 HYSBZ
    asp.net+MVC--1
    -----IT男生涯————初始篇
    Permutation
    RMQ with Shifts
    Fast Matrix Operations
    "Ray, Pass me the dishes!"
  • 原文地址:https://www.cnblogs.com/peaceWang/p/5371949.html
Copyright © 2011-2022 走看看