Recently, I saw a statement in the group: typedef typename T: value_type _ type. In my first response, I thought this was a custom type, later, I checked the information and wrote several examples to prove my idea. However, this is different from the custom type we usually see, especially T :: value_type is confusing to most people. In fact, the meaning is: This T must contain the value_type Member, that is, T must be a class or namespace. If you do not understand it, continue to look down.
// Custom type 1-this is the deformation of the conventional custom type. The imported classtype should be a class and must have the value_type member, for example, the class in STL. template <class classtype, class keytype = int, class valuetype = char *> struct ccustomtype {typedef typename classtype: value_type custom_type; custom_type data ;};
Use ccustomtype to customize the type:
Ccustomtype <string> string _; string _. data = 'C'; // string: value_type is of the char type. The following basic_string shows the ccustomtype <basic_string <char, char_traits <char>, allocator <char >>> string_2; string_2.data = 'X'; // charccustomtype <list <char *> LIST _; LIST _. data = "you are so beautiful"; // char * ccustomtype <set <uint> SET _; Set _. data = 512139461; // uint
Declare ccustomtype <Map <int, char *>, Int, char *> pair _;
Here pair _. Data is the MAP member value_type, that is, the pair type. More specifically, it is pair <int, char *>
Before using ccustomtype <Map <int, char *>, Int, char *>, let's take a look at the usage of pair in STL:
STD: pair <int, char *> pair_1 (2012, "Hello World"); // use the constructor to initialize pair_1 // STD: pair <int, char *> pair_2; pair_2 = make_pair (1989, (char *) "Hansen"); // OK make_pair value assignment // pair_2.first = 2000; // OK pair_2.second = "just test"; // OK // Map <int, char *> NMAP; NMAP. insert (pair_1); NMAP. insert (pair_2); ccustomtype <Map <int, char *>, Int, char *> pair _; // OK map should be used in this way. The key is of the int type, value is char * type // (pair _. data ). first = 2012; error c3892: "pair _": the constant cannot be assigned. // view pair _. data. first is a const int type. Why does it become a const Int? The <int, char *> type that I passed in should be pair <int, char *>, at first, I had this question... (pair _. data ). second = "that's OK"; // OK, value is not const, and can be assigned a value
Analysis:
// According to the prototype above, declare a pair type variable pair_testmap <int, char * >:: value_type pair_test (2013, "test "); // follow up step by step through the STL source code to view the map's value_type, first: const int, second: char * // 1. map value_type comes from _ mybase member value_type: typedef typename _ mybase: value_type; // 2. continue with the discovery that _ mybase is a template class constructed by _ tmap_traits: typedef _ tree <_ tmap_traits <_ KTY, _ ty, _ PR, _ alloc, false> _ mybase; // 3. I will reveal the answer, and then follow _ tmap_traits to find that he has defined the pair type: typedef pair <const _ KTY, _ ty> value_type; // 4. this is why the map key is of the const type. It also indicates that the data member of the custom type ccustomtype above is actually pair <const T, T>
So how can we assign values to data members of the custom type ccustomtype? The answer is that through the constructor, we will perform a simple deformation on ccustomtype:
Template <class classtype, class keytype = int, class valuetype = char *> struct ccustomtypeex {typedef typename classtype: value_type custom_type; custom_type data; ccustomtypeex () {} ccustomtypeex (keytype _ key, valuetype _ value): Data (_ key, _ value) // call the classtype: pair constructor {}};
CCustomTypeEx<map<int,char*>,int,char*> pair_ex(1989,"hansen");// ok
Conclusion: For the ccustomtype <map, Int, char *> pair _ problem, I actually stole the concept from the beginning, and the previous red sentence was not completely understood, identify pair _. data is a pair type, and we firmly believe that pair can be assigned after construction. It is true that regular pair, such as STD: pair, can indeed do this, but do not forget the key phrase typedef typename classtype: value_type custom_type in ccustomtype. That is to say, this value_type must be a member of classtype, So pair _. data
It should be map: value_type, which is the pair <const int, char *> type.
Another three o'clock... close... sleep .....