A map is a dual sequence (key) and value). It provides fast extraction operations based on key codes. That is to say, you can use the subscript operator [] to use the key code as the subscript to perform the search and return the corresponding value. Therefore, we can imagine the use of map as an array with a special subobject. In many cases, it is very convenient and simple to use the subscript operator [] to access the elements in the map. However, if the map subscript operator [] cannot be used, it will also cause unexpected problems.
We know that C ++ does not check whether the subscript is out of bounds. An error occurs during runtime when an array element is accessed with a subscript that exceeds the range of the array subscript. The program may crash. For map, there is no similar concept of subscript out-of-bounds, but it does not exist in map as the key code (key) of the underlying object. In this case, unexpected problems may occur if the application is not proper, and such problems are hidden. If you do not know the subscript operator [] of map, it is difficult to find the problem.
For example, the following code snippet is available:
// Define a map
Map <string, string> mapteldir;
// Use the [] operator to add content to the map conveniently and quickly
Mapteldir ["starlee"] = "13813131313 ";
//...
// Modify the key code to the value of lixing.
// Note that the key code here does not exist in the map
If (mapteldir ["lixing"] = "13913131313 ")
{
Mapteldir ["lixing"] = "13513131313 ";
}
//...
// Output
Cout <"The cellhone Number of lixing is:" <mapteldir ["lixing"] <Endl;
The above Code seems to have no problem, but the phone number output in the last line is always blank! The reason is that the subscript operator [] of map is incorrectly used in the judgment statement.
The subscript operator [] of map is used as the subscript to perform the search and return the corresponding value. If this key code does not exist, insert an item with the default value of the key and value type into this map.
For the above Code, if (mapteldir ["lixing"] = "13913131313") First queries whether the key code in map is lixing, therefore, a <"lixing", ""> (the default value of sting is a Null String) is inserted into the map ). This results in the IF statement always being false, so it is impossible to modify the key code to the lixing value.
To better illustrate this problem, let's take a look at the implementation code of the map subscript operator:
// The following code is taken from .. vcincludemap in vs2005.
Mapped_type & operator [] (const key_type & _ keyval)
{// Find element matching _ keyval or insert with default mapped
Iterator _ Where = This-> lower_bound (_ keyval );
If (_ Where = This-> end ()
| This-> comp (_ keyval, this-> _ key (_ Where. _ mynode ())))
_ Where = This-> insert (_ where,
Value_type (_ keyval, mapped_type ()));
Return (* _ Where). Second );
}
The subscript operator [] of map is defined using the above method, which makes it convenient and fast to insert values into map and access corresponding values using key codes. Although some people may write error code similar to the above when using it, this is a programmer's error, because it is the programmer's responsibility to write the correct code. This is in line with the c ++ style: very flexible, but be careful when using it.