STD: Map usage

Source: Internet
Author: User
STD: Map usage STL is a set of template classes for Standard C ++ systems. The biggest benefit of using STL template classes is that they are common in various C ++ compilers. In the STL template class, the main classes used for linear data storage and management include vector, list, and map. This article describes the specific usage of a map object based on the process of learning the object by yourself. I am a beginner and have a limited level. I would like to explain the mistakes. Please criticize and correct them more. The functions implemented by the map object are similar to those implemented by the MFC cmap. However, according to the Introduction and discussion in some articles, there is a certain gap between the MFC cmap and STL map in each aspect, for example, it is not a C ++ standard, it does not support Value assignment construction, and the concept of objectization is not clear. When using the map object, you must first include the header file. The following include statement must be included: # include <map> note that the STL header file does not have an extension. h. After the header file is included, you can define and use the map object. The map object is a template class and requires two template parameters, such as STD: Map <int, cstring> enummap; in this way, a map object that uses int as the keyword to search cstring entries is defined. STD represents the namespace, and the map object is in the STD namespace. For convenience, here I still use the cstring class. In fact, we should use the STD: string class of the Standard C ++. Let's define the type of the template class, which is convenient to use. Of course, the Code is as follows: typedef STD: Map <int, cstring> udt_map_int_cstring;
Udt_map_int_cstring enummap; so the map object is defined and added. It is very easy to change the entries in the map because the map class has already overloaded the [] operator. The Code is as follows: enummap [1] = "one ";
Enummap [2] = "two ";
.....
Enummap [1] = "one edit"; or the insert method enummap. insert (make_pair (1, "one"); returns the total number of currently stored items in the map using the size () method: int nsize = enummap. size (); Use the find method to find whether a map contains a keyword. The input parameter is the key to be searched. In our example, it is an int data, the entry data in map is stored in sequence and is called a sequence. here we need to mention two members: Begin () and end, the first and last entries in the map object are represented respectively. The two data types are iterator, and iterator is defined as the type of entries in the map. The code for finding whether a certain entry is contained is as follows: int nfindkey = 2; // key to be searched
Udt_map_int_cstring: iterator it; // defines an entry variable (actually a pointer)
It = enummap. Find (nfindkey );
If (IT = enummap. End ()){
// Not found
}
Else {
// Find
} // Pay attention to the Data Type of the key when you find the key. It is best to use a key such as cstring that can eliminate the difference in data types. Otherwise, it may still fail to be found after forced conversion.It should be noted that iterator, begin (), end () is a general concept of the STL template class, and the operation method is similar to the iterator data type obtained through the map object method is a STD :: pair object, including two data iterator. first and iterator. second represents the keyword and stored data respectively. remove an entry using erase (). The definition of this Member method is as follows: iterator erase (iterator it );
Iterator erase (iterator first, iterator last );
Size_type erase, this object can be obtained from methods such as find. The second definition deletes a range, and requires a start entry and a termination entry. The third object is deleted by a keyword, this is the closest to our thoughts and habits. The code example is as follows: enummap. erase (1); // Delete the entry corresponding to the keyword "1"
Enummap. Erase (enummap. Begin (); // Delete the first entry
Enummap. erase (enummap. begin (), enummap. begin () + 1); // Delete the first two entries. The addition, deletion, modification, and query are all done. I believe that after reading this article, the map object should also be used, these are the results of my research over a week and I will share them with you. Finally, there is an empty (). You don't need to ask. When deleting all objects, do not use an erase one by one. Empty () is equivalent to enummap. erase (enummap. begin (), enummap. end (); map traversal: # include <map>
# Include <string>
# Include <iostream>
Using namespace STD;

Int main ()
{
   Map <string, int>M;
   M ["A"] = 1;
   M ["B"] = 2;
   M ["C"] = 3;
   Map <string, int >:: iterator it;
   For (IT = M. Begin (); it! = M. End (); ++ it)
     Cout <"key:" <it-> first <"value:" <it-> second <Endl;
   Return 0;
}

 Map <string, int >:: iterator it; Define an iteration pointer to it.It-> first is the index key value, and it-> Second is the value.

When using an object, it is best to call its clear method in the destructor, such as class A {Map <int, int> m ;~ A () {M. Clear ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.