cpp►STL容器->排序容器->multimap
Posted itzyjr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cpp►STL容器->排序容器->multimap相关的知识,希望对你有一定的参考价值。
描述
std::multimap
template <class Key, class T, class Compare = less<Key>,
class Alloc = allocator<pair<const Key, T>>>
class multimap;
multimap是一种关联容器,用于存储由key-value组合而成的元素,并遵循特定顺序,其中多个元素可以具有相同的key。
< utility > std::pair::pair
// pair::pair example
#include <utility>// std::pair, std::make_pair
#include <string>// std::string
#include <iostream>// std::cout
int main() {
std::pair<std::string, double> product1;// default constructor
std::pair<std::string, double> product2("tomatoes", 2.30);// value init
std::pair<std::string, double> product3(product2);// copy constructor
product1 = std::make_pair(std::string("lightbulbs"), 0.99);// using make_pair (move)
product2.first = "shoes";// the type of first is string
product2.second = 39.90;// the type of second is double
std::cout << "The price of " << product1.first << " is $" << product1.second << '\\n';
std::cout << "The price of " << product2.first << " is $" << product2.second << '\\n';
std::cout << "The price of " << product3.first << " is $" << product3.second << '\\n';
return 0;
}
The price of lightbulbs is $0.99
The price of shoes is $39.9
The price of tomatoes is $2.3
成员函数(Member functions)
构造函数:
// constructing multimaps
#include <iostream>
#include <map>
bool fncomp(char lhs, char rhs) {
return lhs < rhs;
}
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const {
return lhs < rhs;
}
};
int main() {
std::multimap<char, int> first;
// 用构造函数pair(const first_type& a, const second_type& b);初始化
first.insert(std::pair<char, int>('a', 10));
first.insert(std::pair<char, int>('b', 15));
first.insert(std::pair<char, int>('b', 20));
first.insert(std::pair<char, int>('c', 25));
std::multimap<char, int> second(first.begin(), first.end());
std::multimap<char, int> third(second);
std::multimap<char, int, classcomp> fourth;// class as Compare
bool(*fn_pt)(char, char) = fncomp;
std::multimap<char, int, bool(*)(char, char)> fifth(fn_pt);// function pointer as comp
return 0;
}
操作(Operations)
- equal_range
std::multimap::equal_range
pair<iterator, iterator> equal_range<const key_type& k);
返回包含容器中键等于k的所有元素的范围的边界。
函数返回一个pair,其成员pair::first是范围的下限(与lower_bound相同),pair::second是上限(与upper_bound相同)。
// multimap::equal_range
#include <iostream>
#include <map>
int main() {
std::multimap<char, int> mymm;
mymm.insert(std::pair<char, int>('a', 10));
mymm.insert(std::pair<char, int>('b', 20));
mymm.insert(std::pair<char, int>('b', 30));
mymm.insert(std::pair<char, int>('b', 40));
mymm.insert(std::pair<char, int>('c', 50));
mymm.insert(std::pair<char, int>('c', 60));
mymm.insert(std::pair<char, int>('d', 60));
std::cout << "mymm contains:\\n";
for (char ch = 'a'; ch <= 'd'; ch++) {
std::pair<std::multimap<char, int>::iterator, std::multimap<char, int>::iterator> ret;
ret = mymm.equal_range(ch);
std::cout << ch << " =>";
for (std::multimap<char, int>::iterator it = ret.first; it != ret.second; ++it)
std::cout << ' ' << it->second;
}
return 0;
}
mymm contains:
a => 10
b => 20 30 40
c => 50 60
d => 60
以上是关于cpp►STL容器->排序容器->multimap的主要内容,如果未能解决你的问题,请参考以下文章