vector definition method
Define and initialize 3 two-dimensional arrays with 9 rows and 9 columns and a value of 0
vector<vector<int>> row (9, vector<int>(9,0)); vector<vector<int>> col (9, vector<int>(9,0)); vector<vector<int>> box (9, vector<int>(9,0));
Hash initialization
Code example
#include <iostream> #include <unordered_map> #include <map> #include <string> using namespace std; int main() { //Note: C++11 only supports bracket initialization unordered_map<int, string> myMap={{ 5, "Zhang Da" },{ 6, "Li Wu" }};//Assignment with {} myMap[2] = "Li Si"; //Use [] for single insertion. If the key value 2 already exists, the assignment will be modified. If not, it will be inserted. myMap.insert(pair<int, string>(3, "Chen er"));//Insert using insert and pair //Use of traversal output + iterator auto iter = myMap.begin();//auto is automatically recognized as the iterator type unordered_ map<int,string>::iterator while (iter!= myMap.end()) { cout << iter->first << "," << iter->second << endl; ++iter; } //Find elements and output the use of + iterators auto iterator = myMap.find(2);//find() returns an iterator pointing to 2 if (iterator != myMap.end()) cout << endl<< iterator->first << "," << iterator->second << endl; system("pause"); return 0; }
Unordered is used at this time_ Map, the output result is:
If unordered_ Replace map with map, and the output result is:
map and unordered_map difference
The header files to be imported are different
map: #include < map >
unordered_map: #include < unordered_map >
ยท
Different internal implementation mechanisms
Map: a red black tree is implemented inside the map (red black tree is a non strictly balanced binary search tree, while AVL is a strictly balanced binary search tree). The red black tree has the function of automatic sorting. Therefore, all elements inside the map are orderly, and each node of the red black tree represents an element of the map. Therefore, a series of operations such as searching, deleting and adding to the map are equivalent to the operations on the red black tree. The elements in the map are stored according to the binary search tree (also known as binary search tree and binary sort tree. The feature is that the key values of all nodes in the left subtree are less than the key values of the root node, and the key values of all nodes in the right subtree are greater than the key values of the root node). The key values can be traversed from small to large by using medium order traversal.
unordered_map: unordered_map implements a hash table (also known as hash table. By mapping the key value to a position in the hash table to access the records, the time complexity of search can reach O(1), which is widely used in massive data processing). Therefore, the arrangement order of its elements is disordered. Hash table details
Advantages, disadvantages and applicability
map:
advantage:
Ordering, which is the biggest advantage of map structure. The ordering of its elements will simplify a lot of operations in many applications
The red black tree implements a red black book internally, so that many operations of map can be realized under the time complexity of lgn, so the efficiency is very high
Disadvantages: the space occupancy rate is high because the map implements a red black tree internally, which improves the operation efficiency, but because each node needs to save the parent node, child node and red / Black properties, each node occupies a lot of space
Where applicable: for those problems with sequence requirements, using map will be more efficient
unordered_map:
Advantages: because the hash table is implemented internally, its lookup speed is very fast
Disadvantages: the establishment of hash table is time-consuming
Where applicable: unordered for finding problems_ Map will be more efficient, so when you encounter a search problem, you often consider using unordered_map
Summary:
The problem of memory occupancy is transformed into red black tree VS hash table or unorder_map takes up more memory.
But unordered_ The execution efficiency of map is much higher than that of map
For unordered_map or unordered_ The traversal order of the set container is not necessarily the same as that entered when the container was created, because the traversal is from front to back according to the hash table
Map and unordered_ Use of map
unordered_ The usage of map is the same as that of map. It provides operations such as insert, size and count, and the elements in it are also stored in pair type. Its underlying implementation is completely different, which has been explained above, but it is consistent in terms of external use.