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

C++ map详解

2016-05-13 20:43 295 查看
map的内部是一颗红黑树。

关于map的效率,最初的时候我认为只是方便,map的find是O(n)级别的,

因为以前我曾经被vector里面的find坑过一次,所以一直耿耿于怀…

map里面既然是红黑树在撑腰,那我也不敢造次了,我错了!map。

map 的find是log级别的,而且是稳定的log查询,不会像有的平衡二叉树一样,退化成链表了.

1、map的主键只能有一个。

这里就有一个重要的地方,假如你还是添加了怎么办?

这是就会有两种情况:

(1)无法insert

(2)直接覆盖

详情见代码 :

#include<stdio.h>
#include<map>
#include<iostream>
#include<string>
using namespace std;

typedef map<int,int>::iterator it;
typedef map<int,int> map1;

int main(){
map1 maps;
maps.clear();
maps.insert(make_pair(1,5));
maps.insert(make_pair(4,5));
maps.insert(make_pair(5,5));
maps.insert(make_pair(2,5));
maps.insert(make_pair(9,5));

maps.insert(make_pair(2,7));//(1)无法insert
maps[2]=9;//(2)直接覆盖
it its=maps.find(2);
if(its!=maps.end()){
cout<<its->second<<endl;
}else{
puts("Not Found!");
}
return 0;
}


2、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

这里的话,不光上面6种,虽然网上说是这6种,可我觉得还有很多都是可以的。

比如map< int,int> mapint;也是可以的。

这里还有一个提醒的,就是说map是可以嵌套map的,比如hdu1263这道题,就是map二维的经典运用。

3、map的count函数,这里的count返回值只有0和1两个,所以与其用count,不如使用find这个函数

it its=maps.find(Key_Value);
if(its!=maps.end()){
cout<<its->second<<endl;
}else{
puts("Not Found!");
}


关于查找,还有一个地方需要注意:

如果是直接map[]的查找方式,如果没有会自动创建一个,就是会自动生成一个。

如果是用find就不会了。

#include<stdio.h>
#include<map>
#include<iostream>
#include<string>
using namespace std;

typedef map<int,int>::iterator it;
typedef map<int,int> map1;

int main(){
map1 maps;
maps.clear();
maps.insert(make_pair(1,5));
maps.insert(make_pair(4,5));
maps.insert(make_pair(5,5));
maps.insert(make_pair(2,5));
maps.insert(make_pair(9,5));
maps.insert(make_pair(99,5));
maps.insert(make_pair(129,5));
maps.insert(make_pair(6,5));
maps.insert(make_pair(7,5));

it its=maps.find(17);
if(its!=maps.end()){
cout<<its->second<<endl;
}else{
puts("Not Found!");
}

int second_Value=maps[17];
cout<<second_Value<<endl;
return 0;
}


还有关于这个[],其实有的时候是很方便,但是如果没用好的话,真的是”害人不浅”。就比如下面,访问了maps[10000]之后,会自动insert一对键值。

在使用的时候应当加倍小心!

#include<stdio.h>
#include<map>
#include<iostream>
#include<string>
using namespace std;

typedef map<int,int>::iterator it;
typedef map<int,int> map1;

int main(){
map1 maps;
maps.clear();
maps.insert(make_pair(1,5));
maps.insert(make_pair(4,5));
maps.insert(make_pair(5,5));
maps.insert(make_pair(2,5));
maps.insert(make_pair(9,5));
maps.insert(make_pair(99,5));
maps.insert(make_pair(129,5));
maps.insert(make_pair(6,5));
maps.insert(make_pair(7,5));

it its=maps.find(17);
if(its!=maps.end()){
cout<<its->second<<endl;
}else{
puts("Not Found!");
}

int second_Value=maps[17];
cout<<second_Value<<endl;
its=maps.find(10000);
if(its!=maps.end()){
cout<<its->second<<endl;
}else{
puts("Not Found!");
}
cout<<maps[10000]<<endl;
its=maps.find(10000);
if(its!=maps.end()){
cout<<its->second<<endl;
}else{
puts("Not Found!");
}
return 0;
}


map的swap:

#include <map>
#include <iostream>

using namespace 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 ) );
m2.insert ( pair <int, int> ( 30, 300 ) );

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

cout << "The original map m2 is:";
for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
cout << " " << m1_Iter->second;
cout << "." << endl;
// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map

//  m1.swap( m2 );
//swap(m1,m2);

cout << "After swapping 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, map m2 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 swap
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: