std::map是一种有序关联容器,它包含具有唯一键的键值对。搜索、移除和插入的时间复杂度是 。底层通常是以红黑树实现的。
template<
class Key,
class T,
class Compare = std::less,
class Allocator = std::allocator>
> class map;
本文章的代码库:
https://gitee.com/gamestorm577/CppStd
可以用迭代器、另一个map或者元素列表来构造一个map。代码示例:
std::map m1;
m1[1] = 1.1f;
m1[2] = 2.1f;
m1[3] = 3.1f;
std::map m2(m1.begin(), std::next(m1.begin(), 1));
std::map m3(m2);
std::map tmp(m1);
std::map m4(std::move(tmp));
std::map m5{{1, 1.5f}, {2, 2.5f}};
std::cout << "m1 size = " << m1.size() << std::endl;
std::cout << "m2 size = " << m2.size() << std::endl;
std::cout << "m3 size = " << m3.size() << std::endl;
std::cout << "m4 size = " << m4.size() << std::endl;
std::cout << "m5 size = " << m5.size() << std::endl;
输出结果:
m1 size = 3
m2 size = 1
m3 size = 1
m4 size = 3
m5 size = 2
销毁map时会调用各元素的析构函数。代码示例:
struct MyStruct
{
MyStruct(int i)
: Index(i)
{
}
~MyStruct()
{
std::cout << "destruct, Index = " << Index << std::endl;
}
int Index = 0;
};
std::map m = {{1, 15}, {2, 25}};
std::cout << "end" << std::endl;
输出结果:
destruct, Index = 25
destruct, Index = 15
end
destruct, Index = 25
destruct, Index = 15
可以用另一个map或者元素列表给map赋值。代码示例:
std::map tmp{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::map m1;
std::map m2;
m1 = tmp;
m2 = {{1, 1.1f}, {2, 1.2f}};
std::cout << "m1 size = " << m1.size() << std::endl;
std::cout << "m2 size = " << m2.size() << std::endl;
输出结果:
m1 size = 3
m2 size = 2
返回指定key值的value值的引用,如果没有该key值,会抛出异常。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
m.at(2) = 20.5f;
std::cout << "m key 2 = " << m.at(2) << std::endl;
输出结果:
m key 2 = 20.5
返回指定key值的value值的引用,如果该没有key值,会插入该key值。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::cout << "m key 2 = " << m[2] << std::endl;
std::cout << "m key 20 = " << m[20] << std::endl;
输出结果:
m key 2 = 1.2
m key 20 = 0
接口begin、cbegin指向map起始的迭代器,end、cend指向末尾的迭代器。rbegin、crbegin指向起始的逆向迭代器,rend、crend指向末尾的逆向迭代器。代码示例:
std::map m{{5, 1.1f}, {17, 1.2f}, {19, 1.3f}};
for (auto iter = m.begin(); iter != m.end(); ++iter)
{
iter->second += 20.f;
}
for (auto iter = m.cbegin(); iter != m.cend(); ++iter)
{
std::cout << "key " << iter->first << " value = " << iter->second
<< std::endl;
}
输出结果:
key 5 value = 21.1
key 17 value = 21.2
key 19 value = 21.3
检查map是否为空。代码示例:
std::map m;
std::cout << std::boolalpha;
std::cout << "m empty: " << m.empty() << std::endl;
m[15] = 15.5f;
std::cout << "m empty: " << m.empty() << std::endl;
输出结果:
m empty: true
m empty: false
获取元素的个数。代码示例:
std::map m1;
std::map m2{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::cout << "m1 size = " << m1.size() << std::endl;
std::cout << "m2 size = " << m2.size() << std::endl;
输出结果:
m1 size = 0
m2 size = 3
返回可以容纳的对答元素数。代码示例:
std::map m1;
std::map m2;
std::cout << "m1 max_size = " << m1.max_size() << std::endl;
std::cout << "m2 max_size = " << m2.max_size() << std::endl;
可能的输出结果:
m1 max_size = 461168601842738790
m2 max_size = 384307168202282325
清除所有的元素。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::cout << "m size = " << m.size() << std::endl;
m.clear();
std::cout << "m size = " << m.size() << std::endl;
输出结果:
m size = 3
m size = 0
插入元素,参数可以是元素、迭代器或者元素节点。如果插入的key值已经存在,那么该key值不会被插入,对应的value值也不会修改。代码示例:
std::cout << std::boolalpha;
std::map m;
auto [item1, ret1] = m.insert({1, 12.5f});
std::cout << "ret1 = " << ret1 << std::endl;
auto [item2, ret2] = m.insert(*item1);
std::cout << "ret2 = " << ret2 << std::endl;
auto [item3, ret3] = m.insert({1, 15.5f});
std::cout << "ret3 = " << ret3 << std::endl;
std::cout << "m size = " << m.size() << std::endl;
std::cout << "m key 1 = " << m.at(1) << std::endl;
std::map tmp{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
m.insert(tmp.begin(), tmp.end());
std::cout << "m size = " << m.size() << std::endl;
std::cout << "m key 1 = " << m.at(1) << std::endl;
输出结果:
ret1 = true
ret2 = false
ret3 = false
m size = 1
m key 1 = 12.5
m size = 3
m key 1 = 12.5
插入一对键值,如果key已经存在,对应的value将被新的值替换。代码示例:
std::map m;
m.insert_or_assign(5, 17.3f);
std::cout << "m key 5 = " << m.at(5) << std::endl;
m.insert_or_assign(5, 35.4f);
std::cout << "m key 5 = " << m.at(5) << std::endl;
输出结果:
m key 5 = 17.3
m key 5 = 35.4
构造一对键值到map中。如果key值已经存在,将不会对map做任何修改。代码示例:
struct MyStruct
{
MyStruct(const std::string& str, double num)
: Str(str)
, Num(num)
{
}
std::string Str;
double Num;
};
std::map m;
std::cout << "m size = " << m.size() << std::endl;
m.emplace(5, MyStruct{"a", 1.3});
std::cout << "m size = " << m.size() << std::endl;
m.emplace(5, MyStruct{"b", 2.3});
std::cout << "m size = " << m.size() << std::endl;
m.emplace(25, MyStruct{"a", 1.3});
std::cout << "m size = " << m.size() << std::endl;
std::cout << "m key 5 is: " << m.at(5).Num << std::endl;
输出结果:
m size = 0
m size = 1
m size = 1
m size = 2
m key 5 is: 1.3
向map中尽可能靠近hint之前的位置插入新元素。
template
iterator emplace_hint(const_iterator hint, _Args&&... args)
不同的hint会导致插入元素的效率不同。代码示例:
auto timer = [](std::function func, std::string tag) -> void
{
auto start = std::chrono::system_clock::now();
std::size_t size = func();
auto end = std::chrono::system_clock::now();
std::chrono::duration time = end - start;
std::cout << tag << ", size: " << size << ", use time: " << time.count()
<< std::endl;
};
const int count = 1000000;
auto map_emplace = [=]() -> std::size_t
{
std::map m;
for (int i = 0; i < count; ++i)
{
m.emplace(i, 'a');
}
return m.size();
};
auto map_emplace_hint1 = [=]() -> std::size_t
{
std::map m;
auto iter = m.begin();
for (int i = 0; i < count; ++i)
{
m.emplace_hint(iter, i, 'a');
iter = m.end();
}
return m.size();
};
auto map_emplace_hint2 = [=]() -> std::size_t
{
std::map m;
auto iter = m.begin();
for (int i = 0; i < count; ++i)
{
m.emplace_hint(iter, i, 'a');
iter = m.begin();
}
return m.size();
};
auto map_emplace_hint3 = [=]() -> std::size_t
{
std::map m;
auto iter = m.begin();
for (int i = 0; i < count; ++i)
{
iter = m.emplace_hint(iter, i, 'a');
}
return m.size();
};
timer(map_emplace, "map_emplace");
timer(map_emplace_hint1, "map_emplace_hint1");
timer(map_emplace_hint2, "map_emplace_hint2");
timer(map_emplace_hint3, "map_emplace_hint3");
可能的输出结果:
map_emplace, size: 1000000, use time: 620.713
map_emplace_hint1, size: 1000000, use time: 260.807
map_emplace_hint2, size: 1000000, use time: 693.763
map_emplace_hint3, size: 1000000, use time: 382.412
构造一对键值到map中。如果key值已经存在,将不会对map做任何修改。代码示例:
std::map m;
m.emplace_hint(m.begin(), 5, 15.1);
m.emplace_hint(m.begin(), 17, 45.5);
m.emplace_hint(m.begin(), 5, 39.4);
m.emplace_hint(m.begin(), 29, 19.4);
for (auto& item : m)
{
std::cout << "key: " << item.first << ", value: " << item.second
<< std::endl;
}
输出结果:
key: 5, value: 15.1
key: 17, value: 45.5
key: 29, value: 19.4
移除指定位置或者指定key的容器。代码示例:
std::map m{{5, 1.1f}, {7, 1.2f}, {19, 1.3f}, {29, 1.4f}};
std::cout << "m size = " << m.size() << std::endl;
m.erase(m.begin());
std::cout << "m size = " << m.size() << std::endl;
m.erase(18);
std::cout << "m size = " << m.size() << std::endl;
m.erase(19);
std::cout << "m size = " << m.size() << std::endl;
输出结果:
m size = 4
m size = 3
m size = 3
m size = 2
和另一个map交换元素内容。代码示例:
std::map m1{{5, 1.1f}, {7, 1.2f}, {19, 1.3f}, {29, 1.4f}};
std::map m2{{15, 1.1f}, {27, 1.2f}};
m1.swap(m2);
std::cout << "m1 size = " << m1.size() << std::endl;
std::cout << "m2 size = " << m2.size() << std::endl;
输出结果:
m1 size = 2
m2 size = 4
提取map中的某个元素节点,提取后map不再拥有该元素。代码示例:
std::map m{{5, 1.1f}, {7, 1.2f}, {19, 1.3f}, {29, 1.4f}};
std::cout << "m size: " << m.size() << std::endl;
auto node = m.extract(5);
std::cout << "m size: " << m.size() << std::endl;
m.insert(std::move(node));
std::cout << "m size: " << m.size() << std::endl;
输出结果:
m size: 4
m size: 3
m size: 4
合并另一个map,只插入不存在的key值。代码示例:
std::map m1{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::map m2{{3, 50.1f}, {4, 50.2f}};
m1.merge(m2);
for (auto& item : m1)
{
std::cout << "key: " << item.first << ", value: " << item.second
<< std::endl;
}
输出结果:
key: 1, value: 1.1
key: 2, value: 1.2
key: 3, value: 1.3
key: 4, value: 50.2
返回匹配key值的元素个数,结果只能是0或者1。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::cout << "m key 1 count: " << m.count(1) << std::endl;
std::cout << "m key 4 count: " << m.count(4) << std::endl;
输出结果:
m key 1 count: 1
m key 4 count: 0
返回匹配key值的迭代器。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::cout << "m key 2 value is: " << m.find(2)->second << std::endl;
输出结果:
m key 2 value is: 1.2
检查是否包含某个key值,返回布尔值。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::cout << std::boolalpha;
std::cout << "m contains 2: " << m.contains(2) << std::endl;
std::cout << "m contains 4: " << m.contains(4) << std::endl;
输出结果:
m contains 2: true
m contains 4: false
返回首个大于等于给定键值的迭代器。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
auto iter = m.lower_bound(2);
std::cout << "key = " << iter->first << std::endl;
输出结果:
key = 2
返回首个大于给定键值的迭代器。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
auto iter = m.upper_bound(2);
std::cout << "key = " << iter->first << std::endl;
输出结果:
key = 3
返回一对迭代器,分别是lower_bound和upper_range的结果。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
auto [iter1, iter2] = m.equal_range(2);
std::cout << "iter1 key = " << iter1->first << std::endl;
std::cout << "iter2 key = " << iter2->first << std::endl;
输出结果:
iter1 key = 2
iter2 key = 3
返回用于比较key值的函数。代码示例:
struct MyComp
{
bool operator()(int lhs, int rhs) const
{
std::cout << "MyComp" << std::endl;
return true;
}
};
std::map m;
auto comp = m.key_comp();
comp(10, 20);
输出结果:
MyComp
返回用于比较键值对的函数,参数是键值对,但实际是比较的key值。代码示例:
struct MyComp
{
bool operator()(int lhs, int rhs) const
{
std::cout << "MyComp" << std::endl;
return true;
}
};
std::map m;
auto comp = m.value_comp();
comp(std::make_pair(1, "a"), std::make_pair(2, "b"));
输出结果:
MyComp
operator==,!=,<,<=,>,>=用于比较两个map。代码示例:
std::map m1{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::map m2{{1, 1.1f}, {2, 1.2f}};
std::cout << std::boolalpha;
std::cout << "m1 == m2: " << (m1 == m2) << std::endl;
std::cout << "m1 != m2: " << (m1 != m2) << std::endl;
std::cout << "m1 < m2: " << (m1 < m2) << std::endl;
std::cout << "m1 <= m2: " << (m1 <= m2) << std::endl;
std::cout << "m1 > m2: " << (m1 > m2) << std::endl;
std::cout << "m1 >= m2: " << (m1 >= m2) << std::endl;
输出结果:
m1 == m2: false
m1 != m2: true
m1 < m2: false
m1 <= m2: false
m1 > m2: true
m1 >= m2: true
交换两个map的元素内容。代码示例:
std::map m1{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::map m2{{1, 1.1f}, {2, 1.2f}};
std::swap(m1, m2);
std::cout << "m1 size = " << m1.size() << std::endl;
std::cout << "m2 size = " << m2.size() << std::endl;
输出结果:
m1 size = 2
m2 size = 3
删除满足要求的元素。代码示例:
std::map m{{1, 1.1f}, {2, 1.2f}, {3, 21.3f}, {4, 21.5f}};
std::erase_if(m,
[](const std::pair& pair) -> bool
{
return pair.second > 10.f;
});
for (auto& item : m)
{
std::cout << "key: " << item.first << ", value: " << item.second
<< std::endl;
}
输出结果:
key: 1, value: 1.1
key: 2, value: 1.2