map用法小注

之前看《算法笔记》并没真正掌握stl的用法,map在处理一些ccf的题时很有用,小小记录下:

1、字符为主键,不同字符对应相同的数字

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

map<char,int>canshuji;    //不同的char对应相同的数字
map<char,int>chuli; 

int main(){
    
    
	string str;
	cin>>str;
	for(int i = 0;i <str.size();i++){
    
    
		canshuji[str[i]] = 1;
	}	
	for(map<char,int>::iterator it = canshuji.begin();it != canshuji.end();it++){
    
    
		printf("%c %d\n",it->first,it->second);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104387867
今日推荐