1.Map是c++的一个标准容器,它提供了很好一对一的关系。ios
Map是一种关联是容器,在map中增长和删除元素很是容易。能够修改一个特定的节点而不对其余节点不产生影响,因为map是一种关联式容器,Key--value结构咱们必须明确,在map中咱们能够根据key的值修改其对应的value的值,而不能修改key的值,其最大的特色是key和value的值能够是任意类型(内部类型你或用户本身定义的类型)。c++
2. Map的特色:api
(1)快速创建key--value的索引。key和value能够是任意类型。app
(2)map的查询时间复杂度为LogN,也就是若是包含1000个记录,查询次数最多为10次,1,000,000个记录,最多查找20次。函数
(3)快速插入Key - Value 记录。性能
(4)快速删除记录spa
(5)根据Key 修改value记录。指针
(6)遍历全部记录。对象
三、使用map排序
使用map得包含map类所在的头文件
#include <map>
map对象是模板类,须要关键字和存储对象两个模板参数:std:map<int, string> personnel;
这样就定义了一个用int做为索引,并拥有相关联的指向string的指针.
为了使用方便,能够对模板类进行一下类型定义,
typedef map<int, CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;
四、在map中插入元素
改变map中的条目很是简单,由于map类已经对[]操做符进行了重载
enumMap[1] = "One";
enumMap[2] = "Two";
.....
这样很是直观,但存在一个性能的问题。插入2时,先在enumMap中查找主键为2的项,没发现,而后将一个新的对象插入enumMap,键是2,值是一个空字符串,插入完成后,将字符串赋为"Two"; 该方法会将每一个值都赋为缺省值,而后再赋为显示的值,若是元素是类对象,则开销比较大。咱们能够用如下方法来避免开销:
enumMap.insert(map<int, CString> :: value_type(2, "Two"))
五、查找并获取map中的元素
下标操做符给出了得到一个值的最简单方法:
CString tmp = enumMap[2];
可是,只有当map中有这个键的实例时才对,不然会自动插入一个实例,值为初始化值。
咱们可使用Find()和Count()方法来发现一个键是否存在。
查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里须要提到的是begin()和end()两个成员,分别表明map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.
int nFindKey = 2; //要查找的Key
//定义一个条目变量(实际是指针)
UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey);
if(it == enumMap.end())
{
//没找到
}
else {
//找到
}
经过map对象的方法获取的iterator数据类型是一个std::pair对象,包括两个数据 iterator->first 和 iterator->second 分别表明关键字和存储的数据
六、从map中删除元素
移除某个map中某个条目用erase()
该成员方法的定义以下
clear()就至关于 enumMap.erase(enumMap.begin(), enumMap.end());
总结了一些map基本简单实用的操做!
1. 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;
2. map添加数据;
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中最简单最经常使用的插入添加!
3,map中元素的查找:
find()函数返回一个迭代器指向键值为key的元素,若是没找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else cout<<"wo find 112"<<endl;
4,map中元素的删除:
若是删除112;
map<int ,string >::iterator l_it;;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else maplive.erase(l_it); //delete 112;
5,map中 swap的用法:
Map中的swap不是一个容器中的元素交换,而是两个容器交换;
For example:
#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 ) );
m3.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;
// This is the member function version of swap
//m2 is said to be the argument map; m1 the target map
m1.swap( 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
swap( m1, m3 );
cout << "After swapping with m3, map m1 is:";
for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
cout << " " << m1_Iter -> second;
cout << "." << endl;
}
6.map的sort问题:
Map中的元素是自动按key升序排序,因此不能对map用sort函数:
For example:
#include <map>
#include <iostream>
using namespace 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 m1 is:"<<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
请按任意键继续. . .
7, 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的函数
声明:仅摘录,方便查找。