https://en.cppreference.com/w/cpp/container/multiset
- 头文件
<set>
- 声明
(1)
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class multiset;
(2) (since C++17)
namespace pmr {
template <class Key, class Compare = std::less<Key>>
using multiset = std::multiset<Key, Compare,
std::pmr::polymorphic_allocator<Key>>;
}
- 描述
std::multiset i是一个关联容器,它包含有一个有序的类型key的对象的集合。与std::set不同的是,该集合内的对象可以有多个相同key的对象。通过使用key和比较函数Compare完成排序。搜索、删除和插入操作是对数复杂度的。Sets通常以红-黑树的方式来实现。两个对象a和b,如果!comp(a, b) && !comp(b, a)成立,则认为它们是等价的。
等价的对象的排序是基于插入的顺序,并且不会发生改变。
std::multiset meets the requirements of Container, AllocatorAwareContainer, AssociativeContainer and ReversibleContainer.
- 成员类型
类型 | 定义 |
key_type | Key |
value_type | Key |
size_type | Unsigned integer type (usually std::size_t) |
difference_type | Signed integer type (usually std::ptrdiff_t) |
key_compare | Compare |
value_compare | Compare |
allocator_type | Allocator |
reference | Allocator::reference (until C++11) value_type& (since C++11) |
const_reference | Allocator::const_reference (until C++11) const value_type& (since C++11) |
pointer | Allocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11) |
const_pointer | Allocator::const_pointer (until C++11) std::allocator_traits<Allocator>::const_pointer (since C++11) |
iterator | LegacyBidirectionalIterator |
const_iterator | Constant LegacyBidirectionalIterator |
reverse_iterator | std::reverse_iterator<iterator> |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
node_type(since C++17) | a specialization of node handle representing a container node |
insert_return_type(since C++17) | type describing the result of inserting a node_type, a specialization of instantiated with template arguments iterator and node_type. |
- 迭代器
begin cbegin | (C++11) returns an iterator to the beginning (public member function) |
end cend | (C++11) returns an iterator to the end (public member function) |
rbegin crbegin | (C++11) returns a reverse iterator to the beginning (public member function) |
rend crend | (C++11) returns a reverse iterator to the end (public member function) |
- 指标
empty | checks whether the container is empty (public member function) |
size | returns the number of elements (public member function) |
max_size | returns the maximum possible number of elements (public member function) |
- 修改
clear | (until C++11)void clear(); (since C++11)void clear() noexcept; | clears the contents (public member function) |
insert | (1) std::pair<iterator,bool> insert( const value_type& value ); (2) (since C++11) std::pair<iterator,bool> insert( value_type&& value ); (3) (until C++11)iterator insert( iterator hint, const value_type& value ); (since C++11)iterator insert( const_iterator hint, const value_type& value ); (4) (since C++11) iterator insert( const_iterator hint, value_type&& value ); (5) template< class InputIt > void insert( InputIt first, InputIt last ); (6) (since C++11) void insert( std::initializer_list<value_type> ilist ); (7) (since C++17) insert_return_type insert(node_type&& nh); (8) (since C++17) iterator insert(const_iterator hint, node_type&& nh); | inserts elements or nodes (since C++17) (public member function) |
emplace | (since C++11)template< class... Args > std::pair<iterator,bool> emplace( Args&&... args ); | (C++11) constructs element in-place (public member function) |
emplace_hint | (since C++11)template <class... Args>iterator emplace_hint( const_iterator hint, Args&&... args ); | (C++11) constructs elements in-place using a hint (public member function) |
erase | (1) (until C++11)void erase( iterator pos ); (since C++11)iterator erase( const_iterator pos ); (since C++17)iterator erase( iterator pos ); (2) (until C++11)void erase( iterator first, iterator last ); (since C++11)iterator erase( const_iterator first, const_iterator last ); (3) size_type erase( const key_type& key ); | erases elements (public member function) |
swap | (until C++17)void swap( set& other ); (since C++17)void swap( set& other ) noexcept(/* see below */); | swaps the contents (public member function) |
extract | (1)(since C++17)node_type extract( const_iterator position ); (2)(since C++17)node_type extract( const key_type& x ); | (C++17) extracts nodes from the container (public member function) |
merge | (1)(since C++17)void merge(std::set<Key, C2, Allocator>& source); (2)(since C++17)template<class C2>void merge(std::set<Key, C2, Allocator>&& source); (3)(since C++17)template<class C2>void merge(std::multiset<Key, C2, Allocator>& source); (4)(since C++17)template<class C2>void merge(std::multiset<Key, C2, Allocator>&& source); | (C++17) splices nodes from another container (public member function) |
- 查找
count | (1)size_type count( const Key& key ) const; (2)(since C++14)template< class K > size_type count( const K& x ) const; | returns the number of elements matching specific key (public member function) |
find | (1)iterator find( const Key& key ); (2)const_iterator find( const Key& key ) const; (3)(since C++14)template< class K > iterator find( const K& x ); (4)(since C++14)template< class K > const_iterator find( const K& x ) const; | finds element with specific key (public member function) |
contains | (1)(since C++20)bool contains( const Key& key ) const; (2)(since C++20)template< class K > bool contains( const K& x ) const; | (C++20) checks if the container contains element with specific key (public member function) |
equal_range | (1)std::pair<iterator,iterator>equal_range( const Key& key ); (2)std::pair<const_iterator,const_iterator> equal_range( const Key& key ) const; (3)(since C++14)template< class K >std::pair<iterator,iterator> equal_range( const K& x ); (4)(since C++14)template< class K >std::pair<const_iterator,const_iterator> equal_range( const K& x ) const; | Returns a range containing all elements with the given key in the container. The range is defined by two iterators, one pointing to the first element that is not less than key and another pointing to the first element greater than key. Alternatively, the first iterator may be obtained with lower_bound(), and the second with upper_bound(). |
lower_bound | (1) iterator lower_bound( const Key& key ); const_iterator lower_bound( const Key& key ) const; (2) (since C++14)template< class K >iterator lower_bound(const K& x); (since C++14)template< class K >const_iterator lower_bound(const K& x) const; | returns an iterator to the first element not less than the given key (public member function) |
upper_bound | (1) iterator upper_bound( const Key& key ); const_iterator upper_bound( const Key& key ) const; (2) (since C++14)template< class K >iterator upper_bound( const K& x ); (since C++14)template< class K >const_iterator upper_bound( const K& x ) const; | returns an iterator to the first element greater than the given key (public member function) |
- 观测器
key_comp | key_compare key_comp() const; | returns the function that compares keys (public member function) |
value_comp | std::set::value_compare value_comp() const; | returns the function that compares keys in objects of type value_type (public member function) |
- 举例
#include <cassert>
#include <iostream>
#include <set>
// Example module 97 key compare function
struct ModCmp {
bool operator()(const int lhs, const int rhs) const
{
return (lhs % 97) > (rhs % 97);
}
};
int main()
{
std::multiset<int, ModCmp> cont;
for(int i = 0; i < 10; i++)
{
cont.insert(i);
}
std::cout << "\n********************0-10************************** \n";
for (int key : cont) {
std::cout << key << " \n";
}
for(int i = 5; i < 15; i++)
{
cont.insert(i);
}
std::cout << "\n*********************0-14************************ \n";
for (int key : cont) {
std::cout << key << " \n";
}
std::cout << "\n*******************compare************************** \n";
auto comp_func = cont.key_comp();
for (int key : cont) {
bool before = comp_func(key, 100);
bool after = comp_func(100, key);
if (!before && !after)
std::cout << key << " equivalent to key 100\n";
else if (before)
std::cout << key << " goes before key 100\n";
else if (after)
std::cout << key << " goes after key 100\n";
else
assert(0); // Cannot happen
}
}
输出:
./multi_set.out
********************0-10**************************
9
8
7
6
5
4
3
2
1
0
*********************0-14************************
14
13
12
11
10
9
9
8
8
7
7
6
6
5
5
4
3
2
1
0
*******************compare**************************
14 goes before key 100
13 goes before key 100
12 goes before key 100
11 goes before key 100
10 goes before key 100
9 goes before key 100
9 goes before key 100
8 goes before key 100
8 goes before key 100
7 goes before key 100
7 goes before key 100
6 goes before key 100
6 goes before key 100
5 goes before key 100
5 goes before key 100
4 goes before key 100
3 equivalent to key 100
2 goes after key 100
1 goes after key 100
0 goes after key 100