) STD: Summary of map usage

Source: Internet
Author: User
It provides basic map usage, such as insert, search, delete, and traverse. It also shows you how to implement double bond map, including
(1) hit the target only when both keys match
(2) Any matching of the two keys will hit the target.

Can be extended to multiple keys

(1) Introduction
Features:
1. Map binds the key object and t object together. Therefore, it is a pair associative container, indicating that its value type is pair.
2. It is also a unique associative container, indicating that no two elements have the same key.
3. It is also a sorted associative container, so the third parameter can only be less, greater, and other functor. In comparison,
Hash table is a functor such as performance_to and not_to _to.
(2) Basic usage
The following examples show some basic usage of MAP: insert, search, delete, and traverse.

1 # If defined (_ msc_ver)
2 # pragma warning (Disable: 4786)
3 # endif
4 # include <iostream>
5 # include <map>
6 # include <algorithm>
7 int main (INT argc, char * argv [])
8 {
9/* define a map */
10 STD: Map _ map;
11
12/* Insert */
13 _ map. insert (STD: Map: value_type (0, 32.8 ));
14 _ map. insert (STD: Map: value_type (1, 33.2 ));
15 _ map. insert (STD: Map: value_type (2, 35.8 ));
16 _ map. insert (STD: Map: value_type (3, 36.4 ));
17 _ map. insert (STD: Map: value_type (4, 37.8 ));
18 _ map. insert (STD: Map: value_type (5, 35.8 ));
19
20/* this is a common map Assignment Method */
21 _ map [7] = 245.3;
22
23/* Find by key */
24 STD: Map: iterator itr;
25 itr = _ map. Find (4 );
26
27 if (itr! = _ Map. End ())
28 {
29 STD: cout <"item:" <itr-> first <"found, content:" <itr-> second <STD: Endl;
30}
31
32 STD: cout <STD: Endl;
33
34/* Delete item from map */
35 if (itr! = _ Map. End ())
36 {
37 _ map. Erase (itr );
38}
39
40/* travel through a map */
41 STD: Map: iterator itr1 = _ map. Begin ();
42 for (; itr1! = _ Map. End (); ++ itr1)
43 {
44 STD: cout <"item:" <itr1-> first <", content:" <itr1-> second <STD: Endl;
45}
46
47 STD: cout <STD: Endl;
48
49/* empty a map */
50_map. Clear ();
51
52 return 0;
53}

(3) how to define a structure when the key is a structure
For example, if the key is of the mystruct type, map can be defined as follows:
STD: Map> _ map;
The default value of compare is STD: less, which can be left empty. The custom structure must implement the comparison operation specified by compare. Therefore, the custom Structure
Mystruct must be written as follows:

1 struct mystruct
2 {
3 int key;
4
5 bool operator <(const mystruct RHs) const
6 {
7 Return key <RHS. Key;
8}
9 };

Of course, you can also implement global operator <

1 bool operator <(const mystruct LHS, const mystruct RHs)
2 {
3 return LHS. Key <RHS. Key;
4}

In addition, when compare is STD: greater, operator>
(4) how to map two keys and hit the target only when both keys match
You can define the structure mystruct as follows: 1 struct mystruct
2 {
3 int key1;
4 double key2
5
6 bool operator <(const mystruct RHs) const
7 {
8/* both keys must match to hit */
9 return (key1 <RHS. key1 | key2 <RHS. key2 );
10}
11 };

(5) how to map two keys and match any of them to hit the target?
You can define the structure mystruct as follows:

1 struct mystruct
2 {
3 int key1;
4 double key2
5
6 bool operator <(const mystruct RHs) const
7 {
8/* match any two keys to hit */
9 return (key1 <RHS. key1 | (key1> RHS. key1 & key2 <RHS. key2) & (key2 <RHS. key2 ));
10}
11 };

(6) If the stored t can be repeated, multimap can be used.
(7) If the key itself is the T to be stored, just replace map with set.

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.