STL-multimap usage

Source: Internet
Author: User
From: http://lingchuangsong.blog.163.com/blog/static/12693232201032714325288/

Example:

# Include "stdio. H"
# Include "stdlib. H"
# Include <iostream>
# Include <stack>
# Include <queue>
# Include <string>
# Include <cmath>
# Include <map>
Using namespace STD;

Void main ()
{

Multimap <int, string> test;
Test. insert (make_pair (1, "11 "));
Test. insert (make_pair (2, "22 "));
Test. insert (make_pair (3, "33 "));
Test. insert (make_pair (4, "44 "));
Test. insert (make_pair (5, "55 "));
Test. insert (make_pair (4, "4444 "));
Test. insert (make_pair (5, "55555 "));
Test. insert (make_pair (4, "4444 + 111 "));
Test. insert (make_pair (5, "55555 + 222 "));

Multimap <int, string >:: iterator ite;

For (ITE = test. Begin (); ite! = Test. End (); ite ++)
{
Cout <ite-> first <"" <ite-> second <Endl;
}

ITE = test. Find (4 );
If (ITE! = Test. End ())
{
Cout <"find the value" <Endl;
}
Else
{
Cout <"not find the value" <Endl;
}

Pair <multimap <int, string >:: iterator, multimap <int, string >:: iterator> Pai;
Pai = test. pai_range (4 );
Multimap <int, string >:: iterator it;

For (IT = Pai. First; it! = Pai. Second; ++ it)
{
Cout <it-> second <Endl;

}
}

Standard Template Library (STL): multimap usage example

13:43:25 | category: STL | font size subscription

// Multimaptest. cpp: defines the entry point of the console application.

//

# Include "stdafx. H"

# Include <iostream>

# Include <string>

# Include <map>

Using namespace STD;

Struct userdevice {

String m_devicename;

String m_deviceid;

Int m_devicepopedom;

};

Typedef multimap <string, userdevice> usertable;

Typedef usertable: const_iterator cit;

Typedef pair <CIT, CIT> range;

Int _ tmain (INT argc, _ tchar * argv [])

{

Cit;

Userdevice D1, D2, D3, D4;

D1.m _ DeviceID = "12341234 ";

D1.m _ devicename = "d1 ";

D1.m _ devicepopedom = 123;

D2.m _ DeviceID = "23622344 ";

D2.m _ devicename = "D2 ";

D2.m _ devicepopedom = 234;

D3.mb _ DeviceID = "3451234 ";

D3.m _ devicename = "D3 ";

D3.m _ devicepopedom = 345;

D4.mb _ DeviceID = "43622344 ";

D4.m _ devicename = "D4 ";

D4.m _ devicepopedom = 456;

Usertable m_user;

M_user.insert (make_pair ("zhangsanfeng", D1 ));

M_user.insert (make_pair ("zhangsanfeng", D2 ));

M_user.insert (make_pair ("zhangsanfeng2", D3 ));

M_user.insert (make_pair ("zhangsanfeng2", D4 ));

// Method 1

Range = m_user.equal_range ("zhangsanfeng ");

For (CIT I = range. First; I! = Range. Second; I ++)

{

Cout <I-> second. m_deviceid <','

<I-> second. m_devicename <','

<I-> second. m_devicepopedom

<Endl;

}

Cout <Endl;

// Method 2

CIT it2 = m_user.find ("zhangsanfeng2 ");

While (it2! = M_user.end ())

{

Cout <it2-> second. m_deviceid <','

<It2-> second. m_devicename <','

<It2-> second. m_devicepopedom <','

<Endl;

It2 ++;

}

Cout <Endl;

// Traverse

CIT it3 = m_user.begin ();

While (it3! = M_user.end ())

{

Cout <it3-> second. m_deviceid <','

<It3-> second. m_devicename <','

<It3-> second. m_devicepopedom <','

<Endl;

It3 ++;

}

Cin. Get ();

Return 0;

}

Lower_bound () and upper_bound (): lower_bound (k) Find the first value associated with the key K, and upper_bound (k) is to find the element whose first key value is greater than K. The following example uses upper_bound () to locate the first element whose key value is greater than "213.108.96.7. Generally, when the key is a string, there will be a Dictionary Compilation comparison:

DNS. insert (make_pair ("219.108.96.70", "pythonzone.com "));

CIT = DNS. upper_bound ("213.108.96.7 ");

If (CIT! = DNS. End () // found anything?

Cout <cit-> second <Endl; // display: pythonzone.com

If you want to display all the values, you can use the following loop:

// Insert multiple values with the same key

DNS. insert (make_pair ("219.108.96.70", "pythonzone.com "));

DNS. insert (make_pair ("219.108.96.70", "python-zone.com "));

// Obtain the iteration pointer of the first value

CIT = DNS. upper_bound ("213.108.96.7 ");

// Output: pythonzone.com, python-zone.com

While (CIT! = DNS. End ())

{

Cout <cit-> second <Endl;

++ Cit;

}

 

Http://www.cppblog.com/mydriverc/articles/33141.html

 

-=---=-

 

Part 2

 

-=---=-

 

In the article "using the <map> library to create an associated container", we discussed the map associated container in the standard library. But that is only part of the map container. The standard library also defines a multimap container, which is similar to map, except that it allows duplicate keys. This attribute makes multimap more useful than expected: for example, a person with the same phone book can have more than two phone numbers, in the file system, you can map multiple symbolic links to the same physical file, or the DNS server can map several URLs to the same IP address. In these scenarios, you can:

// Note: pseudo code

Multimap <string, string> phonebook;

Phonebook. insert ("Harry", "8225687"); // home phone number

Phonebook. insert ("Harry", "555123123"); // phone number of the organization

Phonebook. insert ("Harry", "2532532532"); // mobile phone number

The ability to store duplicate keys in multimap greatly affects its interfaces and usage.

So how do I create an associated container with a non-unique key?The answer is to use the multimap container defined in the <map> library.

 

Raise Questions

Unlike map, multimap can contain duplicate keys. This raises the question: how does the overload subscript operator return multiple correlated values with the same key? The following pseudo code is used as an example:

String phone = phonebook ["Harry]; the standard library designer solves this problem by removing the subscript operator from multimap. Therefore, you must use different methods to insert and obtain elements and handle errors.

Insert

Suppose you need to develop a DNS Background Program (that is, a service program in Windows), which maps IP addresses to matched URL strings. You know that in some cases, the same IP address will be associated with multiple URLs. All these URLs point to the same site. In this case, you should use multimap instead of map. For example:

# Include <map>

# Include <string>

Multimap <string, string> dns_daemon; Use the insert () member function instead of the subscript operator to insert elements. Insert () has a pair type parameter. In "use <map> library to create associated containers", we demonstrate how to use the make_pair () helper function to complete this task. You can also use it as follows:

Dns_daemon.insert (make_pair ("213.108.96.7", "cppzone.com"); In the insert () call above, "213.108.96.7" is the key, and "cppzone.com" is the associated value. The same key will be inserted later, with different associated values:

Dns_daemon.insert (make_pair ("213.108.96.7", "cppluspluszone.com"); therefore, dns_daemon contains two elements with the same key value. Note that values returned by multimap: insert () and MAP: insert () are different.

Typedef pair <const key, T> value_type;

Iterator

Insert (const value_type &); // #1 multimap

Pair <iterator, bool>

Insert (const value_type &); // #2 Map

The multimap: insert () member function returns an iteration pointer to the newly inserted element, that is, iterator (multimap: insert () can always be successfully executed ). However, MAP: insert () returns pair <iterator, bool>. Here, the bool value indicates whether the insert operation is successful.

 

Search for a single value

Similar to map, multimap has two versions of the reloaded find () member function:

Iterator find (const key_type & K );

Const_iterator find (const key_type & K) const; find (k) returns the iteration pointer pointing to the first pair matching the key K, that is, this function is most useful when you want to check whether there is at least one value associated with the key, or if you only need the first matching. For example:

Typedef multimap <string, string> MMSs;

Void func (const MMSs & DNS)

{

MMSs: const_iterator CIT = DNS. Find ("213.108.96.7 ");

If (CIT! = DNS. End ())

Cout <"213.108.96.7 found" <Endl;

Else

Cout <"not found" <Endl;

}

Process multiple associated values

The count (k) member function returns the number of values associated with the given key. The following example shows how many values are associated with the key "213.108.96.7:

Cout <DNS. Count ("213.108.96.7") // output: 2

<"Elements associated" <Endl; to access multiple values in multimap, use the member functions of performance_range (), lower_bound (), and upper_bound:

Performance_range (k): This function searches for all values associated with K. Returns the pair of the iteration pointer, marking the start and end ranges. The following example shows all values associated with the key "213.108.96.7:

Typedef multimap <string, string >:const_iterator cit;

Typedef pair <CIT, CIT> range;

Range = DNS. ipv_range ("213.108.96.7 ");

For (CIT I = range. First; I! = Range. Second; ++ I)

Cout <I-> second <Endl; // output: cpluspluszone.com

// Cppzone.com lower_bound () and upper_bound (): lower_bound (k) Find the first value associated with the key K, and upper_bound (k) is to find the element whose first key value is greater than K. The following example uses upper_bound () to locate the first element whose key value is greater than "213.108.96.7. Generally, when the key is a string, there will be a Dictionary Compilation comparison:

DNS. insert (make_pair ("219.108.96.70", "pythonzone.com "));

CIT = DNS. upper_bound ("213.108.96.7 ");

If (CIT! = DNS. End () // found anything?

Cout <cit-> second <Endl; // display: pythonzone.com if you want to display all the values, you can use the following loop:

// Insert multiple values with the same key

DNS. insert (make_pair ("219.108.96.70", "pythonzone.com "));

DNS. insert (make_pair ("219.108.96.70", "python-zone.com "));

// Obtain the iteration pointer of the first value

CIT = DNS. upper_bound ("213.108.96.7 ");

// Output: pythonzone.com, python-zone.com

While (CIT! = DNS. End ())

{

Cout <cit-> second <Endl;

++ Cit;

}

 

Conclusion

Although map and multimap have the same interface, the important difference lies in repeated keys. Design and use should be treated differently. Note the nuances of the insert () member function in each container.

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.