C++ map的基本操作和使用

来源:互联网 发布:js获取json的key 编辑:程序博客网 时间:2024/05/18 00:18

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。


map能自动建立Key - value的对应。key 和 value可以是任意你需要的类型。

map中的元素是自动按key升序排序,所以不用对map进行排序,也无法排序(如果要按降序输出,可以用map.sizeof( ) 获得map的长度)。

根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。

头文件:  #include <map>


map最基本的构造函数;

map(A,B)name;A和B都是类型名 A是输入的变量类型,B是A要映射的类型;

在输出时,A用first指向,B用second指向。

比如:

#include<iostream>#include<map>#include<string>using namespace std;int main(){map<string,int> mp;mp.insert(pair<string,int>("jieguo",3));map<string,int>::iterator it; //迭代在后面会讲到it=mp.begin(); cout<< it->first<<endl<< it->second <<endl;return 0;

例举几种map的定义:

   map<string ,int>mapstring;        map<int ,string >mapint;
   map<sring,char>mapstring;        map< char ,string>mapchar;
   map<char,int>mapchar;           map<int ,char >mapint;


map插入数据:

  1.maplive.insert(pair<int,string>(102,"aclive"));

  2.maplive[112]="April";//map中最简单最常用的插入添加!


map中元素的查找与删除:

如果只是要判断map中是否有A元素,要用到迭代器:

map<string,int>::iterator it;  //定义迭代器

#include<stdio.h>#include<iostream>#include<map>#include<string>using namespace std;int main(){map<string,int> mp;mp.insert(pair<string,int>("jieguo",3));mp.insert(pair<string,int>("qiyin",4));map<string,int>::iterator it;//定义迭代器 it=mp.find("qiyin"); //查找"qiyin"这个元素 if(it!=mp.end()) mp.erase(it);//删除操作 else printf("it doesn't exist\n");for(it=mp.begin();it!=mp.end();it++)cout<<it->first<<endl;return 0;}

map中两个元素的交换(实际上两个map容器交换,key值和value都交换了)

1.  map.swap(s1,s2);

2.  s1.swap(s2);

举个网上的例子

#include<map>#include<iostream>usingnamespace std;int main(){map <int, int> m1, m2, m3;map <int,int>::iterator m1_Iter;m1.insert( pair <int, int>(1, 10 ) );m1.insert ( pair <int,int> ( 2, 20 ) );m1.insert ( pair <int,int> ( 3, 30 ) );m2.insert ( pair <int,int> ( 10, 100 ) );m2.insert ( pair <int,int> ( 20, 200 ) );m3.insert ( pair <int,int> ( 30, 300 ) );cout << "The original map m1is:";for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )cout << " "<<m1_Iter->second;cout << "."<< endl;// This isthe member function version of swap// m2 is said to be theargument map; m1 the target mapm1.swap( m2);cout << "Afterswapping with m2, map m1 is:";for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )cout << " "<< m1_Iter ->second;cout << "."<< endl;cout << "After swapping with m2, mapm2 is:";for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )cout << " "<< m1_Iter ->second;cout << "."<< endl;// This is the specialized template version of swapswap( m1, m3 );cout << "Afterswapping with m3, map m1 is:";for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )cout << " "<< m1_Iter ->second;cout << "."<< endl;}
可以看出,这个例子中,不仅仅是key值(first)进行了互换,第二个(second)也互换了。


map的sort函数:

#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 202 503 604 406 407 30

map的基本操作函数:
C++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的函数


0 0
原创粉丝点击