本文主要是介绍容器库(9)-std::multimap,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
std::multimap是一种关联容器,包含键值对的已排序的列表。和std::map不同的是,std::multimap允许重复的key值。搜索、移除和插入操作具有对数复杂度。
template<class Key,class T,class Compare = std::less<Key>,class Allocator = std::allocator<std::pair<const Key, T>>
> class multimap;
本文章的代码库:
https://gitee.com/gamestorm577/CppStd
成员函数
构造、析构和赋值
构造函数
可以用迭代器、另一个multimap或者元素列表来构造一个multimap。代码示例:
std::multimap<int, float> m1{{1, 1.5f}, {1, 1.2f}, {2, 2.5f}};
std::multimap<int, float> m2(m1.begin(), std::next(m1.begin(), 1));
std::multimap<int, float> m3(m2);
std::multimap<int, float> tmp(m1);
std::multimap<int, float> m4(std::move(tmp));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;
输出结果:
m1 size = 3
m2 size = 1
m3 size = 1
m4 size = 3
析构函数
销毁multimap时会调用各元素的析构函数。代码示例:
struct MyStruct
{MyStruct(int i): Index(i){}~MyStruct(){std::cout << "destruct, Index = " << Index << std::endl;}int Index = 0;
};std::multimap<int, MyStruct> m = {{1, 15}, {1, 20}, {2, 25}};
std::cout << "end" << std::endl;
输出结果:
destruct, Index = 25
destruct, Index = 20
destruct, Index = 15
end
destruct, Index = 25
destruct, Index = 20
destruct, Index = 15
赋值函数
可以用另一个multimap或者元素列表给multimap赋值。代码示例:
std::multimap<int, float> tmp{{1, 1.1f}, {1, 1.2f}, {3, 1.3f}};
std::multimap<int, float> m1;
std::multimap<int, float> m2;m1 = tmp;
m2 = {{1, 1.1f}, {1, 1.2f}};
std::cout << "m1 size = " << m1.size() << std::endl;
std::cout << "m2 size = " << m2.size() << std::endl;
输出结果:
m1 size = 3
m2 size = 2
迭代器
接口begin、cbegin指向multimap起始的迭代器,end、cend指向末尾的迭代器。rbegin、crbegin指向起始的逆向迭代器,rend、crend指向末尾的逆向迭代器。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 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 1 value = 21.1
key 1 value = 21.2
key 2 value = 21.3
容量
empty
检查multimap是否为空。代码示例:
std::multimap<int, float> m1;
std::multimap<int, float> m2{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::cout << std::boolalpha;
std::cout << "m1 empty: " << m1.empty() << std::endl;
std::cout << "m2 empty: " << m2.empty() << std::endl;
输出结果:
m1 empty: true
m2 empty: false
size
获取元素的个数。代码示例:
std::multimap<int, float> m1;
std::multimap<int, float> m2{{1, 1.1f}, {1, 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
max_size
返回可以容纳的最大元素数。代码示例:
std::multimap<int, int> m1;
std::multimap<double, double> 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
修改器
clear
清除所有的元素。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 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
insert
插入元素,参数可以是元素、迭代器或者元素节点。代码示例:
std::multimap<int, float> m{{1, 1.1f}};m.insert({1, 12.5f});
m.insert({1, 12.5f});
m.insert({2, 12.5f});
std::cout << "m size = " << m.size() << std::endl;std::multimap<int, float> tmp{{1, 1.1f}, {2, 1.2f}, {2, 1.3f}};
m.insert(tmp.begin(), tmp.end());
std::cout << "m size = " << m.size() << std::endl;
输出结果:
m size = 4
m size = 7
emplace
构造一对键值到multimap中。代码示例:
std::multimap<int, float> m;
m.emplace(1, 12.5f);
m.emplace(1, 12.5f);
m.emplace(2, 12.5f);
std::cout << "m size = " << m.size() << std::endl;
输出结果:
m size = 3
emplace_hint
向multimap中尽可能靠近hint之前的位置插入新元素:
template <class... Args>
iterator emplace_hint(const_iterator hint, Args&&... args);
不同的hint会导致插入元素的效率不同。代码示例:
auto timer = [](std::function<std::size_t()> 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<double, std::milli> time = end - start;std::cout << tag << ", size: " << size << ", use time: " << time.count()<< std::endl;
};const int count = 1000000;auto multimap_emplace = [=]() -> std::size_t
{std::multimap<int, char> m;for (int i = 0; i < count; ++i){m.emplace(i, 'a');}return m.size();
};auto multimap_emplace_hint1 = [=]() -> std::size_t
{std::multimap<int, char> 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 multimap_emplace_hint2 = [=]() -> std::size_t
{std::multimap<int, char> 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 multimap_emplace_hint3 = [=]() -> std::size_t
{std::multimap<int, char> m;auto iter = m.begin();for (int i = 0; i < count; ++i){iter = m.emplace_hint(iter, i, 'a');}return m.size();
};timer(multimap_emplace, "multimap_emplace");
timer(multimap_emplace_hint1, "multimap_emplace_hint1");
timer(multimap_emplace_hint2, "multimap_emplace_hint2");
timer(multimap_emplace_hint3, "multimap_emplace_hint3");
输出结果:
multimap_emplace, size: 1000000, use time: 1686.37
multimap_emplace_hint1, size: 1000000, use time: 777.029
multimap_emplace_hint2, size: 1000000, use time: 1246.12
multimap_emplace_hint3, size: 1000000, use time: 1177.37
erase
移除指定位置或者指定key的容器。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {1, 1.3f}, {2, 2.1f},{2, 2.2f}, {3, 3.1f}, {3, 3.2f}};
std::cout << "m size = " << m.size() << std::endl;
m.erase(m.begin());
std::cout << "m size = " << m.size() << std::endl;
m.erase(m.begin(), std::next(m.begin(), 2));
std::cout << "m size = " << m.size() << std::endl;
m.erase(3);
std::cout << "m size = " << m.size() << std::endl;
输出结果:
m size = 7
m size = 6
m size = 4
m size = 2
swap
和另一个multimap交换元素内容。代码示例:
std::multimap<int, float> m1{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::multimap<int, float> m2{{1, 4.1f}, {2, 4.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 = 3
extract
提取multimap中的某个元素节点,提取后multimap不再拥有该元素。如果有多个元素和指定的key值相同,则提取第一个。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}, {2, 1.4f}};
std::cout << "m size: " << m.size() << std::endl;
m.extract(1);
std::cout << "m size: " << m.size() << std::endl;
auto node = m.extract(2);
std::cout << "m size: " << m.size() << std::endl;
std::cout << "node value: " << node.mapped() << std::endl;
m.insert(std::move(node));
std::cout << "m size: " << m.size() << std::endl;
输出结果:
m size: 4
m size: 3
m size: 2
node value: 1.3
m size: 3
merge
合并另一个map或者multimap。代码示例:
std::multimap<int, float> m1{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::map<int, float> m2{{1, 1.1f}, {2, 1.2f}};
std::multimap<int, float> m3{{2, 1.1f}, {2, 1.2f}};
std::cout << "m1 size = " << m1.size() << std::endl;
m1.merge(m2);
std::cout << "m1 size = " << m1.size() << std::endl;
m1.merge(m3);
std::cout << "m1 size = " << m1.size() << std::endl;
输出结果:
m1 size = 3
m1 size = 5
m1 size = 7
查找
count
返回匹配key值的元素个数。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::cout << "m key 1 count: " << m.count(1) << std::endl;
std::cout << "m key 2 count: " << m.count(2) << std::endl;
std::cout << "m key 3 count: " << m.count(3) << std::endl;
输出结果:
m key 1 count: 2
m key 2 count: 1
m key 3 count: 0
find
返回匹配key值的迭代器。如果有多个配备的元素,那么返回任意一个。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::cout << "m key 1 value is: " << m.find(1)->second << std::endl;
输出结果:
m key 1 value is: 1.1
contains
检查是否包含某个key值,返回布尔值。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::cout << std::boolalpha;
std::cout << "m contains 1: " << m.contains(1) << std::endl;
std::cout << "m contains 3: " << m.contains(3) << std::endl;
输出结果:
m contains 1: true
m contains 3: false
lower_bound
返回首个大于等于给定键值的迭代器。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 2.1f}, {2, 2.2f},{2, 2.3f}, {3, 3.1f}, {3, 3.2f}};
auto iter = m.lower_bound(2);
std::cout << "key: " << iter->first << ", value: " << iter->second<< std::endl;
输出结果:
key: 2, value: 2.1
upper_bound
返回首个大于给定键值的迭代器。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 2.1f}, {2, 2.2f},{2, 2.3f}, {3, 3.1f}, {3, 3.2f}};
auto iter = m.upper_bound(2);
std::cout << "key: " << iter->first << ", value: " << iter->second<< std::endl;
输出结果:
key: 3, value: 3.1
equal_range
返回一对迭代器,分别是lower_bound和upper_range的结果。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 2.1f}, {2, 2.2f},{2, 2.3f}, {3, 3.1f}, {3, 3.2f}};
auto [iter1, iter2] = m.equal_range(2);
std::cout << "iter1 key: " << iter1->first << ", value: " << iter1->second<< std::endl;
std::cout << "iter2 key: " << iter2->first << ", value: " << iter2->second<< std::endl;
输出结果:
iter1 key: 2, value: 2.1
iter2 key: 3, value: 3.1
观察器
key_comp
返回用于比较key值的函数。代码示例:
struct MyComp
{bool operator()(int lhs, int rhs) const{std::cout << "MyComp" << std::endl;return true;}
};std::multimap<int, std::string, MyComp> m;
auto comp = m.key_comp();
comp(10, 20);
输出结果:
MyComp
value_comp
返回用于比较键值对的函数,参数是键值对,但实际是比较的key值。代码示例:
struct MyComp
{bool operator()(int lhs, int rhs) const{std::cout << "MyComp" << std::endl;return true;}
};std::multimap<int, std::string, MyComp> m;
auto comp = m.value_comp();
comp(std::make_pair(1, "a"), std::make_pair(2, "b"));
输出结果:
MyComp
非成员函数
比较运算符
operator==,!=,<,<=,>,>=用于比较两个multimap。代码示例:
std::multimap<int, float> m1{{1, 1.1f}, {1, 1.2f}, {2, 1.3f}};
std::multimap<int, float> m2{{1, 1.1f}, {1, 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
swap
交换两个multimap的元素内容。代码示例:
std::multimap<int, float> m1{{1, 1.1f}, {2, 1.2f}, {3, 1.3f}};
std::multimap<int, float> 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
erase_if
删除满足要求的元素。代码示例:
std::multimap<int, float> m{{1, 1.1f}, {1, 1.2f}, {2, 21.3f}, {2, 21.5f}};
std::erase_if(m,[](const std::pair<int, float>& 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: 1, value: 1.2
这篇关于容器库(9)-std::multimap的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!