std : : map

1. How are map and set implemented? How can red-black trees implement these two containers at the same time? Why use red-black trees?

  1. Their bottom layer is implemented in a red-black tree structure, so operations such as insertion and deletion are completed in O(logn) time, so efficient insertion and deletion can be completed;

  2. Here we define a template parameter. If it is a key, then it is a set. If it is a map, then it is a map. The bottom layer is a red-black tree. The node data type of the red-black tree that implements map is key+value, and The node data type that implements set is value

  3. Because map and set are required to be automatically sorted, red-black trees can achieve this function with relatively low time complexity.

2. What are the map insertion methods?

1. Use the insert function to insert pair data
mapStudent.insert(pair<int, string>(1, "student_one")); 

2. Use the insert function to insert value_type data
mapStudent.insert(map<int, string>::value_type (1, "student_one"));

3. Use the make_pair() function in the insert function
mapStudent.insert(make_pair(1, "student_one")); 

4. Insert data using array method
mapStudent[1] = "student_one"; 

3.Why is there no reserve in map?

std::mapIt is one of the associative containers in the C++ Standard Template Library (STL). It uses red-black trees to achieve ordered storage of key-value pairs. In std::map, elements are sorted according to the natural order of keys and cannot be repeated.

std::mapThe reason why the method is not provided reserve()is that, unlike other containers (eg std::vector, , std::unordered_mapetc.), std::mapthe internal data structure of the container is implemented based on a red-black tree, and it does not require continuous memory allocation . Therefore, when creating an empty std::map, there is no need to reserve a certain capacity.

reserve()The method is usually used by the container to pre-allocate a certain amount of storage space . For std::map, since its internal red-black tree structure does not require continuous storage space , there is no problem of reallocation and copying, so there is no need to provide reserve()a method.

If you want to std::mapspecify the initial size when creating, you can do this by passing an existing container as a constructor parameter:

std::map<Key, Value> myMap(existingContainer.begin(), existingContainer.end());

In short, the method is std::mapnot provided reserve()because the characteristics of its internal data structure determine that it does not require continuous storage space, so it does not need to reserve a certain capacity when it is created.

4. When does the map iterator become invalid?

std::map1. Inserting elements: If a new key-value pair is inserted into it during the iteration process , all previous iterators will become invalid, because the insertion operation may cause the internal data structure to be reorganized and change the structure of the tree.

std::map<int, std::string> myMap;
std::map<int, std::string>::iterator it = myMap.begin();

myMap[1] = "One"; // 这里插入新元素
// 现在迭代器 it 可能已经失效

2. Delete elements: If an element is deleted during the iteration process, the iterator pointing to the deleted element will also become invalid.

std::map<int, std::string> myMap;
myMap[1] = "One";
myMap[2] = "Two";
std::map<int, std::string>::iterator it = myMap.begin();

myMap.erase(1); // 删除元素
// 现在迭代器 it 可能已经失效

std::map3. Changing the value of a key: If the value of an existing key is modified during the iteration, the iterator will usually remain valid because they still point to the same key, but be careful, this may change the sorting position of the key .

std::map<int, std::string> myMap;
myMap[1] = "One";
std::map<int, std::string>::iterator it = myMap.begin();

it->second = "NewOne"; // 修改键值
// 迭代器 it 仍然有效,但排序位置可能已经改变

5. The differences and application scenarios of map, set, unordered_set, and unordered_map in STL

map

        map supports automatic sorting of key values. The underlying mechanism is a red-black tree. The query and maintenance time complexity of the red-black tree is O(logn) , but it takes up a lot of space because each node must maintain a parent node and a child node. and color information.

set

        Set is similar to map, and the underlying implementation of set is usually a red-black tree. Set is a special Map with only keys and no values.

unordered_map 

        unordered_map is a newly added container in C++ 11. The underlying mechanism is a hash table. The element position is calculated through the hash function. Its query time complexity is O(1) . The maintenance time is fixed with the length of the list maintained by the buclet bucket, but it is established The hash table takes a lot of time.

unordered_set

        unordered_set is similar to unordered_map, and the underlying implementation of unordered_set is usually a hash table. unordered_set is a special unordered_map with only keys and no values.

It can be seen from the underlying mechanism and characteristics: map is suitable for application scenarios of ordered data, and unordered_map is suitable for application scenarios of efficient query.

Supongo que te gusta

Origin blog.csdn.net/Ricardo_XIAOHAO/article/details/132739618
Recomendado
Clasificación