Map is an associated container used to store key-value data. The key is the keyword used for searching, and the value is the actually stored value.
A specific keyword can only be associated with a unique value. Map is a Sort structure composed of a pair of key values (key/value,
The key value is unique.
MAP is usually implemented using a balanced binary search tree. Therefore, the time complexity of log (n) can be ensured by map insertion, deletion, and search. For
Map is a good choice for the insertion and query of massive data.
This article will explain the common operations of map. Improper, welcome criticism (bicheng.gui@gmail.com ).
1. Map Construction:
Map provides the following template constructor:
Template <class T1, class T2>
Map (); // default constructor
Map (const map & M) // copy constructor
Map (iterator begin, iterator end); // interval Constructor
Map (iterator begin, iterator end, const traits & _ compare) // constructor with comparison predicates
Map (iterator begin, iterator end, const traits & _ compare, const Allocator & all) // With distributor
Example:
# Include <map>
# Include <iostream>
Using namespace STD;
Typedef pair <int, int> int_pair;
Map <int, int >:: iterator itr1;
Map <int, int> m1; // Ascending by default, from small to large
M1 [1] = 10;
M1 [2] = 20;
M1 [3] = 30;
// Output result 10 20 30 with line breaks
For (itr1 = m1.begin (); itr1! = M1.end (); ++ itr1)
{
Cout <itr1-> second <Endl;
}
Map <int, Int, greater <int> m2; // descending Map
Map <int, Int, greater <int >:: iterator itr2;
M2 [1] = 10; // insert an element with a key value of 1 and a value of 10. multimap does not support operator []
M2.insert (int_pair (); // map valuetype is pair (T1, T2 );
M2 [3] = 30;
// Outputs 30, 20, 10
For (itr2 = m2.begin (); itr2! = M2.end (); ++ itr2)
{
Cout <itr2-> second <Endl;
}
// Use the memory distributor of M1 to construct an M3
Map <int, int >:: allocator_type Al = m1.get _ Allocator ();
Map <int, int> m3 (less <int> (), Al );
// Copy an M4 object based on M1
Map <int, int> M4 (M1); // The predicate and type must match.
// Construct a map based on two iterator
Map <int, Int, greater <int> M5 (m2.begin (), m2.end ());
// Output M5, 30,20, 10, with line breaks.
For (itr2 = m5.begin (); itr2! = M5.end (); ++ itr2)
{
Cout <itr2-> second <Endl;
}
The above Code requires the vs2005 and later compilers.
2. Common map operations:
Map supports common iterator interfaces, such as begin (), end (), rbegin (), and rend ().
Empty () checks whether the map is empty. Returns true if it is null.
Size () returns the length of the map.
Maxsize () returns the maximum number of elements that a map can accommodate, which varies with the compiler and platform.
3. Pair <iterator, bool> insert (iterator where, const value_type & Val)
You can insert an object, copy several copies of an object, or objects in a range.
The insert function inserts an object before the element indicated by where.
Sample Code:
Map <int, int >:: iterator itr1, itr2;
Map <int, int> M1, M2;
Typedef pair <int, int> int_pair;
M1.insert (int_pair (1, 10 ));
M1.insert (int_pair (3, 30 ));
Because the map container does not allow duplicate key values, you can obtain whether the operation is successful Based on the returned value after the insert operation is executed.
The returned value is a pair of an iterator and a Boolean value. The iterator points to an element with this value in the map. The Boolean value indicates whether the insert is successful.
If the Boolean value is true, the insertion is successful, and the iterator positions the New Insertion value in the map.
If the Boolean value is false, insertion fails (this value already exists), and The iterator is the original position in the map.
Pair <Map <int, int >:: iterator, bool> insertresult;
Insertresult = m1.insert (int_pair (1, 10 ));
If (insertresult-> second)
{
// Insert successful
}
Else
{
// Insertion failed
}