std::
map::map
公众成员函数 <map>
C++98; |
empty (1) |
explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); |
|
range (2) |
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); |
|
copy (3) |
map (const map& x); |
C++11; |
empty (1) |
explicit map (const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc); |
|
range (2) |
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type()); |
|
copy (3) |
map (const map& x);
map (const map& x, const allocator_type& alloc); |
|
move (4) |
map (map&& x);
map (map&& x, const allocator_type& alloc); |
|
initializer list (5) |
map (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type()); |
C++14: |
empty (1) |
map();
explicit map (const key_compare& comp,
const allocator_type& alloc = allocator_type());
explicit map (const allocator_type& alloc); |
|
range (2) |
template <class InputIterator>
map (InputIterator first, InputIterator last,
const key_compare& comp = key_compare(),
const allocator_type& = allocator_type());
template <class InputIterator>
map (InputIterator first, InputIterator last,
const allocator_type& = allocator_type()); |
|
copy (3) |
map (const map& x);
map (const map& x, const allocator_type& alloc); |
|
move (4) |
map (map&& x);
map (map&& x, const allocator_type& alloc); |
|
initializer list (5) |
map (initializer_list<value_type> il,
const key_compare& comp = key_compare(),
const allocator_type& alloc = allocator_type());
map (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type()); |
构建映射
构造一个映射容器对象,根据使用的构造函数版本初始化它的内容:
C++98:
-
(1)空容器构造函数(默认构造函数)
-
构造一个空容器,没有元素。
-
(2)范围构造函数
-
构造一个容器,其中包含与范围[first,last)相同数量的元素,
每个元素都是由该范围内的相应元素放置构造的。
-
(3)拷贝构造函数
-
构造一个容器,其中包含x中每个元素的副本。
容器保留一个alloc和comp的内部副本,它们用于在容器的整个生命周期内分配存储空间和对元素进行排序。
复制构造函数(3)创建一个容器,该容器保存并使用x的分配器和比较对象的副本。
元素的存储是使用这个内部分配器分配的。
C++11:
-
(1)空容器构造函数(默认构造函数)
-
构造一个空容器,没有元素。
-
(2)范围构造函数
-
构造一个容器,其中包含与范围[first,last)相同数量的元素,
每个元素都是由该范围内的相应元素放置构造的。
-
(3)复制构造函数(和使用分配器复制)
-
构造一个容器,其中包含x中每个元素的副本。
-
(4)移动构造函数(和使用分配器移动)
-
构造一个获取x元素的容器。
如果指定了alloc并且与x的分配器不同,则移动元素。
否则,不构造元素(所有权直接转移)。
X处于未指定但有效的状态。
-
(5)初始化列表构造函数
-
构造一个容器,其中包含il中每个元素的副本。
容器保存了一个alloc的内部副本,用于分配和释放元素的存储空间,
以及构造和销毁它们(由它的allocator_traits指定)。如果没有alloc参数传递给构造函数,
则使用默认构造的分配器,以下情况除外:
- 复制构造函数(3,第一个签名)创建一个容器,
保存并使用,它保留并使用通过在x的分配器上调用相应的selected_on_container_copy_construction trait返回的分配器的副本。
- 移动构造函数(4,第一个签名)获得x的分配器。
容器还保留了一个comp(或x的比较对象)的内部副本,它用于建立容器中元素的顺序并检查等效元素。
通过使用相应的参数调用allocator_traits::construct函数来复制、移动或构造所有元素。
根据比较对象对元素进行排序。如果传递给构造函数的等效元素多于一个,则只保留第一个元素。
☲ 参数
-
comp
-
一个二进制谓词,它接受集合中包含的两个相同类型的值,
如果第一个参数以其定义的严格弱排序在第二个参数之前,
则返回true,否则返回false。
这应该是一个函数指针或函数对象。
成员类型key_compare是容器使用的内部比较对象类型,
在map中定义为它的第三个模板形参(Compare)的别名。
如果key_compare使用默认的less(没有状态),则此参数不相关。
-
alloc
-
分配器对象。
容器保存并使用这个分配器的内部副本.
成员类型allocator_type是容器使用的内部分配器类型,
在map中定义为它的第四个模板参数(Alloc)的别名。
如果allocator_type是默认分配器(没有状态)的实例化,则忽略考虑.
-
first, last
-
输入迭代器初始和最终位置的范围。
使用的范围是[first,last),它包括first和last之间的所有元素,
包括first所指向的元素,但不包括last所指向的元素。
函数模板实参InputIterator必须是一个输入迭代器类型,该类型指向可以构造value_type对象的类型的元素。
-
x
-
另一个相同类型的映射对象(具有相同的类模板参数Key,T,Compare和Alloc),其内容要么被复制要么被获取
-
il
-
一个initializer_list对象。
这些对象是由初始化列表声明符自动构造的。
成员类型value_type是容器中元素的类型,在map中定义为<const key_type, mapped_type>的别名(参见map类型)。
☣ 示例
// constructing maps
#include <iostream>
#include <map>
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs;}
};
int main ()
{
std::map<int> first; // empty map of ints
int myints[]= {10,20,30,40,50};
std::map<int> second (myints,myints+5); // range
std::map<int> third (second); // a copy of second
std::map<int> fourth (second.begin(), second.end()); // iterator ctor.
std::map<int,classcomp> fifth; // class as Compare
bool(*fn_pt)(int,int) = fncomp;
std::map<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
return 0;
} |
该代码不产生任何输出,但演示了构造映射容器的一些方法。
✥ 复杂度
对于空构造函数(1)和move构造函数(4)(除非alloc不同于x的分配器)是常量。
对于所有其他情况,如果元素已经按照相同的标准排序,则迭代器之间的距离为线性(复制结构)。
对于未排序的序列,在该距离内线性化(N*logN)(排序,复制构造)。
☣ 迭代器的有效性
move构造函数(4),如果元素被移动,将使所有与x相关的迭代器、指针和引用失效。
⇄ 数据竞争
访问所有复制的元素。移动构造函数(4)修改x。
☂ 异常安全性
强保证:在抛出异常的情况下没有影响。
如果元素结构的适当参数不支持allocator_traits::construct,
或者[first,last)指定的范围无效,则会导致未定义行为。
🍄 另请参阅
map::operator= |
内容赋值 (公众成员函数) |
map::clear |
清除内容 (公众成员函数) |
map::insert |
插入元素 (公众成员函数) |