1、map创建、元素插入和遍历访问
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, float> str;
//插入元素,按键值的由小到大放入红黑树中
str["Jack"] = 98.5;
str["Bomi"] = 96.0;
str["Kate"] = 97.5;
for(map<string, float>::iterator iter = str.begin(); iter != str.end(); iter++)
cout << (*iter).first << " : " << (*iter).second << endl;
return 0;
}
运行结果:Bomi : 96
Jack : 98.5
Kate : 97.5
2、删除元素和重复插入值:erase()和clear()方法
(1)与set容器一样,map映照容器的erase()删除元素函数,可以删除某个键值上的所有元素。也可以使用clear()方法清空map映照容器。
(2)对于重复插入的值,只取最后插入的值
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, char> str;
//插入元素,按键值的由小到大放入红黑树中
str[25] = 'm';
str[28] = 'k';
str[10] = 'x';
str[30] = 'a';
str[25] = 'i'; //重复插入的值,采用最后插入的值
str[10] = 'y'; //重复插入的值,采用最后插入的值
cout << "原始数据:" << endl;
for(map<int, char>::iterator iter = str.begin(); iter != str.end(); iter++)
cout << (*iter).first << " : " << (*iter).second << endl;
//删除键值为0的元素
int n = str.erase(0);
cout << "删除元素个数:" << n << endl;
//删除键值为25的元素
n = str.erase(25);
cout << "删除元素个数:" << n << endl;
cout << "erase()后数据:" << endl;
for(map<int, char>::iterator iter = str.begin(); iter != str.end(); iter++)
cout << (*iter).first << " : " << (*iter).second << endl;
str.clear();
cout << "clear()后数据:" << endl;
for(map<int, char>::iterator iter = str.begin(); iter != str.end(); iter++)
cout << (*iter).first << " : " << (*iter).second << endl;
return 0;
}
运行结果:原始数据:
10 : y
25 : i
28 : k
30 : a
删除元素个数:0
删除元素个数:1
erase()后数据:
10 : y
28 : k
30 : a
clear()后数据:
3、元素反向遍历:reverse_iterator
需要rbegin()方法和rend方法指出反向遍历的起始位置和终止位置。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, char> str;
//插入元素,按键值的由小到大放入红黑树中
str[25] = 'm';
str[28] = 'k';
str[10] = 'x';
str[30] = 'a';
for(map<int, char>::reverse_iterator iter = str.rbegin(); iter != str.rend(); iter++)
cout << (*iter).first << " : " << (*iter).second << endl;
return 0;
}
运行结果:30 : a
28 : k
25 : m
10 : y
4、元素的搜索:find()
与set一样,使用find()方法搜索某个键值,搜索到了返回键值所在迭代器位置。否则,返回end()迭代器位置。由于采用红黑树结构,搜索速度是极快的。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, char> str;
//插入元素,按键值的由小到大放入红黑树中
str[25] = 'm';
str[28] = 'k';
str[10] = 'x';
str[30] = 'a';
map<int, char>::iterator iter;
iter = str.find(28);
if(iter != str.end())
cout << (*iter).first << " : " << (*iter).second << endl;
else
cout << "not found it" << endl;
return 0;
}
运行结果:28 : k
5、自定义比较函数
默认情况下,按照键值由小到大的顺序插入元素。由于内部数据结构都是红黑树,因此编写比较函数与set是一致的。编写方法有两种,
(1)如果元素不是结构体,那么可以编写比较函数。下面实现键值由大道小的顺序将元素插入map中:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
struct myComp
{
bool operator()(const int& a, const int& b)
{
return a > b;
}
};
int main()
{
//插入元素,按键值的由大到小放入红黑树中
map<int, char, myComp> str;
str[25] = 'm';
str[28] = 'k';
str[10] = 'x';
str[30] = 'a';
for(map<int, char, myComp>::iterator iter = str.begin(); iter != str.end(); iter++)
cout << (*iter).first << " : " << (*iter).second << endl;
return 0;
}
运行结果:运行结果:
30 : a
28 : k
25 : m
10 : x
(2)如果元素是结构体,那么,可以直接把比较函数写在结构体里面。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
struct Info
{
string name;
float score;
//必须要写重载函数,重载"<"操作符,自定义排序规则
bool operator < (Info a) const
{
//按score由大到小排列。如果要由小到大排列,使用">"号即可;
return a.score < score;
}
};
int main()
{
//插入元素,按键值的由大到小放入红黑树中
map<Info, int> str;
Info info;
info.name = "Jack";
info.score = 60;
str[info] = 25;
info.name = "Bomi";
info.score = 80;
str[info] = 10;
info.name = "Peti";
info.score = 66.5;
str[info] = 30;
for(map<Info, int>::iterator iter = str.begin(); iter != str.end(); iter++)
{
cout << (*iter).second << " : ";
cout << ((*iter).first).name << " " << ((*iter).first).score << endl;
}
return 0;
}
运行结果:10 : Bomi 80
30 : Peti 66.5
25 : Jack 60
6、字符映照数字的map写法
对数字的各位进行分离,采用取余数的数学操作是很耗时的。而把数字当成字符串,使用map的映照功能,很方便地实现了数字分离。
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
//插入元素,按键值的由大到小放入红黑树中
map<char, int> str;
str['0'] = 0;
str['1'] = 1;
str['2'] = 2;
str['3'] = 3;
str['4'] = 4;
str['5'] = 5;
str['6'] = 6;
str['7'] = 7;
str['8'] = 8;
str['9'] = 9;
/*
//上面赋值语句可以用循环代码简化
for(int j = 0; j < 10; j++)
m['0' + j] = j;
*/
string a;
a = "6234";
int sum = 0;
for(int i = 0; i < a.length(); i++)
sum += str[a[i]];
cout << "各个数字的和为: " << sum << endl;
sum = str[a[0]];
for(int i = 1; i < a.length(); i++)
{
sum *= 10;
sum += str[a[i]];
}
cout << "字符串转化为数字: " << sum << endl;
}
运行结果:各个数字的和为:15
字符串转化为数字:6234
7、数字映照字符的map写法
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
//插入元素,按键值的由大到小放入红黑树中
map<int, char> str;
str[0] = '0';
str[1] = '1';
str[2] = '2';
str[3] = '3';
str[4] = '4';
str[5] = '5';
str[6] = '6';
str[7] = '7';
str[8] = '8';
str[9] = '9';
/*
//上面赋值语句可以用循环代码简化
for(int j = 0; j < 10; j++)
m[j] = '0' + j;
*/
int n = 7;
string single;
cout << "单位数字转化为字符串: " << single + str[n] << endl;
int num = 6234;
string s;
for(int i = num; i != 0; i /= 10)
{
s.insert(s.begin(), str[i % 10]);
}
cout << "多位数字转化为字符串: " << s << endl;
return 0;
}
运行结果:单位数字转化为字符串:7
多位数字转化为字符串:6234