map容器的一些方法说明

#include <stdio.h>
#include <string>
#include <map>
#include <memory>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, int> dmap;
	dmap.insert(pair<int, int>(3, 1));
	dmap.insert(pair<int, int>(4, 1));
	dmap.insert(pair<int, int>(5, 1));
	dmap.insert(pair<int, int>(6, 1));
	//count函数计算map中指定key的元素有多少个
	printf("count key[%d], num: %d\n", 3, dmap.count(3));
	printf("count key[%d], num: %d\n", 9, dmap.count(9));
	//size函数计算map中的元素数量
	printf("%d\n", dmap.size());
	//max_size计算map中最多容纳的元素
	printf("%0x\n", dmap.max_size());
	//empty检查map是否为空
	printf("%d\n", dmap.empty());
	dmap.erase(dmap.begin(), dmap.end());
	printf("%d\n", dmap.empty());
	getchar();
	return 0;
}

运行结果为:

count key[3], num: 1
count key[9], num: 0
4
aaaaaaa
0
1


[]运算符:

#include <stdio.h>
#include <string>
#include <map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, int> imap;
	map<int, string> smap;
	imap[1] = 1;
	smap[1] = "hello";
	printf("imap[%d] = %d\n", 1, imap[1]);
	printf("smap[%d] = %s\n", 1, smap[1].c_str());
	printf("imap[%d] = %d\n", 2, imap[2]);
	printf("smap[%d] = %s\n", 2, smap[2].c_str());
	getchar();
	return 0;
}

运算结果为:

imap[1] = 1
smap[1] = hello
imap[2] = 0
smap[2] =

#include <stdio.h>
#include <string>
#include <map>
using namespace std;

class A
{
public:
	A() { m_a = -1; }
	A(int a) { m_a = 100; }
	int m_a;
};

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, A> imap;
	A a = A(1);
	imap[1] = a;
	A b = imap[1];
	printf("%d\n", b.m_a);
	A c = imap[10];
	printf("%d\n", c.m_a);

	getchar();
	return 0;
}
运算结果为:

100
-1

结果分析:下标运算符可以在map中取得key对应的value,如果key不存在,会返回一个对应类型的默认值。


#include <stdio.h>
#include <map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, int> imap;
	int i = imap[1];
	map<int, int>::iterator iter = imap.begin();
	for (; iter != imap.end(); iter++)
		printf("%d, %d\n", iter->first, iter->second);
	getchar();
	return 0;
}

运行结果为:

1, 0

分析:如果以key为键的元素并不在map中的话, [key]会导致在map中自动插入一个元素


find函数使用

#include <stdio.h>
#include <string>
#include <map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, int> imap;
	imap[1] = 1;
	imap[2] = 4;
	imap[3] = 8;
	map<int, int>::iterator iter = imap.find(1);
	if (iter != imap.end())
		printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
	else
		printf("iter end\n");

	iter = imap.find(10);
	if (iter != imap.end())
		printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
	else
		printf("iter end\n");

	getchar();
	return 0;
}

运行结果为:

iter->first: 1, iter->second: 1
iter end

分析: find返回一个迭代器, 它指向查找的key对应的元素,如果key不存在,则返回map.end()


at函数

#include <stdio.h>
#include <string>
#include <map>
#include <memory>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, int> imap;
	imap[1] = 1;
	imap[2] = 4;
	imap[3] = 8;
	int i = imap.at(1);      //返回1
	printf("%d\n", i);
	i = imap.at(10);         //出错,at方法中检测到访问不存在的key,会引发异常
	printf("%d\n", i);

	getchar();
	return 0;
}

分析:at的用法和[]类似,不过[]访问不存在的key会返回类型空值,at则会程序崩溃


swap方法:

#include <stdio.h>
#include <string>
#include <map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	map<int, int> imap1;
	map<int, int> imap2;
	imap1[1] = 1;
	imap1[2] = 2;
	imap2[3] = 3;
	imap2[4] = 4;
	imap1.swap(imap2);
	map<int, int>::iterator iter = imap1.begin();
	for (; iter != imap1.end(); ++iter)
		printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
	printf("\n");
	iter = imap2.begin();
	for (; iter != imap2.end(); ++iter)
		printf("iter->first: %d, iter->second: %d\n", iter->first, iter->second);
	getchar();
	return 0;
}

运行结果为:

iter->first: 3, iter->second: 3
iter->first: 4, iter->second: 4

iter->first: 1, iter->second: 1
iter->first: 2, iter->second: 2

分析:swap可以对调两个map中的内容,但是有一个前提,就是两个map必须使用相同的模板类实例,上面程序如果map<double, double> imap2编译会出错,swap返回值为void


map的初始化和赋值

	map<int, int> imap1;
	map<int, int> imap2(imap1);
	map<int, int> imap3(imap2.begin(), imap2.end());
	map<int, int> imap4 = imap1;
	map<int, int> imap5;
	imap5 = imap1;
	map<int, int> imap6({ pair<int, int>(1, 1), pair<int, int>(2, 2) }); //initializer_list方式初始化


排序:

#include <stdio.h>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <memory>
using namespace std;

bool Comp(const pair<string, int>& a, const pair<string, int>& b)
{
	return a.second < b.second;
}

int _tmain(int argc, _TCHAR* argv[])
{
	map<string, int> s_map;
	s_map["a"] = 19;
	s_map["b"] = 12;
	s_map["c"] = 10;
	//按key排序
	map<string, int>::iterator iter = s_map.begin();
	for (; iter != s_map.end(); ++iter)
		printf("%s: %d\n", iter->first.c_str(), iter->second);
	printf("\n");

	vector<pair<string, int>> s_vec(s_map.begin(), s_map.end());
	for (int i = 0; i < s_vec.size(); i++)
		printf("%s: %d\n", s_vec[i].first.c_str(), s_vec[i].second);
	printf("\n");
	//按值排序
	sort(s_vec.begin(), s_vec.end(), Comp);
	for (int i = 0; i < s_vec.size(); i++)
		printf("%s: %d\n", s_vec[i].first.c_str(), s_vec[i].second);
	printf("\n");
	getchar();
	return 0;
}

运行结果如下:

a: 19
b: 12
c: 10

a: 19
b: 12
c: 10

c: 10
b: 12
a: 19


你可能感兴趣的:(map容器的一些方法说明)