C++ STL之映射map的使用

C++ STL之映射map的使用

map是键值对,⽐比如⼀一个⼈人名对应⼀一个学号,就可以定义⼀一个字符串串string类型的⼈人名为“键”,学号 int类型为“值”,如 map<string, int> m; 当然键、值也可以是其它变量量类型~map会⾃自动将所有的 键值对按照键从⼩小到⼤大排序,以下是map中常⽤用的⽅方法:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
	map<string, int> m; // 定义⼀一个空的map m,键是string类型的,值是int类型的 
	m["hello"] = 2; // 将key为"hello", value为2的键值对(key-value)存⼊入map中 
	cout << m["hello"] << endl; // 访问map中key为"hello"的value, 如果key不不存在,则返回0
	cout << m["world"] << endl;
	m["world"] = 3; // 将"world"键对应的值修改为3
	m[","] = 1; // 设⽴立⼀一组键值对,键为"," 值为1
// ⽤用迭代器器遍历,输出map中所有的元素,键⽤用it->first获取,值⽤用it->second获取 
	for (auto it = m.begin(); it != m.end(); it++) {
		cout << it->first << " " << it->second << endl; 
	}
// 访问map的第⼀一个元素,输出它的键和值
	cout << m.begin()->first << " " << m.begin()->second << endl; 
// 访问map的最后⼀一个元素,输出它的键和值
	cout << m.rbegin()->first << " " << m.rbegin()->second << endl; 
// 输出map的元素个数
	cout << m.size() << endl;
	return 0;
}
参考

柳神的博客

发布了91 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/WeDon_t/article/details/103758357