How to list all values of a key in multimap? (C/C ++) (STL)

Source: Internet
Author: User

Map and multimap are both self-contained find () and can be searched without generic algorithm. In fact, when both container and algorithm provide methods, we should first test the container self-belt method. Because algorithm still needs iterator to test the generic model, but the container self-belt method is tailor-made, therefore, the compaction speed is faster.

There are three methods to list all values of a key in multimap. This example shows how to use these three methods.

1 /**/ /*  
2 (C) oomusou 2006 Http://oomusou.cnblogs.com
3
4 Filename: multimapfindbykey. cpp
5 Compiler: Visual C + + 8.0/iso c ++
6 Description: Demo how to find by key in multimap
7 Release: 12/16/2006 1.0
8 */
9 # Include < Iostream >
10 # Include < Map >
11 # Include < String >
12
13 Using   Namespace STD;
14
15 Int Main () {
16 Typedef multimap < String , String > Authorbooks;
17 Authorbooks;
18
19 Authorbooks. insert (make_pair ( " Stanley B. Lippman " , " C ++ Primer " ));
20 Authorbooks. insert (make_pair ( " Stanley B. Lippman " , " Essential tail C ++ " ));
21 Authorbooks. insert (make_pair ( " Scott Meyers " , " Valid C ++ " ));
22 Authorbooks. insert (make_pair ( " Andrei Alexandrescu " , " Modern C ++ Design " ));
23
24 String Searchitem =   " Stanley B. Lippman " ;
25
26 // Find all values by key using count & find
27 Authorbooks: size_type entries = Authorbooks. Count (searchitem );
28 Authorbooks: iterator ITER = Authorbooks. Find (searchitem );
29 For (Authorbooks: size_type CNT =   0 ; CNT ! = Entries; ++ CNT)
30 Cout < ITER ++-> Second < Endl;
31
32 Cout < Endl;
33 Cout < Endl;
34
35 // Find all values by key using lower_bound (), upper_bound ();
36 Authorbooks: iterator beg = Authorbooks. lower_bound (searchitem );
37 Authorbooks: iterator end = Authorbooks. upper_bound (searchitem );
38
39 While (Beg ! = End)
40 Cout < Beg ++-> Second < Endl;
41
42 Cout < Endl;
43 Cout < Endl;
44
45 // Find all values by key using sort _range ()
46 Typedef authorbooks: iterator iterab;
47 Pair < Iterab, iterab > Pos = Authorbooks. searchitem );
48 While (Pos. First ! = POs. Second)
49 Cout < POs. First ++-> Second < Endl;
50
51 Return   0 ;
52 }

In addition to using the count () + find () program, the lower_bound ()/upper_bound () and pai_range () programs have almost the same length, so there is no special suggestion.

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.