C ++ STL map usage Problems

Source: Internet
Author: User

The map class provided by the C ++ standard library is the container that saves the key-value pairs of <key, value>. The '[]' operator provides semantics similar to Random Access to arrays, you can use the key to conveniently access or modify the corresponding value.

In addition, as a container, STL map provides the corresponding iterator Map <key-type, value-type>: iterator, this allows us to easily traverse map key-Value Pair members through iterator.

The above interface provided by STL map can greatly simplify the data structure of our code, but for developers who are not familiar with STL map usage or do not understand the implementation principles of STL map, I often encounter unexpected problems due to incorrect use, which is also the reason for writing them here.

 

1. Problems Caused by operator [] Semantics

Define Map <string, mystruct *> map_sample, and mystruct as the custom type.

Using the [] operator, we can access map members like arrays, which is very convenient, for example:

Mystruct * PTR = map_sample ["wjb"];

If the key wjb does not exist, but the key 'wjb 'does not exist in the current map_sample, the operator [] automatically adds the key wjb to map_sample, and initialize the default value (which will lead to the value being initialized as null ). This feature may cause problems in some application scenarios. If the value mystruct * Is malloc each time, it will be used directly after being accessed in another task through, when the above condition does not exist, it will lead to a null pointer to access or the core dump of the program will be triggered when free is called. Here you will say that it is because you did not judge whether the pointer is null after using [], not a [] problem, however, the 'wjb 'Member is actually added. When you find that the returned pointer is null, do you want to delete the null key-Value Pair in erase? -- Thankless

Therefore, if your program cannot determine whether a key exists, we recommend that you do not use the '[]' operator. Instead, you can use the find operator to determine whether a key exists and then access the key to avoid the above problems.

 

2. Usage of map iterator

Let's take a look at the following code snippet:

STD: Map <int, int >:: iterator it = m_map.begin (); // Row 1
For (; it! = M_map.end (); It ++) // Row 2
{// Row 3
M_map.erase (it); // row 4
}

The above code will cause program memory access problems, resulting in program core dump.

The internal implementation of STL map is the RB tree. Therefore, <key, value> appears for a node inside the tree, prev and so on. Therefore, after 4 erase drops the it key-value team, the memory to which it points does not belong to this map object and may be recycled by the OS at any time, the IT ++ operations in Row 2 depend on the next pointer of it, which causes memory access problems and your program may be core, if the memory to which it points has been recycled. We can solve this problem through the following methods:

STD: Map <int, int >:: iterator it = m_map.begin (); // Row 1
For (; it! = M_map.end (); m_map.erase (it ++) // Row 2
{

}

After STL map calls erase, The iterator becomes invalid. Remind yourself when using it.

 

I am a cainiao, But I just wrote down the recorded problems. Maybe it is wrong, huh, huh.

PS: My first blog, wow, haha ~~~~~~~~~~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.