map summarizes the use and operation of C ++

1, map Introduction

map is an associative container STL, which provides one of the hash.

  • The first can be called a key (key), each keyword may only appear once in the map;
  • The second value might be called the keyword (value);

2, the container iterator map (first and second)

map corresponding to the iterator first key value, the second value corresponding to a value. E.g

map<int, string> m_map;
m_map[1] = “hello”;

map<int,string>::iterator iter = m_map.begin();
iter->first; // 这个是 int 值是 1
iter->second; //这个是 string 值是 "hello"

3, map insert

// 定义一个map对象
map<int, string> mapStudent;
 
// 第一种 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
 
// 第二种 用insert函数插入value_type数据
mapStudent.insert(map<int, string>::value_type(001, "student_one"));
 
// 第三种 用"array"方式插入
mapStudent[123] = "student_first";
mapStudent[456] = "student_second";

These three usage, although the data are inserted may be implemented, but they are different, of course, the first and second effect is done on the same, with the insert data insert function involves the insertion of data in Uniqueness of the concept set, i.e., when there is the keyword in the map, insert operations are not inserted in the data, but by way of the array is different, it can override the value corresponding to the keyword before, program as follows:

mapStudent.insert(map<int, string>::value_type (001, "student_one"));
 
mapStudent.insert(map<int, string>::value_type (001, "student_two"));

After the execution of these two statements above, the keyword value corresponding to the map 001 is "student_one", and the second statement is not in force, then it comes to how we know whether the insert statement insert successful issue, you can use pair to get plugged into successful, the program are as follows

// 构造定义,返回一个pair对象
pair<iterator,bool> insert (const value_type& val);
 
pair<map<int, string>::iterator, bool> Insert_Pair;
 
Insert_Pair = mapStudent.insert(map<int, string>::value_type (001, "student_one"));
 
if(!Insert_Pair.second)
    cout << ""Error insert new element" << endl;

We came to know through the second pair of variables is inserted success, its first variable returns a map iterator, if you insert successful Insert_Pair.second should be true, and otherwise false.

4, map query and iterate

When the critical key search term appears, it returns the position where the object's data, if not, the function returns the value iter the end of the same.

// find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
iter = mapStudent.find("123");
 
if(iter != mapStudent.end())
       cout<<"Find, the value is"<<iter->second<<endl;
else
   cout<<"Do not Find"<<endl;

5, map elements and delete empty

//迭代器刪除
iter = mapStudent.find("123");
mapStudent.erase(iter);
 
//用关键字刪除
int n = mapStudent.erase("123"); //如果刪除了會返回1,否則返回0
 
//用迭代器范围刪除 : 把整个map清空
mapStudent.erase(mapStudent.begin(), mapStudent.end());
//等同于mapStudent.clear()

6, pay attention to the matter map

A. in the map, when looking for the key value, we must first determine whether the map contains the key.

B. If not checked, direct return map [key], unexpected behavior may occur. If the key map contains, there is no problem, if the key does not contain a map, the subscript a dangerous side effect, inserts a key element in the map, value the default value, return value. In other words, map [key] can not return null.

C. map provides two ways to see if contains the key, m.count (key), m.find (key).

D. m.count (key): Since the map does not contain duplicate key, thus m.count (key) value is 0, or 1, indicating whether contains.

E. m.find (key): returns an iterator, determines whether there.

F. For the following scenarios, there is a key to use, otherwise it returns null, there are two ways for writing the following:

if(m.count(key)>0)
{
     return m[key];
 }
return null;

 iter = m.find(key);
 if(iter!=m.end())
 {
    return iter->second;
 }
 return null;

It should be noted: The former method is very intuitive, but many poor efficiency. Because the previous method, requires executing two search. Therefore, the recommended method.

G. STL in the container, there are generic algorithms find (begin, end, target) to find the target, map also provides a member method find (key)

Guess you like

Origin blog.csdn.net/QIJINGBO123/article/details/89818434