std::multimap<Key,T,Compare,Allocator>:: multimap
| (1) | ||
|
multimap
(
)
;
|
(C++11 前) | |
|
multimap
(
)
:
multimap
(
Compare
(
)
)
{
}
|
(C++11 起)
(C++26 起为 constexpr) |
|
|
explicit
multimap
(
const
Compare
&
comp,
const Allocator & alloc = Allocator ( ) ) ; |
(2) | (C++26 起为 constexpr) |
|
explicit
multimap
(
const
Allocator
&
alloc
)
;
|
(3) |
(C++11 起)
(C++26 起为 constexpr) |
|
template
<
class
InputIt
>
multimap
(
InputIt first, InputIt last,
|
(4) | (C++26 起为 constexpr) |
|
template
<
class
InputIt
>
multimap
(
InputIt first, InputIt last,
|
(5) |
(C++14 起)
(C++26 起为 constexpr) |
|
multimap
(
const
multimap
&
other
)
;
|
(6) | (C++26 起为 constexpr) |
|
multimap
(
const
multimap
&
other,
const
Allocator
&
alloc
)
;
|
(7) |
(C++11 起)
(C++26 起为 constexpr) |
|
multimap
(
multimap
&&
other
)
;
|
(8) |
(C++11 起)
(C++26 起为 constexpr) |
|
multimap
(
multimap
&&
other,
const
Allocator
&
alloc
)
;
|
(9) |
(C++11 起)
(C++26 起为 constexpr) |
|
multimap
(
std::
initializer_list
<
value_type
>
init,
const
Compare
&
comp
=
Compare
(
)
,
|
(10) |
(C++11 起)
(C++26 起为 constexpr) |
|
multimap
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(C++14 起)
(C++26 起为 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
multimap
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(C++23 起)
(C++26 起为 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
multimap
(
std::
from_range_t
, R
&&
rg,
|
(13) |
(C++23 起)
(C++26 起为 constexpr) |
从多种数据源构造新容器,并可选择使用用户提供的分配器 alloc 或比较函数对象 comp 。
[
first
,
last
)
的内容构造容器。
|
若未提供
alloc
,则通过调用
std::
allocator_traits
<
allocator_type
>
::
|
(C++11 起) |
|
在
类模板实参推导
期间,仅首个实参参与容器
|
(C++23 起) |
|
在
类模板实参推导
期间,仅第一个实参参与容器
|
(C++23 起) |
目录参数
复杂度
1-3)
常量。
4,5)
N·log(N)
其中
N
是
std::
distance
(
first, last
)
的通用情况,若
[
first
,
last
)
已按
value_comp
(
)
排序则为
N
的线性复杂度。
6,7)
与
other
的大小成线性关系。
8,9)
常数复杂度。若提供
alloc
且
alloc
!
=
other.
get_allocator
(
)
,则为线性复杂度。
10,11)
N·log(N)
其中
N
是
init.
size
(
)
,通常情况下的时间复杂度,若
init
已按
value_comp
(
)
排序则为
N
的线性复杂度。
异常
对
注释在容器移动构造(重载 (8,9))之后,原本指向 虽然直到 C++23 才正式要求,但某些实现已经在早期模式中将模板参数
示例
运行此代码
#include <iostream> #include <map> #include <utility> struct Point { double x, y; }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.x < rhs.x; // 注意:此处故意忽略 y 坐标 } }; template <typename Key, typename Value, typename Cmp> void println(auto rem, const std::multimap<Key, Value, Cmp>& map) { std::cout << rem << "{ "; for (auto n{map.size()}; const auto& p : map) std::cout << '[' << p.first << ":" << p.second << (--n ? "], " : "]"); std::cout << " }\n"; } int main() { std::multimap<int, int> m1 = { {1, 1}, {2, 2}, {3, 3}, {4, 4}, {4, 4}, {3, 3}, {2, 2}, {1, 1} }; println("m1 = ", m1); // 自定义比较器 std::multimap<Point, double, PointCmp> mag { {{5, 12}, 13}, {{3, 4}, 5}, {{8, 15}, 17}, {{3, -3}, -1} }; for (auto p : mag) std::cout << "点 (" << p.first.x << ", " << p.first.y << ") 的模长为 " " is " << p.second << '\n'; std::cout << "从范围构造:\n"; using PS = std::pair<int, std::string>; const auto rg = {PS{3, "Earth"}, {2, "Venus"}, {1, "Mercury"}, {3, "Moon"}}; #if __cpp_lib_containers_ranges std::multimap<int, std::string> m2(std::from_range, rg); // 重载 (12) #else std::multimap<int, std::string> m2(rg.begin(), rg.end()); // 回退到 (4) #endif println("m2 = ", m2); } 输出: m1 = { [1:1], [1:1], [2:2], [2:2], [3:3], [3:3], [4:4], [4:4] }
点 (3, 4) 的模长为 5
点 (3, -3) 的模长为 -1
点 (5, 12) 的模长为 13
点 (8, 15) 的模长为 17
从范围构造:
m2 = { [1:Mercury], [2:Venus], [3:Earth], [3:Moon] }
缺陷报告以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
另请参阅
| ||||||||||||||||||||||||||||||||||||||||||||||||||||