C++ STL Map的创建、删除、插入、更新、遍历

C++map容器提供一个键值对容器,mapmultimap差别仅仅在于multiple允许一个键对应多个值。   

map的实现是一颗红黑树,因此,map的内部键的数据都是排好序的,查找和删除、插入的效率都是lgN

map的本质其实就是映射,键值(key-value)一一对应。比如身份证号(key)和姓名(value)一一对应,map的定义格式:

 

std::map 变量;

 

比如人的身份证号和姓名,可以这样定义映射  std::map mapPeople

 

(key)和值(value)可以是任意类型,对key的类型要求是必须支持<操作符。

 


  1   头文件 
  #include    

不能加扩展名.h

 

 2   定义 
  std::map 变量;

 

 std::map PeopleMap;

 

  或者是typedef     std::map mapPeopleType; 
mapPeopleType   PeopleMap; 
  
  3   插入数据 
  (1)   map的变量名[key] = value;

PeopleMap[111]   =   string(“zhang san”); //常用的
  (2)   PeopleMap.insert(map::value_type(111, “zhang wu” ));

可以根据insert的返回值判断释放插入成功

The single element versions (1) return a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the element with an equivalent key in the map. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent key already existed. 

 

也就是说insert返回的是一个pair,这个pair的成员first等于key,而second成员,成功将被设置为true,失败设置为false。
  (3)    PeopleMap.insert(pair(222 ,"zhang liu"));  //常用的
  (4)   PeopleMap.insert(make_pair(222 ,"zhang liu")); //常用的

insert方法和数组方法的区别

Insert方法不能覆盖,如果键已经存在,则插入失败

数组方法,就算键已经存在,使用数组方法,将会直接更新键对应的值。  


  4   查找数据和修改数据 
  (1)   string  value   =    PeopleMap[333]; 
           PeopleMap[333]  =   string(“lilei”); 
  (2)   mapPeopleType  ::iterator  Itr; 
           Itr = PeopleMap.find("b");  //find(key)
            value  =   my_Itr->second; 
           Itr->second   =   new value; 
  不过注意,键本身是不能被修改的,除非删除。 
  
  5   删除数据 
  (1)   PeopleMap.erase(my_Itr); 
  (2)   PeopleMap.erase(key); 
  还是注意,第一种情况在迭代期间是不能被删除的 
  
  6   迭代数据 、遍历
  for   (Itr=PeopleMap.begin();  Itr!=PeopleMap.end();   ++Itr)   {} 
  

7.排序

map的排序默认是根据key从小到大排序,需要注意的是:

(1)按照key从大到小排序

(2)key是一个结构体的时候

(3)想按value排序的时候

 

mapSTL里面的一个模板类,来看下map的定义

 

template < class Key,                                     // map::key_type

           class T,                                       // map::mapped_type

           class Compare = less,                     // map::key_compare

           class Alloc = allocator >    // map::allocator_type

           > class map;

 

他的四个参数,第一个是key,第二个是value,这个大家都知道。

第三个是跟排序有关,第四个设置存储分配模型的

 

现在我们来重点看第三个跟排序有关的:

  class Compare = less

 

这是一个class类型,默认是lesslessSTL里面的一个函数对象

 

 

8  反向迭代器

mapPeopleType  ::reverse_iterator  Itr; 
Itr =   PeopleMap.rbegin();

While(Itr != PeopleMap.rend())

{

 

Itr++;

}


  9   其它方法 
  PeopleMap.size()               返回元素数目 
  PeopleMap.empty()       判断是否为空 
  PeopleMap.clear()           清空所有元素 

  可以直接进行赋值和比较:=,   >,   >=,   <,   <=,   !=   等等 



#include 
#include 
#include 

typedef std::map mapPeopleType;
void for_each(mapPeopleType &PeopleMap)
{
	mapPeopleType::iterator  itr = PeopleMap.begin();

	std::cout<< "for_each "<first << " second "<second<first << " second "<second<insert_pair;
	
	insert_pair = PeopleMap.insert(std::make_pair(222 ,"zhang liu"));//插入已存在的键值将会失败
	//
	if(insert_pair.second == false)
	{
		std::cout<< " first: "<< insert_pair.first->first << ", second: "<<"false"<first << " second "<second<first << " second "<second<(222 ,"zhang liu"));

	//遍历
	for_each(PeopleMap);
	//反向遍历
	reverse_for_each(PeopleMap);

	//添加数据
	add(PeopleMap);
	
	for_each(PeopleMap);

	reverse_for_each(PeopleMap);

	//查找
	my_find(PeopleMap);

	//删除

	del(PeopleMap);
	for_each(PeopleMap);

	//其他
	std::cout<< " size: "<< PeopleMap.size() << ", empty: "<


root@ubuntu:/share# g++ map.cpp -g
root@ubuntu:/share# ./a.out       
for_each 
 first 222 second zhang liu
 first 333 second zhangsan
reverse_for_each 
 first 333 second zhangsan
 first 222 second zhang liu
 add 
 first: 222, second: false
for_each 
 first 222 second zhang liu
 first 333 second zhangsan_ver2
 first 444 second zhang444new
reverse_for_each 
 first 444 second zhang444new
 first 333 second zhangsan_ver2
 first 222 second zhang liu
 find :
 first 222 second zhang liu
not found 22
 444 :
 444 delete success
for_each 
 first 222 second zhang liu
 first 333 second zhangsan_ver2
 size: 2, empty: 0
clear after size: 0, empty: 1
root@ubuntu:/share# 





你可能感兴趣的:(C/C++,C++,STL,面试题)