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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itzyjr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值