C++里map/multimap部分API总结

13 篇文章 0 订阅

1.map容器是一种关联式容器,在插入数据时会根据键值进行排序插入。
2.map容器的每一个元素都是对组,pair<key, value>,其中第一个key为键值,第二个value为实值。
3.键值不可以修改,实值可以修改,可以使用at(key)来访问实值.
4.map的迭代器不支持随机访问。
5.map的键值不允许重复,而multimap的键值可以重复.

#include<iostream>
#include<map>
#include<string>
using namespace std;

//map元素为对组<key,value>,第一个元素为键值,起到索引作用,第二个为实值
//按照键值排序,可以根据键值快速找到实值,键值不允许重复,有序对组
//迭代器不支持随机访问,只能递增++或递减--

void printMap(map<int, char>& m) {
	for (map<int, char>::iterator it(m.begin()); it != m.end(); ++it)
		cout << "键值:" << it->first << "\t实值:" << (*it).second << endl;
	cout << endl;
}

//初始化和赋值
void test01() {
	map<int, char> m;   //默认构造
	m.emplace(make_pair(1, 'f'));  //推荐使用
	m.insert(make_pair(3, 'd'));
	m.emplace(pair<int, char>(2, 'e'));  //对组匿名对象
	m.emplace(map<int, char>::value_type(0, 'e'));
	printMap(m);

	map<int, char> m2(m);   //拷贝构造
	printMap(m2);
	
	map<int, char> m3;
	m3 = m2;         //重载=赋值
	printMap(m3);
}

//大小和交换
void test02() {
	map<int, int> m;
	m.emplace(make_pair(1, 10));
	m.emplace(make_pair(2, 30));

	if (!m.empty())
		cout << "m的大小:" << m.size() << endl;
	else cout << "m容器为空" << endl;

	map<int, int> m2;
	m2.emplace(make_pair(1, 1000));
	m2.swap(m);
	cout << "m的大小:" << m.size() << endl;
}

void printInt(map<int, int>& m) {
	for (auto it = m.begin(); it != m.end(); ++it)
		cout << "键值:" << it->first << "\t实值:" << it->second << endl;
	cout << endl;
}
//删除
void test03() {
	map<int, int> m;
	m.emplace(make_pair(1, 200));
	m.emplace(make_pair(2, 300));
	m.emplace(make_pair(-1, 200));
	m.emplace(make_pair(0, 300));
	printInt(m);
	
	//1.按照键值进行删除
	m.erase(2);    //删除键值为2的对组
	printInt(m);
	//2.删除指定迭代器位置的对组
	m.erase(++m.begin());  //删除第二个位置的对组元素
	printInt(m);
	m.clear();   //清空
}

//查找
void test04() {
	map<int, string> m;
	m.emplace(1, "亚历山大");
	m.emplace(2, "拉格朗日");
	m.emplace(3, "莱布尼兹");
	
	//find(key)按照键值进行查找,找到返回该键值对应的对组的迭代器位置,
	//查找失败返回终止迭代器end()
	map<int, string>::iterator it = m.find(2); //查找键值为2的对组
	if (it != m.end())
		cout << "找到:" << it->second << endl;
	else cout << "不存在该对组" << endl;
	it->second = "张衡";
	cout << "修改后:" << it->second << endl;

	it = m.find(10);    //查找键值为10的对组
	if (it != m.end())
		cout << "找到:" << it->second << endl;
	else cout << "不存在该对组" << endl;
}

//统计count
//根据键值统计元素个数
void test05() {
	//map不会插入键值相同的对组
	map<string, int> m;
	m.emplace(pair<string, int>("商鞅", 4000000));
	m.emplace(make_pair("白起", 7800000));
	m.emplace(make_pair("白起", 30000000));   //map不会插入该元组
	cout << "m的大小:" << m.size() << endl;
	auto it = m.find("白起");
	if (it != m.end())
		cout << "姓名:" << it->first << "\t工资:" << it->second << endl;
	int num = m.count("商鞅");     //map里只有0或1
	cout << "商鞅个数:" << num << endl;

	//multimap允许插入键值相同的元组
	multimap<string, int> m2;
	m2.emplace(make_pair("西施", 22));
	m2.emplace(make_pair("西施", 20));   //multimap会插入该元组
	cout << "m2的大小:" << m2.size() << endl;
	num = m2.count("西施");
	cout << "西施个数:" << num << endl;
}

//类型系统内置的数据类型排序
class MyCompare {
public:
	//按照键值指定为降序排序
	bool operator()(int a, int b) {
		return a > b;
	}
};
void test06() {
	map<int, int, MyCompare> m;
	m.emplace(make_pair(1, 10));
	m.emplace(make_pair(2, 20));
	m.emplace(make_pair(3, 30));
	for (auto it = m.begin(); it != m.end(); ++it)
		cout << "key=" << it->first << "\tvalue=" << it->second << endl;
}

int main() {
	/*cout << "**************构造和赋值***********" << endl;
	test01();*/
	/*cout << "**************大小和交换***********" << endl;
	test02();*/
	/*cout << "**************删除***********" << endl;
	test03();*/
	/*cout << "**************查找***********" << endl;
	test04();*/
	/*cout << "**************统计***********" << endl;
	test05();*/
	cout << "**************自带数据类型排序***********" << endl;
	test06();

	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL中的mapmultimap是关联容器,用于存储键值对(key-value pairs),其中每个键(key)唯一对应一个值(value)。 map是一个有序容器,根据键的大小进行自动排序,默认按照键的升序进行排序。每个键只能在map中出现一次,如果尝试插入具有相同键的元素,新元素将替代旧元素。 multimap也是一个有序容器,与map不同的是,它允许多个具有相同键的元素存在。多个具有相同键的元素将按照插入的顺序进行存储,而不会自动排序。 这两个容器都提供了一系列的操作函数,如insert、erase、find等,用于插入、删除和查找元素。 以下是一个使用map的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores.insert(std::make_pair("Alice", 90)); scores.insert(std::make_pair("Bob", 80)); scores.insert(std::make_pair("Charlie", 70)); // 查找并输出Bob的分数 std::cout << "Bob's score: " << scores["Bob"] << std::endl; // 遍历并输出所有键值对 for (const auto& pair : scores) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 上述示例中,我们创建了一个存储string类型键和int类型值的map容器scores。通过insert函数依次插入了三个键值对。然后我们通过scores["Bob"]来获取Bob的分数,并输出结果为80。 接着我们使用范围-based for循环遍历map中的所有键值对,并输出每个键值对的键和值。 multimap的用法与map类似,只是它允许多个具有相同键的元素存在。 这些关联容器在查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用中,你可以根据自己的需求选择适合的容器类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值