advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 3 users have rated this item.
Use multimap to Create Associative Containers with Duplicate Keys (cont'd)
Finding a Single Value
multimap, like map, has two overloaded versions of the find() member function:

iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
advertisement
find(k) returns an iterator to the first pair that matches the key k. This means it's mostly useful when you want to check whether there is at least one value associated with the key or when you need only the first match. 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;
}
Handling Multiple Associated Values
The count(k) member function returns the number of values associated with a given key. The following example reports 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 a multimap, use the equal_range(), lower_bound() and upper_bound() member functions:
  • equal_range(k): This function finds all of the values associated with the key k. This function returns a pair of iterators that mark the beginning and the end of the range. The following example displays all the values associated with the key 213.108.96.7:
    
    typedef multimap <string, string>::const_iterator CIT;
    typedef pair<CIT, CIT> Range;
    Range range=dns.equal_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) finds the first value associated with the key k, and upper_bound(k) finds the first element whose key is greater than k. The following example uses upper_bound() to locate the first element whose key is greater than 213.108.96.7. As usual, when the key is of type string a lexicographical comparison takes place:
    
    dns.insert(make_pair("219.108.96.70",
    "pythonzone.com"));
    CIT cit=dns.upper_bound("213.108.96.7");
    if (cit!=dns.end()) //found anything?
    cout<<cit->second<<endl; //display: pythonzone.com
    
    If you wish to display all the subsequent values, use a loop like this:
    
    //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"));
    //get an iterator to the first value
    CIT cit=dns.upper_bound("213.108.96.7");
    //output: pythonzone.com python-zone.com
    while(cit!=dns.end())
    {
    cout<<cit->second<<endl;
    ++cit;
    }
    
Concept Mapping
Although map and multimap have similar interfaces, the crucial difference between the two, with respect to duplicate keys, entails different design and usage. In addition, you should be aware of the subtle differences between the insert() member function of each container.

Previous Page: Presenting the Problem  


Danny Kalev is a certified system analyst and software engineer specializing in C++ and theoretical aspects of formal languages. He was a member of the C++ standards committee between 1997 and 2000. He recently finished his MA in general linguistics summa cum laude. In his spare time he likes to listen to classical music, read Victorian literature and explore natural languages such as Hittite, Basque and Irish Gaelic. Additional interests include archeology and geology. Danny moderates several C++ forums and contributes regularly to various C++-related sites and magazines. He also gives lectures about programming languages and applied linguistics at academic institutes.
Page 1: IntroductionPage 3: Finding a Single Value
Page 2: Presenting the Problem 
Please rate this item (5=best)
 1  2  3  4  5
advertisement