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)直接覆盖
详情见代码 :
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这个函数
关于查找,还有一个地方需要注意:
如果是直接map[]的查找方式,如果没有会自动创建一个,就是会自动生成一个。
如果是用find就不会了。
还有关于这个[],其实有的时候是很方便,但是如果没用好的话,真的是”害人不浅”。就比如下面,访问了maps[10000]之后,会自动insert一对键值。
在使用的时候应当加倍小心!
map的swap:
关于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 }
相关文章推荐
- c++ new 运算符做了什么
- LeapMotion SDK 开发之一 编写第一个LeapMotion VC++ 程序
- C++ 11 Lambda表达式
- c++ 编程规范
- C++ list的基本操作和使用
- Mother's Milk
- C++的四舍五入函数
- 编译VC++6.0时出出现unexpected end of file while looking for precompiled header directived解决方法
- 静态链表(C++实现)
- C++primer plus 第九章习题
- C++中substr函数的用法
- 1004. 成绩排名 (20)
- 极客班GeekBand C++第一周笔记
- 关于在C/C++语言中,函数如何返回数组,数组如何作为参数传递以及返回数组的函数该如何调用问题的总结
- c++语法——类和对象的学习(1)
- LevenbergMarquardt 算法 eigen实现(c++)
- POJ 1159 Palindrome
- C++虚函数与JAVA中抽象函数比较 接口与抽象类比较
- 【C++】:C++的境界1
- c++中箭头操作符(->)和点操作符(.)的不同