STL之multiset简介
2019独角兽企业重金招聘Python工程师标准>>>
1、简介
multiset跟set具有相同功能,但允许重复的元素。multiset容器的内部结构通常由平衡二叉树(balanced binary tree)来实现。当元素放入容器中时,会按照一定的排序法则自动排序,默认是按照less<>排序规则来排序。这种自动排序的特性加速了元 素查找的过程,但是也带来了一个问题:不可以直接修改set或multiset容器中的元素值,因为这样做就可能违反了元素自动排序的规则。如果你希望修 改一个元素的值,必须先删除原有的元素,再插入新的元素。
2、成员函数
2.1 构造函数
multiset( );
explicit multiset (
const Compare& _Comp
);
multiset (
const Compare& _Comp,
const Allocator& _Al
);
set(
const multiset<Key, Compare, Allocator> & _Right
);
template<class InputIterator>
multiset (
InputIterator _First,
InputIterator _Last
);
template<class InputIterator>
multiset (
InputIterator _First,
InputIterator _Last,
const Compare& _Comp
);
template<class InputIterator>
multiset (
InputIterator _First,
InputIterator _Last,
const Compare& _Comp,
const Allocator& _Al
);
参数:
_Al
The storage allocator class to be used for this set object, which defaults to Allocator.
_Comp
The comparison function of type constTraits used to order the elements in the set, which defaults to Compare.
_Right
The set of which the constructed set is to be a copy.
_First
The position of the first element in the range of elements to be copied.
_Last
The position of the first element beyond the range of elements to be copied.
2.2 迭代函数
const_iterator begin( ) const;
iterator begin( );
功能:
返回指向multiset头部的迭代器
const_iterator end( ) const;
iterator end( );
功能:
返回指向multiset尾部的迭代器
const_reverse_iterator rbegin( ) const;
reverse_iterator rbegin( );
功能:
返回一个指向multiset尾部的逆向迭代器
const_reverse_iterator rend( ) const;
reverse_iterator rend( );
功能:
返回一个指向multiset首尾部的逆向迭代器
2.3.2 擦除数据
iterator erase(
iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);
参数:
_Where
Position of the element to be removed from the set.
_First
Position of the first element removed from the set.
_Last
Position just beyond the last element removed from the set.
_Key
The key of the elements to be removed from the set.
2.3.3 交换数据
void swap(
set<Key, Traits, Allocator>& _Right
);
参数:
_Right
The argument set providing the elements to be swapped with the target set.
2.3.4 清空数据
void clear( );
2.3 操作函数
const_iterator lower_bound(
const Key& _Key
) const;
iterator lower_bound(
const Key& _Key
);
功能:
返回容器中第一个值大于或等于_Key的元素的iterator位置。
参数:
const Key& _Key, The argument key to be compared with the sort key of an element from the multiset being searched.
const_iterator upper_bound(
const Key& _Key
) const;
iterator upper_bound(
const Key& _Key
);
功能:
返回容器中第一个值大于_Key的元素的iterator位置。
参数:
const Key& _Key, The argument key to be compared with the sort key of an element from the multiset being searched.
pair <const_iterator, const_iterator>
equal_range (
const Key& _Key
) const;
pair <iterator, iterator>
equal_range (
const Key& _Key
);
功能:
返回容器中值等于val的所有元素的范围[beg, end)组成的pair<beg, end> 。即A pair of iterators such that the first is the lower_bound of the key and the second is the upper_bound of the key.
参数:
_Key
The argument key to be compared with the sort key of an element from the multiset being searched.
3、代码范例
#include <iostream>
#include <set>
using namespace std;
void PRINT_ELEMENTS(multiset<int> col1, const char* cstr)
{
multiset<int>::const_iterator pos;
cout << cstr << endl;
for (pos = col1.begin(); pos != col1.end(); ++pos)
{
cout << *pos << " ";
}
}
int main()
{
multiset<int> col1;
col1.insert(2);
col1.insert(5);
col1.insert(4);
col1.insert(6);
col1.insert(1);
col1.insert(5);
PRINT_ELEMENTS(col1, "col1: ");
cout << endl;
multiset<int>::const_iterator pos;
pair<multiset<int>::iterator, multiset<int>::iterator> range;
cout << "lower_bound(3): " << *col1.lower_bound(3) << endl;
cout << "upper_bound(3): " << *col1.upper_bound(3) << endl;
range = col1.equal_range(3);
cout << "equal_range(3): " << *range.first << " " << *range.second << endl;
cout << "elements with value(3): ";
for (pos = range.first; pos != range.second; ++pos)
{
cout << *pos << " ";
}
cout << endl;
cout << endl;
cout << "lower_bound(5): " << *col1.lower_bound(5) << endl;
cout << "upper_bound(5): " << *col1.upper_bound(5) << endl;
range = col1.equal_range(5);
cout << "equal_range(5): " << *range.first << " " << *range.second << endl;
cout << "elements with value(5): ";
for (pos = range.first; pos != range.second; ++pos)
{
cout << *pos << " ";
}
cout << endl;
}
输出:
col1:
1 2 4 5 5 6
lower_bound(3): 4
upper_bound(3): 4
equal_range(3): 4 4
elements with value(3):
lower_bound(5): 5
upper_bound(5): 6
equal_range(5): 5 6
elements with value(5): 5 5
转载于:https://my.oschina.net/u/174422/blog/80616
STL之multiset简介相关推荐
- STL set multiset
STL容器大的方向分为两类,序列式容器和关联式容器. 这两者通过数据在容器内的排列来区分.关联容器和顺序容器的根本不同在于:关联容器中的元素是按关键字来保存和访问的,而顺序容器中的元素则是按它们在容器 ...
- 5.1 c++ STL 容器适配器简介
1. 适配器简介 在详解什么是容器适配器之前,初学者首先要理解适配器的含义. 其实,容器适配器中的"适配器",和生活中常见的电源适配器中"适配器"的含义非常接近 ...
- STL标准容器类简介
from: http://www.cppblog.com/lmlf001/archive/2006/04/20/5967.aspx 标准容器类 说明 顺序性容器 vector 从后面快速的插入与删除, ...
- STL之multiset中equal_range,multimap中的equal_range,bitset容器,string字符串操作,lambda表达式
1multiset中用equal_range来遍历所有的元素 #include <set> #include <iostream> using namespace std ...
- stl的multiset和set和priority_queue区别
优先级队列只允许按照排序顺序访问一个元素 - 即,可以获得最高优先级的项目,而当删除该项目时,可以获得下一个最高优先级,依此类推. 优先级队列还允许重复元素,因此它更像是一个multiset而不是se ...
- stl之multiset容器的应用
与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,只是multiset容器允许将重复的元素健值插入,而set容器则不允许. set容器所使用的C++标准头文件set,其实也是 ...
- STL学习_(一)STL简介-STL六大组件简介
一.什么是STL 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多在计算机科学领域里常用的基本数据结构和基本算法.为广大C++ ...
- STL LIST使用简介
STL中list的使用: STL中的list就是一双向链表,可高效地进行插入删除元素.现总结一下它的操作. 文中所用到两个list对象c1,c2分别有元素c1(10,20,30) c2(40,50, ...
- STL 容器简介:C++ 容器:顺序性容器、关联式容器和容器适配器
STL标准容器类简介 标准容器类 说明 顺序性容器 vector 从后面快速的插入与删除,直接访问任何元素 deque 从前面或后面快速的插入与删除,直接访问任何元素 list 双链表,从任何地方快速 ...
最新文章
- Nat. Commun. | msiPL:质谱数据分析的新工具
- 百度和360的关键词提交查询
- JAVA国际化输出日期格式
- php解压功能的函数
- [html] 你知道微信端的浏览器内核是什么吗?
- Can‘t find a suitable configuration file in this directory or any parent. 报错解决错误
- golang中,new和make的区别
- Mail.Ru Cup 2018 Round 1 virtual participate记
- hashmap删除指定key_Java集合之HashMap源码解析(JDK8)
- @程序员,盘一盘炼成高效能开发者的 14 个习惯!
- asp.net门诊收费管理系统
- Linux/Ubuntu 98版五笔安装
- 百度关键词排名查询源码_推荐4个Google关键词排名查询工具
- P5339 [TJOI2019]唱、跳、rap和篮球
- UOJ #449. 【集训队作业2018】喂鸽子
- asp毕业设计——基于asp+access的车辆调度管理系统设计与实现(毕业论文+程序源码)——车辆调度管理系统
- VS Code + Tex Live + SumatraPDF配置LaTeX反向搜索(持续更新)
- java猴子分桃_算法——猴子分桃
- 圆圈中间一个乘号:克罗内克积
- Thinkphp6调用企业微信官方php版本接口方法
热门文章
- 解决Linux下ArcGIS Server的Tomcat不稳定问题,nginx配置反向代理时的一个小问题
- python 基础 信息量很大很好,适合复习
- C#操作ini文件类
- mysql中怎样自动生成代码_MySql之自动生成CRUD代码
- android源码分析(一) - 语言切换机制
- 架构语言ArchiMate - ArchiMate提供的基本视角(Viewpoints)介绍一
- 微软已停止对Vista RTM(SP0)的服务支持
- MarkDown、Vim双剑合璧
- Eclipse·Maven·构建SpringMVC简单工程-3
- centos7安装kibana5.x