1. Difference and connection
■ map and set:
Map and set are implemented based on red-black trees. Red-black trees are balanced binary trees, and the time complexity of their addition, deletion, and modification operations is O(logN). The map judges whether the elements are the same according to the operator< comparison, and compares the size of the elements, and then selects the appropriate position to insert into the tree. Therefore, if the map is traversed in order, the output result is ordered.
■unordered_map和unordered_set:
unordered_map and unordered_set are implemented based on hash tables. They are the contents of the C++Boost library, unordered translates to "unordered", but in fact it is not completely disordered, but uses an unhashing storage method. Their search speed is constant, faster than map and set.
And boost::unordered_map calculates the hash value of the element, and judges whether the elements are the same according to the hash value. Therefore, the unordered_map is traversed, and the result is disordered.
2. Specific operations of map and set (multimap and multiset)
■map
Two identical key values are not allowed in the map. If a key1 has been inserted previously, it will fail when inserted again.
□ Insert:
map<int, string> m;
m.insert(pair<int, string>(1, "wanger"));
m.insert(pair<int, string>(2, "lisi"));
m.insert(pair<int, string>(3, "park"));
□ Delete:
map<int, string> m;
m.insert(pair<int, string>(1, "wanger"));
m.insert(pair<int, string>(2, "lisi"));
m.insert(pair<int, string>(3, "park"));
m.erase(1);
□ Exchange:
map<int, string> m1;
map<int, string> m2;
m1.insert(pair<int, string>(1, "wanger"));
m1.insert(pair<int, string>(2, "lisi"));
m1.insert(pair<int, string>(3, "park"));
m2.insert(pair<int, string>(100, "zhangsan"));
m2.insert(pair<int, string>(200, "fantas"));
m2.insert(pair<int, string>(300, "link"));
m1.swap(m2);
□ Output:
map<int, string>::iterator it = m.begin();
while(it != m.end())
{
cout<<(*it).first<<" "<<(*it).second<<endl;
++it;
}
□ Search:
map<int, string> m;
m.insert(pair<int, string>(1, "wanger"));
m.insert(pair<int, string>(2, "lisi"));
m.insert(pair<int, string>(3, "park"));
map<int, string>::iterator it = m.find(2);
cout<<(*it).first<<" "<<(*it).second<<endl;
□ Empty:
map<int, string> m;
m.insert(pair<int, string>(1, "wanger"));
m.insert(pair<int, string>(2, "lisi"));
m.insert(pair<int, string>(3, "park"));
m.clear();
□ In addition, map has the following functions:
count() returns the number of occurrences of the specified element
empty() returns true if the map is empty
equal_range() returns the iterator pair of special items
get_allocator() returns the configurator of the map
key_comp() returns the function that compares the key of the element
lower_bound() Key value >= the first position of a given element
max_size() returns the maximum number of elements that can be accommodated
rbegin() returns a reverse iterator pointing to the end of the map
rend() returns a reverse iterator pointing to the head of the map
size( ) Returns the number of elements in the map
upper_bound() returns the key value> the first position of the given element
value_comp() returns a function to compare element value
■multimap
Multimap supports the existence of two identical key values on the basis of map.
□ Insert:
multimap<int, string> m;
m.insert(pair<int, string>(1, "wanger"));
m.insert(pair<int, string>(2, "lisi"));
m.insert(pair<int, string>(3, "park"));
m.insert(pair<int, string>(1, "bobby"));
■set
A set is an ordered container, all elements are sorted and support operations such as insertion, deletion, and search.
□ Insert:
set<int> s;
for(size_t i = 0; i < 5; i++)
s.insert(i*10);
□ Delete:
set<int> s;
for(size_t i = 0; i < 5; i++)
s.insert(i*10);
s.erase(10);
□ Search:
s.find(10);
□ Empty:
s.clear();
□ Exchange:
set<int> s1;
set<int> s2;
for(size_t i = 0; i < 5; i++)
s1.insert(i*10);
for(size_t i = 0; i < 5; i++)
s2.insert(i*100);
s1.swap(s2);
□ In addition, set has the following functions:
int size() const: returns the number of container elements
bool empty() const: Determine whether the container is empty, if it returns true, it indicates that the container is empty
reverse_iterator rbegin(): Returns the reverse iterator pointer of the tail element
reverse_iterator rend(): Returns the iterator pointer at the previous position of the first element
reverse_iterator rbegin(): Returns the reverse iterator pointer of the tail element
reverse_iterator rend(): Returns the iterator pointer at the previous position of the first element
■multiset
Multiset supports the insertion of two identical elements on the basis of set.
□ Insert:
multiset<int> s;
for(size_t i = 0; i < 5; i++)
s.insert(i*10);
s.insert(10);
3. The specific operation methods of unordered_map and unordered_set (unordered_multimap and unordered_multiset) are similar to map and set.