Map usage in STL

Source: Internet
Author: User

Map is an associated container of STL, which provides one-to-one (the first can be called a keyword, each keyword can only appear once in map, and the second can be called the value of this keyword) because of this feature, it is possible to provide a quick channel for programming when we process one-to-one data. Here, we will talk about the organization of map internal data. Within map, we will build a red/black tree (a non-strictly balanced binary tree), which has the ability to automatically sort data, therefore, all the data in the map is ordered, and we will see the benefits of ordering later.

The following is an example of one-to-one data ing. For example, in a class, each student's student ID has a one-to-one ing relationship with his name. This model may be easily described using map. Obviously, the student ID is described using Int, the name is described by a string (this article does not use char * to describe the string, but uses string in STL). The following is the map description code: Map <int, string> mapstudent;
1. Map Constructor
Map provides a total of six constructor functions, which involve the memory distributor and are omitted from the table. Below we will see some map constructor methods,

Here, we usually use the following method to construct a map:
Map <int, string> mapstudent;
2. Data insertion
After constructing the map container, We can insert data into it. Here are three data insertion methods:
First, insert the pair data using the insert function. The following example shows that (although the following code is hand-written, it can be compiled in VC and GCC,

You can run the following command to check the effect. In VC, add this statement to block the 4786 warning # pragma warning (Disable: 4786 ))
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
Map <int, string> mapstudent;
Mapstudent. insert (pair <int, string> (1, "student_one "));
Mapstudent. insert (pair <int, string> (2, "student_two "));
Mapstudent. insert (pair <int, string> (3, "student_three "));
Map <int, string >:: iterator ITER;
For (iter = mapstudent. Begin (); iter! = Mapstudent. End (); ITER ++)
{
Cout <ITER-> first <"<ITER-> second <end;
}
}
Type 2: Use the insert function to insert value_type data. The following is an example.
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
Map <int, string> mapstudent;
Mapstudent. insert (Map <int, string>: value_type (1, "student_one "));
Mapstudent. insert (Map <int, string >:: value_type (2, "student_two "));
Mapstudent. insert (Map <int, string >:: value_type (3, "student_three "));
Map <int, string >:: iterator ITER;
For (iter = mapstudent. Begin (); iter! = Mapstudent. End (); ITER ++)
{
Cout <ITER-> first <"<ITER-> second <end;
}
}
Method 3: insert data using arrays. The following is an example.
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
Map <int, string> mapstudent;
Mapstudent [1] = "student_one ";
Mapstudent [2] = "student_two ";
Mapstudent [3] = "student_three ";
Map <int, string >:: iterator ITER;
For (iter = mapstudent. Begin (); iter! = Mapstudent. End (); ITER ++)
{
Cout <ITER-> first <"<ITER-> second <end;
}
}

The following is an explanation of pair:

----------------------------------------------

Pair type Overview

Pair is a template type that contains two data values. The two data types can be different. The basic definition is as follows:

 

Pair <int, string>;

It indicates that there are two types in A. The first element is of the int type, and the second element is of the string type. If pair is not initialized during creation, the default constructor is called to initialize the constructor.

 

Pair <string, string> A ("James", "Joy ");

It can also be initialized directly when defined as above.

 

Because the pair type is cumbersome to use, if you want to define multiple pair types in the same shape, you can use typedef to simplify the statement:

Typedef pair <string, string> author;

Author Pro ("may", "Lily ");

Author Joye ("James", "Joyce ");

 

 

Pair object operations

 

  • For a pair class, since it has only two elements named first and second, it can directly access its members by using common vertex operators.

Pair <string, string> A ("Lily", "poly ");

String name;

Name = pair. Second;

  • Generate a new pair object

You can use make_pair to construct a new pair type for the existing two data types:

Int A = 8;

String M = "James ";

Pair <int, string> newone;

Newone = make_pair (A, M );

 

--------------------------------------------------------------

3.Map size
How do we know how much data has been inserted into the map? The size function can be used as follows:
Int nsize = mapstudent. Size ();
4.Data Traversal
Three methods are also provided to traverse the map.
First: the application's forward iterator, which is everywhere in the above example, skipped
Type 2: Apply the reversed-phase iterator. The following is an example to illustrate the effect. Run the program on your own.
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
Map <int, string> mapstudent;
Mapstudent. insert (pair <int, string> (1, "student_one "));
Mapstudent. insert (pair <int, string> (2, "student_two "));
Mapstudent. insert (pair <int, string> (3, "student_three "));
Map <int, string >:: reverse_iterator ITER;
For (iter = mapstudent. rbegin (); iter! = Mapstudent. rend (); ITER ++)
{
Cout <ITER-> first <"<ITER-> second <end;
}
}
Method 3: Use the array method. The program description is as follows:
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
Map <int, string> mapstudent;
Mapstudent. insert (pair <int, string> (1, "student_one "));
Mapstudent. insert (pair <int, string> (2, "student_two "));
Mapstudent. insert (pair <int, string> (3, "student_three "));
Int nsize = mapstudent. Size ()
// The error is returned. It should be for (INT nindex = 1; nindex <= nsize; nindex ++)
// By rainfish
For (INT nindex = 0; nindex <nsize; nindex ++)
{
Cout <mapstudent [nindex] <end;
}
}
5.Data Search(Including determining whether this keyword appears in map)
Here, we will understand the benefits of ensuring orderly data insertion by map.
There are many methods to determine whether a data (keyword) appears in map. Although the title here is a data search, a large number

Basic Map usage.
Three data search methods are provided here.
First, use the count function to determine whether a keyword exists. The disadvantage is that the data location cannot be located. Due to the characteristics of map, the one-to-one ing relationship

The returned value of the count function is either 0 or 1. Of course, 1 is returned.
Type 2: Use the find function to locate the data occurrence location. It returns an iterator. When the data appears, it returns the iterator of the data location,

If there is no data to be searched in the map, the iterator returned by the map is equal to the iterator returned by the end function.
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()
{
Map <int, string> mapstudent;
Mapstudent. insert (pair <int, string> (1, "student_one "));
Mapstudent. insert (pair <int, string> (2, "student_two "));
Mapstudent. insert (pair <int, string> (3, "student_three "));
Map <int, string >:: iterator ITER;
Iter = mapstudent. Find (1 );
If (ITER! = Mapstudent. End ())

{
Cout <"find, the value is" <ITER-> second <Endl;
}
Else
{
Cout <"do not find" <Endl;
}
}
Method 3: This method is intended to determine whether or not data appears. However, I intend to explain it here.
Lower_bound function usage. This function is used to return the lower bound of the keyword to be searched (an iterator)
Upper_bound function usage. This function is used to return the upper bound of the keyword to be searched (an iterator)
For example, if the values of 1, 2, 3, and 4 have been inserted in map, if lower_bound (2), 2 is returned, and upper-bound (2) is returned.

Is 3
The pai_range function returns a pair. The first variable in pair is the iterator returned by lower_bound. The second iterator in pair is

The iterator returned by upper_bound. If the two iterators are equal, this keyword is not displayed in map.
# Include <map>
# Include <string>
# Include <iostream>
Using namespace STD;
Int main ()

{
Map <int, string> mapstudent;
Mapstudent [1] = "student_one ";
Mapstudent [3] = "student_three ";
Mapstudent [5] = "student_five ";
Map <int, string >:: iterator ITER;
Iter = mapstudent. lower_bound (2 );
{
// The iterator of lower bound 3 is returned.
Cout <ITER-> second <Endl;
}
Iter = mapstudent. lower_bound (3 );
{
// The iterator of lower bound 3 is returned.
Cout <ITER-> second <Endl;
}
Iter = mapstudent. upper_bound (2 );

{
// The iterator of the upper bound 3 is returned.
Cout <ITER-> second <Endl;
}
Iter = mapstudent. upper_bound (3 );
{
// The iterator with the upper bound 5 is returned.
Cout <ITER-> second <Endl;
}
Pair <Map <int, string >:: iterator, Map <int, string >:: iterator> mappair;
Mappair = mapstudent. interval _range (2 );
If (mappair. First = mappair. Second)
{
Cout <"do not find" <Endl;
}
Else

{
Cout <"find" <Endl;
}
Mappair = mapstudent. interval _range (3 );

If (mappair. First = mappair. Second)
{

Cout <"do not find" <Endl;
}
Else

{
Cout <"find" <Endl;
}

}
6.Empty and empty data clearing
The clear () function can be used to clear the data in the map to determine whether there is data in the map. The empty () function can be used. If it returns true, it indicates that the map is empty.
7. data deletion
Here, the erase function is used. It has three overloaded functions. The usage of these functions is described in detail in the example below.

# Include <map>

# Include <string>

# Include <iostream>

Using namespace STD;

Int main ()

{

Map <int, string> mapstudent;

Mapstudent. insert (pair <int, string> (1, "student_one "));

Mapstudent. insert (pair <int, string> (2, "student_two "));

Mapstudent. insert (pair <int, string> (3, "student_three "));

// If You Want To demonstrate the output effect, select one of the following, and the effect you see will be better.
// Use the iterator to delete 1.
Map <int, string >:: iterator ITER;
Iter = mapstudent. Find (1 );
Mapstudent. Erase (ITER );
// If you want to delete 1, use the keyword to delete
Int n = mapstudent. Erase (1); // If deleted, 1 is returned; otherwise, 0 is returned.
// Use an iterator to delete parts
// The Code clears the entire map
Mapstudent. earse (mapstudent. Begin (), mapstudent. End ());
// When you delete a part, it is also a STL feature. The delete interval is a set of pre-closed and post-open.
// Add the traversal code and print the output.
}

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.