您的位置:首页 > 编程语言 > C语言/C++

[C++ 从入门到放弃-05]C++STL之map映照容器

2017-07-03 14:16 459 查看
map映照容器的元素数据包含两个部分:

一个键值;

一个映照的数据;

组成的,键值和映照数据之间具有一一对应的关系。

map映照容器的数据结构也是采用红黑树来实现的。

注意:不允许插入元素的键值相同。比较函数所比较的也只是键值之间的比较,在检索的时候,通俗的说查找的时候,还是通过键值进行查找。

map的用法和set及其相似,如果你之前的都看了,相信你可以猜到map有哪些方法以及操作了。

1. map创建、元素插入和遍历访问

创建map对象,键值和映照数据的类型自己来定义。在没有指定比较函数的时候,元素的插入位置是按键值的由小到大的顺序进行插入的。

#include<iostream>
#include<map>
#include<string>

using namespace std;

int main()
{
//定义map对象,当前没有任何元素
map<string, float> m;
m["CaiCai"] = 98.5;
m["Ningbo"] = 96.0;
m["Zhejiang"] = 97.5;

map<string, float>::iterator it;
for(it = m.begin(); it != m.end(); it ++)
cout<<(*it).first<<" : "<<(*it).second<<endl;

return 0;
}

2. 删除元素

跟set类似,map映照容器也是通过erase()函数进行元素的删除的。clear()函数也可以使用。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, char> m;

//插入元素,按键值由小到大插入到红黑树中
m[25] = 'm';
m[28] = 'k';
m[10] = 'x';
m[30] = 'a';

//删除键值为28的
m.erase(28);

map<int, char>::iterator it;
for(it = m.begin(); it != m.end(); it ++)
cout<<(*it).first<<" : "<<(*it).second<<endl;

return 0;
}

3. 元素的反向遍历、元素的搜索

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, char> m;

m[25] = 'm';
m[28] = 'k';
m[10] = 'x';
m[30] = 'a';

//反向遍历
map<int, char>::reverse_iterator rit;
for(rit = m.rbegin(); rit != m.rend(); rit ++)
cout<<(*rit).first<<" : "<<(*rit).second<<endl;

//查找键值为28的元素
map<int, char>::iterator it;
it = m.find(28);
if(it != m.end())
cout<<(*it).first<<" : "<<(*it).second<<endl;
else
cout<<"Not find it!"<<endl;

return 0;

}

4. 自定义比较函数

将元素插入到map中的时候,map会根据指定的比较函数将元素插入到该放的位置上,如果没有指定比较函数,就采用默认的比较函数,按键值从小到大的规则插入。

4.1 元素不是结构体

#include<iostream>
#include<map>
#include<string>
using namespace std;

//自定义比较函数mycmp
struct mycmp
{
bool operator () (const int& a, const int& b)
{
if(a != b)
return a > b;
else
return a > b;
}
};

int main()
{
map<int, char, mycmp> m;

m[25] = 'm';
m[28] = 'k';
m[10] = 'x';
m[30] = 'a';

map<int, char, mycmp>::iterator it;
for(it = m.begin(); it != m.end(); it ++)
cout<<(*it).first<<" : "<<(*it).second<<endl;

return 0;
}

4.2 元素是结构体

将比较函数直接写在结构体中。

#include<iostream>
#include<map>
#include<string>
using namespace std;

struct Info
{
string name;
float score;

//按score由大到小的顺序排列,
bool operator < (const Info& a) const
{
return a.score < score;
}
};

int main()
{
map<Info, int> m;
Info info;

info.name = "CaiCai";
info.score = 60;
m[info] = 25;

info.name = "Ningbo";
info.score = 80;
m[info] = 10;

info.name = "Zhejiang";
info.score = 67;
m[info] = 30;

map<Info, int>::iterator it;
for(it = m.begin(); it != m.end(); it ++)
{
cout<<(*it).second<<" : ";
cout<<((*it).first).name<<" "<<((*it).first).score<<endl;
}

return 0;
}

5. map实现数字分离

对数字各位进行分离,把数字当做字符串进行处理,使用map的映照功能,很便捷的对数字进行分离。
#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<char, int> m;
m['0'] = 0;
m['1'] = 1;
m['2'] = 2;
m['3'] = 3;
m['4'] = 4;
m['5'] = 5;
m['6'] = 6;
m['7'] = 7;
m['8'] = 8;
m['9'] = 9;

for(int i = 0; i < 10; i ++)
m['0'+i] = i;

string sa, sb;
sa = "2015";
int sum = 0;

for(int i = 0; i < sa.length(); i ++)
sum += m[sa[i]];

cout<<"sum = "<<sum<<endl;

return 0;
}

6. map实现数字映照字符

很多题目需要我们将数字Int转换成字符或者其他形式字符串表达,使用map会很方便。

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
map<int, char> m;
m[0] = '0';
m[1] = '1';
m[2] = '2';
m[3] = '3';
m[4] = '4';
m[5] = '5';
m[6] = '6';
m[7] = '7';
m[8] = '8';
m[9] = '9';

for(int i = 0; i < 10; i ++)
m['0'+i] = i;

int n = 7;
string s = "The number is: ";
cout<<s+m
<<endl;

return 0;
}
OK,掌握这6点基本上就足够使用了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: