您现在的位置是:首页 > 编程 > 

map的使用(C++)

2025-07-20 21:49:16
map的使用(C++) map的使用简介:map是C++的STL中最常用的容器之一,他对于算法题的在算法题与工程项目中的贡献难以替代,本文旨在快速让读者入门map的使用,附带英文解说。map/multimap的基本概念(它们都是同一个头文件)注意:Python里面的字典就是这么造出来的.insert函数仔细看,里面的参数是pair,然后通过输出可以看出,元素按照对组的key值进行排序,也就是fir

map的使用(C++)

map的使用

简介:map是C++的STL中最常用的容器之一,他对于算法题的在算法题与工程项目中的贡献难以替代,本文旨在快速让读者入门map的使用,附带英文解说。

map/multimap的基本概念(它们都是同一个头文件)

注意:Python里面的字典就是这么造出来的.

insert函数仔细看,里面的参数是pair,然后通过输出可以看出,元素按照对组的key值进行排序,也就是first值。 multimap除了允许重复的key值以外其他的和map一样

代码语言:javascript代码运行次数:0运行复制
ote: This is how the dictionary in Python is made
If you look at the insert function carefully, the parameters in it are "pair". Then you can see from the output that the elements are sorted according to the key value of the group, that is the "first" value.
multimap is the same as map except for duplicate key values
代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>

using namespace std;
/* map container ctruction and assignment*/
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	// Create map container

	// Default ctruction
	map<int, int>m;
	m.insert(pair<int, int>(2, 10));
	m.insert(pair<int, int>(1, 120));
	m.insert(pair<int, int>(4, 100));
	m.insert(pair<int, int>(, 400));
	cout << "m: " << endl;
	PrintMap(m);
	cout << endl;

	// Copy ctruction
	map<int, int>m2(m);
	cout << "m2: " << endl;
	PrintMap(m2);
	cout << endl;

	// Assignment
	map<int, int>m;
	m = m2;
	cout << "m: " << endl;
	PrintMap(m);

}![请添加图片描述](.jpeg)


void test02()
{
	multimap<int, string>mm;
	mm.insert(pair<int, string>(2, "刘备"));
	mm.insert(pair<int, string>(1, "小明"));
	mm.insert(pair<int, string>(5, "宋神宗"));
	mm.insert(pair<int, string>(2, "李斯"));
	mm.insert(pair<int, string>(8, "曹操"));
	mm.insert(pair<int, string>(1, "路飞"));
	cout << "mm: " << endl;
	for (multimap<int, string>::iterator it = mm.begin(); it != m(); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}
int main()
{
	test01();
	cout << endl << "---------------------" << endl;
	test02();
	return 0;
}

运行结果

map大小和交换

注意:swap函数目前来看只支持相同数据类型的集合之间的交换

代码语言:javascript代码运行次数:0运行复制
ote: at present, swap function only supports exchange between sets of the same data type
代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>
#include<string>

using namespace std;
/* map container size and swap*/
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}

void PrintMap(map<string, int>& m)
{
	for (map<string, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}
void test01()
{
	// Create map container

	map<int, int>m;
	m.insert(pair<int, int>(2, 10));
	m.insert(pair<int, int>(1, 120));
	m.insert(pair<int, int>(4, 100));
	m.insert(pair<int, int>(, 400));
	
	if (())
	{
		cout << "m为空" << endl;
	}
	else
	{
		cout << "m不为空" << endl;
		cout << "m的大小:" << m.size() << endl;
	}
	cout << endl;
	cout << "交换前: " << endl;
	cout << "m: " << endl;
	PrintMap(m);
	cout << endl;

	map<string, int>m2;
	m2.insert(pair<string, int>("玄德", 12));
	m2.insert(pair<string, int>("玄德", 2));
	m2.insert(pair<string, int>("张飞", 4));
	m2.insert(pair<string, int>("关于", 1));
	m2.insert(pair<string, int>("曹操", 2));
	m2.insert(pair<string, int>("张建", 5));
	m2.insert(pair<string, int>("皓一", 6));

	// string 字符串如果是中文的话,应该是按照汉字的编码表来排序的,但是汉字的编码表不止一张,这些之后再讨论
	cout << "m2: " << endl;
	PrintMap(m2);
	cout << endl;
	

	map<int, int>m;
	m.insert(pair<int, int>(12, 12));
	m.insert(pair<int, int>(9, 2));
	m.insert(pair<int, int>(5, 4));
	m.insert(pair<int, int>(4, 1));
	m.insert(pair<int, int>(, 2));
	m.insert(pair<int, int>(45, 5));
	m.insert(pair<int, int>(1, 6));

	cout << "m: " << endl;
	PrintMap(m);
	cout << endl;

	cout << "------------------------" << endl;
	cout << "交换后:" << endl;
	// m.swap(m2);  // 这里可以看见, swap函数只支持相同类型的map容器之间的交换
	m.swap(m);

	cout << "m: " << endl;
	PrintMap(m);
	cout << endl;
	
	cout << "m: " << endl;
	PrintMap(m);
	cout << endl;
}

int main()
{
	test01();

	return 0;
}

运行结果

map插入和删除

注意:第三种插入方式太长不建议使用,第四种方式虽然看起来简短但是也不建议使用,这个括号[]的目的不是用来设置元素的,用于设置元素的话,容易导致混乱,后面的一个例子会说明。 []主要是用来访问的,当我们确定这个key的值的时候我们就可以通过key来访问,key对应的值。

代码语言:javascript代码运行次数:0运行复制
ote: the third insertion method is too long and is not recommend. Although the fourth insertion method looks short, it is not recommended. The purpose of this [] is not to set
elements. If it is used to set elements, it will easily lead to confusion. A later example will illustrate.
When we determine the value of the key, we can access the corresponding value of the key through the key.

第一种情况

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>
#include<string>

using namespace std;
/* map container insertion and deletion*/
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}


void test01()
{
	// Create map container
	map<int, int>m;

	// insert
	// The first method 
	m.insert(pair<int, int>(1, 10));
	
	// The second method
	m.insert(make_pair(2, 20));

	// The third method 
	m.insert(map<int, int>::value_type(, 0));

	// The fourth mathod
	
	m[4] = 40;
	cout << "m[5] = ";
	cout << m[5] << endl;
	cout << "m[6] = ";
	cout << m[6] << endl;
	// 这两处位置就体现了一个问题,明明在插入的时候没有插入m[5], m[6]但是在打印的时候这两个却可以打印出来
	// 而且后面的PrintMap也将这个两个打印出来了
	// 这里要说明的是如果没有插入m[n],是可以输出这个m[n]的,但是它的值为0

	cout << endl << "m: " << endl;
	PrintMap(m);
}

int main()
{
	test01();

	return 0;
}

运行结果

第二种情况

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>
#include<string>

using namespace std;
/* map container insertion and deletion*/
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}


void test01()
{
	// Create map container
	map<int, int>m;

	// insert
	// The first method 
	m.insert(pair<int, int>(1, 10));
	
	// The second method
	m.insert(make_pair(2, 20));

	// The third method 
	m.insert(map<int, int>::value_type(, 0));

	// The fourth mathod
	
	// m[4] = 40;  把这里的m[4]给注释了之后,在这个容器中就没有了这个值,然后打印的时候就不会出现这个值
					// 及时是PrintMap的打印,这一体现出了map容器的元素的存储在空间上也不是连续的
	cout << "m[5] = ";
	cout << m[5] << endl;
	cout << "m[6] = ";
	cout << m[6] << endl;
	// 这两处位置就体现了一个问题,明明在插入的时候没有插入m[5], m[6]但是在打印的时候这两个却可以打印出来
	// 而且后面的PrintMap也将这个两个打印出来了
	// 这里要说明的是如果没有插入m[n],是可以输出这个m[n]的,但是它的值为0

	cout << endl << "m: " << endl;
	PrintMap(m);
}

int main()
{
	test01();

	return 0;
}

运行结果

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>
#include<string>

using namespace std;
/* map container insertion and deletion*/
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}


void test01()
{
	// Create map container
	map<int, int>m;

	// insert
	// The first method 
	m.insert(pair<int, int>(1, 10));
	
	// The second method
	m.insert(make_pair(2, 20));

	// The third method 
	m.insert(map<int, int>::value_type(, 0));

	// The fourth mathod
	
	m[4] = 40; 
	// []不建议插入,用途 可以利用key访问到value
	// cout << m[4]<< endl;
	cout << endl << "m: " << endl;
	PrintMap(m);
	cout << endl;

	// erase
	(m.begin());  // Through iterator
	cout << endl << "m: " << endl;
	PrintMap(m);
	cout << endl;

	();  // Through value
	cout << endl << "m: " << endl;
	PrintMap(m);
	cout << endl;

	(m.begin(), ());  // 等价于();
	cout << endl << "m: " << endl;
	PrintMap(m);
	cout << endl;

}

int main()
{
	test01();

	return 0;
}

运行结果

map查和统计

注意find返回的迭代器,不是具体位置,将find的结果打印出来的话就是这个值。 在mapcount就只有0或者1multimap允许重复key值里面有多种结果。

代码语言:javascript代码运行次数:0运行复制
ote: the iterator returned by find function is not the specific location. If the result of find function is printed out, it is this value.
In map, count is only 0 or 1, and multimap allows mulitimap results in repeated key values.
代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>
#include<string>

using namespace std;
/* map container find and count*/
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}


void test01()
{
	// Create map container
	map<int, int>m;
	m.insert(make_pair(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(map<int, int>::value_type(, 0));
	m[4] = 40;
	m[4] = 40;
	m[4] = 40;
	m[4] = 40;

	map<int, int>::iterator pos = m.find(2);
	if (pos != ())
	{
		cout << "到这个元素 key = " << pos->first << "  value = "<< pos->second <<endl;
	}
	else
	{
		cout << "未到这个元素" << endl;
	}
	cout << endl;
	int num = (4);
	// num = 1, map 不允许插入重复的元素
	cout << "num = " << num << endl;
}

int main()
{
	test01();

	return 0;
}

运行结果

总结

map容器排序

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
#include<map>
#include<string>

using namespace std;
/* map sort*/

class Mycmp  // 这个就是仿函数	  
{
public:
	bool operator()(int m1, int m2) ct
	{
		// 降序
		return m1 > m2;
	}
};
void PrintMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}


void test01()
{
	// Create map container
	map<int, int>m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(, 0));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));

	cout << "m: " << endl;
	for (map<int, int>::iterator it = m.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;

	map<int, int, Mycmp>m2;  // 仿函数必须在定义的时候使用,已经定义好了的,排序规则不可						  // 改变
	m2.insert(make_pair(1, 10));
	m2.insert(make_pair(2, 20));
	m2.insert(make_pair(, 0));
	m2.insert(make_pair(4, 40));
	m2.insert(make_pair(5, 50));

	cout << "m2: " << endl;
	for (map<int, int, Mycmp>::iterator it = m2.begin(); it != (); it++)
	{
		cout << "key = " << it->first << "  value = " << it->second << endl;
	}
	cout << endl;
}

int main()
{
	test01();

	return 0;
}

运行结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2024-01-12,如有侵权请联系 cloudcommunity@tencent 删除mapc++coutinsertint

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1160311.html

相关标签:无
上传时间: 2025-07-20 13:26:03
留言与评论(共有 13 条评论)
本站网友 晟天新能源
17分钟前 发表
但是它的值为0 cout << endl << "m
本站网友 免疫力低
27分钟前 发表
排序规则不可 // 改变 m2.insert(make_pair(1
本站网友 3839小游戏
10分钟前 发表
int>m; // insert // The first method m.insert(pair<int
本站网友 平安保险一账通
18分钟前 发表
用途 可以利用key访问到value // cout << m[4]<< endl; cout << endl << "m
本站网友 贺普丁
7分钟前 发表
value_type(
本站网友 聊天网页
6分钟前 发表
" << endl; PrintMap(m); cout << endl; // Copy ctruction map<int
本站网友 蚌埠祛斑
18分钟前 发表
int>m; // insert // The first method m.insert(pair<int
本站网友 余额宝支付限额
7分钟前 发表
但是它的值为0 cout << endl << "m
本站网友 中国先锋网
15分钟前 发表
" << endl; PrintMap(m); } int main() { test01(); return 0; }运行结果: 代码语言:javascript代码运行次数:0运行复制#include<iostream> #include<map> #include<string> using namespace std; /* map container insertion and deletion*/ void PrintMap(map<int
本站网友 梦幻西游豪宅价格
11分钟前 发表
and multimap allows mulitimap results in repeated key values.代码语言:javascript代码运行次数:0运行复制#include<iostream> #include<map> #include<string> using namespace std; /* map container find and count*/ void PrintMap(map<int
本站网友 写小说的网站
26分钟前 发表
10)); m.insert(pair<int
本站网友 梦幻西游服务端
26分钟前 发表
int>m; m.insert(pair<int