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

C++—map的基本操作总结

2017-10-09 16:18 357 查看
原文地址:http://blog.csdn.net/google19890102/article/details/51720305
http://blog.csdn.net/u012577123/article/details/46821581
标准库map类型是一种以键-值(key-value)存储的数据类型。以下分别从以下的几个方面总结:
map对象的定义和初始化
map对象的基本操作,主要包括添加元素,遍历等


1、pair类型


1.1、pair类型的定义和初始化

pair类型是在有文件utility中定义的,pair类型包含了两个数据值,通常有以下的一些定义和初始化的一些方法:
pair<T1, T2> p;

pair<T1, T2> p(v1, v2);

make_pair(v1, v2)

上述第一种方法是定义了一个空的pair对象p,第二种方法是定义了包含初始值为v1和v2的pair对象p。第三种方法是以v1和v2值创建的一个新的pair对象。


1.2、pair对象的一些操作

除此之外,pair对象还有一些方法,如取出pair对象中的每一个成员的值:
p.first

p.second

例如:
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;

int main(){
pair<int, string> p1(0, "Hello");
printf("%d, %s\n", p1.first, p1.second.c_str());
pair<int, string> p2 = make_pair(1, "World");
printf("%d, %s\n", p2.first, p2.second.c_str());
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14


2、map对象的定义和初始化

map是键-值对的组合,有以下的一些定义的方法:
map<k, v> m;

map<k, v> m(m2);

map<k, v> m(b, e);

上述第一种方法定义了一个名为m的空的map对象;第二种方法创建了m2的副本m;第三种方法创建了map对象m,并且存储迭代器b和e范围内的所有元素的副本。
map的value_type是存储元素的键以及值的pair类型,键为const。
map<string ,int>mapstring;
map<int,string >mapint;
map<sring,char>mapstring;
map< char ,string>mapchar;
map<char,int>mapchar;
map<int ,char>mapint;
//这几个是最基本的,当然还有其它的构造函数的


3、map对象的一些基本操作


3.1、map中元素的插入

在map中元素有两种插入方法:
使用下标
使用insert函数

在map中使用下标访问不存在的元素将导致在map容器中添加一个新的元素。

insert函数的插入方法主要有如下:
m.insert(e)

m.insert(beg, end)

m.insert(iter, e)

map<int ,string>maplive;
1. maplive.insert(pair<int,string>(102,"aclive"));
2. maplive.insert(map<int,string>::value_type(321,"hai"));
3. maplive[112]="April";//map中最简单最常用的插入添加!
上述的e一个value_type类型的值。beg和end标记的是迭代器的开始和结束。

两种插入方法如下面的例子所示:
#include <stdio.h>
#include <map>
using namespace std;

int main(){
map<int, int> mp;
for (int i = 0; i < 10; i ++){
mp[i] = i;
}
for (int i = 10; i < 20; i++){
mp.insert(make_pair(i, i));
}
map<int, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++){
printf("%d-->%d\n", it->first, it->second);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


3.2、map中元素的查找和读取

注意:上述采用下标的方法读取map中元素时,若map中不存在该元素,则会在map中插入。
因此,若只是查找该元素是否存在,可以使用函数
count(k)
,该函数返回的是k出现的次数;若是想取得key对应的值,可以使用函数
find(k)
,该函数返回的是指向该元素的迭代器。
上述的两个函数的使用如下所示:
#include <stdio.h>
#include <map>
using namespace std;

int main(){
map<int, int> mp;
for (int i = 0; i < 20; i++){
mp.insert(make_pair(i, i));
}

if (mp.count(0)){
printf("yes!\n");
}else{
printf("no!\n");
}

map<int, int>::iterator it_find;
it_find = mp.find(0);
if (it_find != mp.end()){
it_find->second = 20;
}else{
printf("no!\n");
}

map<int, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++){
printf("%d->%d\n", it->first, it->second);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


3.3、从map中删除元素

从map中删除元素的函数是
erase()
,该函数有如下的三种形式:
m.erase(k)

m.erase(p)

m.erase(b, e)

第一种方法删除的是m中键为k的元素,返回的是删除的元素的个数;第二种方法删除的是迭代器p指向的元素,返回的是void;第三种方法删除的是迭代器b和迭代器e范围内的元素,返回void。
如下所示:
#include <stdio.h>
#include <map>
using namespace std;

int main(){
map<int, int> mp;
for (int i = 0; i < 20; i++){
mp.insert(make_pair(i, i));
}

mp.erase(0);

mp.erase(mp.begin());

map<int, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++){
printf("%d->%d\n", it->first, it->second);
}

return 0;
}
map<int ,string>::iterator l_it;;
l_it =maplive.find(112);
if( l_it == maplive.end())
cout<<"we do not find112"<<endl;
else
maplive.erase(l_it);//delete 112;

#include<map>
#include<iostream>

usingnamespace std;

int main( )
{
map<int, int> m1;
map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );
m1.insert ( pair<int, int> ( 4, 40) );
m1.insert ( pair<int, int> ( 3, 60) );
m1.insert ( pair<int, int> ( 2, 50) );
m1.insert ( pair<int, int> ( 6, 40) );
m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;
for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )
cout << m1_Iter->first<<""<<m1_Iter->second<<endl;

}

The original map m1 is:
1 20
2 50
3 60
4 40
6 40
7 30


4、map的基本操作函数

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的函数

4.1multimap

//multimap允许重复的键值插入容器
//        **********************************************************
//        * pair只包含一对数值:pair<int,char>                       *
//        * map是一个集合类型,永远保持排好序的,                   *
//  pair  * map每一个成员就是一个pair,例如:map<int,char>           *
//        * map的insert()可以把一个pair对象作为map的参数,例如map<p> *
//        ***********************************************************
#pragma warning(disable:4786)
#include<map>
#include<iostream>
using namespace std;

int main(void)
{
multimap<int,char*> m;
//multimap的插入只能用insert()不能用数组
m.insert(pair<int,char*>(1,"apple"));
m.insert(pair<int,char*>(1,"pear"));//apple和pear的价钱完全有可能是一样的
m.insert(pair<int,char*>(2,"banana"));
//multimap的遍历只能用迭代器方式不能用数组
cout<<"***************************************"<<endl;
multimap<int,char*>::iterator i,iend;
iend=m.end();
for(i=m.begin();i!=iend;i++)
{
cout<<(*i).second<<"的价钱是"
<<(*i).first<<"元/斤\n";
}
cout<<"***************************************"<<endl;
//元素的反相遍历
multimap<int,char*>::reverse_iterator j,jend;
jend=m.rend();
for(j=m.rbegin();j!=jend;j++)
{
cout<<(*j).second<<"的价钱是"
<<(*j).first<<"元/斤\n";
}
cout<<"***************************************"<<endl;
//元素的搜索find(),pair<iterator,iterator>equal_range(const key_type &k)const
//和multiset的用法一样
multimap<int,char*>::iterator s;
s=m.find(1);//find()只要找到一个就行了,然后立即返回。
cout<<(*s).second<<"    "
<<(*s).first<<endl;
cout<<"键值等于1的元素个数是:"<<m.count(1)<<endl;
cout<<"***************************************"<<endl;
//删除 erase(),clear()
m.erase(1);
for(i=m.begin();i!=iend;i++)
{
cout<<(*i).second<<"的价钱是"
<<(*i).first<<"元/斤\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: