C++中 Map的了解与基本用法(代码演示+自我总结+map中一对多的用法)

C++中 map的了解与基本用法(代码演示)

一:map的基本认识

Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

二:map的基本操作函数(建议了解map常用的函数 以后用到啥 再学啥)

C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数

三:map常用函数的代码演示

1: map最基本的构造函数 map<关键字,键值> mapint
							 map<string , int >mapstring;         map<int ,string >mapint;
							  map<sring, char>mapstring;         map< char ,string>mapchar;
							  map<char ,int>mapchar;            map<int ,char >mapint;
2:插入数据
#include<bits/stdc++.h>
using namespace std;

int main()
{
	//构造方式 int 那个位置代表的是 关键字 而 string 那个位置代表的是 键值 
	map<int,string>m;
	
//	m[1]++;
//	m[2]++;
	// 插入数据 
	// 第一种方法:
	cout << "*****************************" << endl;
	cout << "第一种方法插入 !" << endl;
	m.insert(pair<int ,string>(1,"stu1"));
	m.insert(pair<int ,string>(2,"stu2"));
	m.insert(pair<int ,string>(3,"stu3"));
	
	map<int,string>::iterator t;
//	
	for( t = m.begin(); t != m.end(); t++ )
	{
		cout << t->first << ' ' << t->second << endl;//t->first表示关键字,t->second 表示键值	
	} 
	
	cout << "*****************************" << endl;
	cout << "第二种方法插入 !" << endl;
	m[1] = "stu1";
	m[2] = "stu2";
	m[3] = "stu3";
	
	
	for( t = m.begin(); t != m.end(); t++ )
	{
		cout << t->first << ' ' << t->second << endl;	
	} 
	 		
} 
3:查找数据
#include<bits/stdc++.h>
using namespace std;

int main()
{
	//构造方式 int 那个位置代表的是 关键字 而 string 那个位置代表的是 键值 
	map<int,string>m;
	
//	m[1]++;
//	m[2]++;
	// 插入数据 
	// 第一种方法:
	cout << "*****************************" << endl;
	cout << "第一种方法插入 !" << endl;
	m.insert(pair<int ,string>(1,"stu1"));
	m.insert(pair<int ,string>(2,"stu2"));
	m.insert(pair<int ,string>(3,"stu3"));
	
	map<int,string>::iterator t;

    cout << "*****************************" << endl;
	cout << "查找 !" << endl; 
	
	t = m.find(1);
	if(t != m.end())
	{
		cout << "查找成功!" << endl;	
    }
    else
    {
    	cout << "查找失败!" << endl; 
	}
		
} 
4:删除数据
#include<bits/stdc++.h>
using namespace std;

int main()
{
	//构造方式 int 那个位置代表的是 关键字 而 string 那个位置代表的是 键值 
	map<int,string>m;
	
//	m[1]++;
//	m[2]++;
	// 插入数据 
	// 第一种方法:
	cout << "*****************************" << endl;
	cout << "第一种方法插入 !" << endl;
	m.insert(pair<int ,string>(1,"stu1"));
	m.insert(pair<int ,string>(2,"stu2"));
	m.insert(pair<int ,string>(3,"stu3"));
	
	map<int,string>::iterator t;
	
	for( t = m.begin(); t != m.end(); t++ )
	{
		cout << t->first << ' ' << t->second << endl;	
	} 
		
	cout << "*****************************" << endl;
	cout << "删除 !" << endl; 
	
	t =  m.find(2);
	m.erase(t);
	
	for( t = m.begin(); t != m.end(); t++ )
	{
		cout << t->first << ' ' << t->second << endl;	
	} 	
} 
5:运行结果

在这里插入图片描述

四:map用法的补充

1.将map容器中(默认的递增顺序,改为递减的顺序(这里的顺序指的是关键值))

#include<bits/stdc++.h>
using namespace std;

int main(){
	std::map<int, int, std::greater<int> > m;  //map默认的递增 ,这样改成递减的 
//在创造map时,增加参数 **std::greater<int>**,就变成增加

	int a[5] = {5,4,3,2,1};
	
	map<int,int>::iterator t; 
	
	for(int i = 0; i < 5; i++){
		m[i] = a[i];
	}
	
	for(t = m.begin(); t != m.end(); t++){
		
		cout << ' ' << t->first << ' ' << t->second << endl; 
		
	} 

}

在这里插入图片描述

2:利用map容器实现一对多

  /**
	思路:我们想要的结果是  一对多  即一个人对应好几个课程号,
		  可以用到map<string,vector<int>>	
		  我还考虑了vector<int>v[2500],但是vector中不能表示成 一个字符串对应好几个数
		  所以选择了map 


*/
#include<bits/stdc++.h>
using namespace std;

int main(){

	int N,K;
	map<string,vector<int> >m; // 注意vector<int> 后面得加上空格 
	map<string,vector<int> >::iterator t;
	
	scanf("%d%d",&N,&K);
	
		
	for(int i = 1; i <= K; i++){
		
		int nums,a;
	//	cin >> i >> nums;
		scanf("%d%d",&a,&nums);//这里不要输入 i即便i是从1开始的
		
		for(int j = 0; j < nums; j++){
			
		//	string str;
		//	cin >> str;
			char ch[6];
			scanf("%s",ch);
		
		    m[ch].push_back(a);		
		
		}	
	}
	
	for(int i = 0; i < N; i++){
		
		char name[6];
		scanf("%s",name);
		
		printf("%s %d",name,m[name].size()); //m[name].size() 输出一个人选择了多少门课程 
		
		sort(m[name].begin(),m[name].end());
		
//		for(int temp : m[name]){
//			printf(" %d",temp);
//		}

        for(int j = 0; j < m[name].size(); j++){
			cout << ' ' << m[name][j]; 
		} 
		
		printf("\n");
		
	}
	
	
	 
}

3:利用map容器

m[num] 其默认值是为0;

  • 28
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

天天向上的菜鸡杰!!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值