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

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

  1. 简介
    • set 和 multiSet 内部都是使用红黑树来实现,会自动将元素进行排序。两者不同在于set 不允许重复,而multiSet 允许重复
  2. 头文件 #include < set >
  3. 构造函数及析构函数 construction.png
  4. 非变动性操作函数
    • 运算符重载noModifyOperation.png
    • 查找操作函数searchOperation.png
    • 赋值操作assignOperation.png
    • 迭代器操作iterator.png
  5. 插入删除操作InsertDelete.png
  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. 运行截图output1.png

转载于:https://www.cnblogs.com/peaceWang/p/5371949.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值