C differs from Python in terms of the dictionary with the same name, but it has the same data structure with similar functionality. C supports mapping, which can be used in the STL class std::map. The map object contains a pair of values in each entry, one is the key value and the other is the map value. Key values are used to search for and uniquely identify entries in the map. While mapped values are not necessarily unique, key values must always be unique in the map. Let's take a look at how to use mapping.
First, let's see how to define a mapping data structure in C.
#include <map> map <data_type 1, data_type 2> myMap; </map>
Let’s take an example and see how to do this −
#include <iostream> #include <map> using namespace std; int main() { //initialising the map map <int, string> myMap; //inserting two key-value pairs myMap.insert({1, "Hello"}); myMap.insert({2, "World"}); //displaying the key-value pairs for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) { cout << itr->first << " " << itr->second << endl; } return 0; }
1 Hello 2 World
In C, maps (Maps) can be initialized in different ways. The algorithm is very simple.
Create a map object.
Assign a value to an object when it is declared.
Using an initialization list to initialize a map is the same as initializing an array in C. We just need to assign key-value pairs when declaring the mapping, enclosed in curly braces, in the format {key, value}. The syntax is as follows.
#include <map> map <data_type 1, data_type 2> myMap = {{key1, value1}, {key2, value2}};
#include <iostream> #include <map> using namespace std; int main() { //initialising the map map <int, string> myMap = {{1, "One"}, {2, "Two"}, {3, "Three"}}; //displaying the key-value pairs for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) { cout << itr->first << " " << itr->second << '\n'; } return 0; }
1 One 2 Two 3 Three
This is similar to assigning a value to a specific index in an array. We didn't mention the index, but put the key values in the map subscript, just like in an array.
#include <map> map <data_type 1, data_type 2> myMap; myMap[key1] = value1; </map>
#include <iostream> #include <map> using namespace std; int main() { //declaring the map map <int, string> myMap; myMap[1] = "One"; myMap[2] = "Two"; myMap[3] = "Three"; //displaying the key-value pairs for (auto itr = myMap.begin(); itr != myMap.end(); ++itr) { cout << itr->first << " " << itr->second << '\n'; } return 0; }
1 One 2 Two 3 Three
It may be necessary to copy a map into another map, so we can initialize a map from another map. We take advantage of the map class's copy constructor by passing the map object to the map's copy constructor at declaration time.
#include <map> map <data_type 1, data_type 2> myMap1(myMap2);
#include <iostream> #include <map> using namespace std; int main() { //declaring the map map <int, string> myMap; myMap[1] = "One"; myMap[2] = "Two"; myMap[3] = "Three"; //copying using copy constructor map <int, string> myMap2(myMap); //displaying the key-value pairs for (auto itr = myMap2.begin(); itr != myMap2.end(); ++itr) { cout << itr->first << " " << itr->second << '\n'; } return 0; }
1 One 2 Two 3 Three
Map in C is an ordered set, that is, the elements in the Map are sorted by key value. This makes it slower compared to other similar data structures such as an unordered map where key-value pairs are not sorted. All operations in the map have logarithmic complexity and are implemented in memory as a red-black tree. However, in practice, mapping is very useful because it provides great flexibility in storing data in a key-value manner. We've discussed all the main ways to initialize a map; while there are many ways to initialize, these are the most intuitive ways to do it.
The above is the detailed content of C++ program initialization dictionary. For more information, please follow other related articles on the PHP Chinese website!