Broad Network


Differences between Associative Containers in C++

Associative Container in C++ Simplified – Part 7

Forward: In this part of the series, I point out the main differences between the different associative containers in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 7 of my series, Associative Container in C++ Simplified. There are four basic types of associative containers, which are the map, set, multimap and multiset. We have seen the map in some detail. The other three associative containers are similar to the map. In this part of the series, I point out the main differences between the different associative containers in C++.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Groups of Associative Containers
The map and the multimap containers as you can judge from their names are of the same group. multimap is a variation of a map. The set and multiset associative containers are of another group. Multiset is a variation of a set.

The Different Associative Containers
The main difference between the map and the multimap is that in the map, the keys are unique, while in the multimap, the keys do not have to be unique. So in the multimap, you can have more than one key of the same value; such keys are called, Equivalent Keys.

The main difference between the set and the multiset is that in the set, the keys are unique, while in the multiset, the keys do not have to be unique. So multiset supports equivalent keys.

The main difference between the two groups is that, for the map group, the key and the value of each element are of different types; however in the set group the key and the value of each element are of the same type.

The pair class Revisited
Consider the following code segment:

        pair<const char*, int> myPr("eee", 25);
        cout << myPr.first << "\n";
        cout << myPr.second << "\n";

We have seen the pair class of the C++ standard template library (map and set headers) before. The two values of the instantiated pair object do not have to be of the same type. We are using the pair object in this series for the key and value of an associative container element. The interest here is to know how to read out the key and value from the pair object.

In the description of the pair class in the C++ standard library, the name of the pair property (data member) that identifies the key is, first. The name of the pair property that identifies the value is, second.

In the above segment, the pair class is from the map header file. The first statement in the segment instantiates the pair object. The arguments of the pair object are the key and value we want for an element of an associative container. The second statement reads out the key, using, first; and the third statement reads out the value using, second.

For the rest of this article, we shall look at code samples of the multimap, set and multiset associative containers. To have a map, you need to include the map header; to have a multimap, you still need to include the map header; for a set and a multiset, you need the set header. You must read and try all the code samples, below.

A multimap Code Sample
In the following program, the pair class is used to insert the elements into the multimap. If you dereference a returned iterator from a multimap element, you get the pair object.

#include <iostream>
#include <map>

using namespace std;

int main()
    {
     multimap<const char*, int> myMM;

        _Rb_tree_iterator<pair <const char* const, int> > iter = myMM.begin();

        pair<const char*, int> myPr1("aa", 10);
        myMM.insert(iter, myPr1);
        ++iter;
        pair<const char*, int> myPr2("bb", 20);
        myMM.insert(iter, myPr2);
        ++iter;
        pair<const char*, int> myPr3("bb", 30);
        myMM.insert(iter, myPr3);
        ++iter;
        pair<const char*, int> myPr4("cc", 40);
        myMM.insert(iter, myPr4);

        _Rb_tree_iterator<pair <const char* const, int> > iterOut = myMM.begin();

        pair<const char*, int> p1 = *iterOut;
        cout << p1.first <<", "<< p1.second << '\n';
        ++iterOut;
        pair<const char*, int> p2 = *iterOut;
        cout << p2.first <<", "<< p2.second << '\n';
        ++iterOut;
        pair<const char*, int> p3 = *iterOut;
        cout << p3.first <<", "<< p3.second << '\n';
        ++iterOut;
        pair<const char*, int> p4 = *iterOut;
        cout << p4.first <<", "<< p4.second << '\n';

        return 0;
    }

You can use the same type of iterator for map and multimap.

A Set Code Sample
- I will continue the rest of the tutorial later . . .



Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message