更新时间:2015-10-06 20:33:20浏览次数:1+次
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;
}
运行结果:#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;
}
运行结果:#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;
}
运行结果:#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;
}
运行结果:#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;
}
运行结果:#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;
}
运行结果:#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;
}
运行结果:#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;
}
运行结果:
相关资讯