Broad Network


Map Element Access in C++

Associative Container in C++ Simplified – Part 2

Forward: In this part of the series, we see how to access the value of an element in a map.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is part 2 of my series, Associative Container in C++ Simplified. In this part of the series, we see how to access the value of an element in a map.

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.

In this series, the discussion of an operator or method is begun by typing its syntax.

T& operator[](const key_type& x);
This is the [] operator having a slightly different meaning to the one of the array. You use it as you use the one for the array. There is an advantage in its use here in the sense that you can use it to add an element at the end of the map. Note, when a map is empty, its end is the beginning. The following program illustrates the use of the operator:

#include <iostream>
#include <map>

using namespace std;


int main()
    {
        map<const char*, int> myMap;

        myMap["one"] = 10;
        myMap["two"] = 20;
        myMap["three"] = 20;

        cout << myMap["one"] << '\n';
        cout << myMap["two"]  << '\n';
        cout << myMap["three"]  << '\n';

        return 0;
    }

The Keys and Values
The key is generally a word. In the above program, there are three keys, which are “one”, “two” and “three”. You can use any word of your choice for a key. Each word is inside square brackets in quotes. This corresponds to the first template argument, which is, const char*. There are three elements, each consisting of a key and a corresponding value. All the values are ints as indicated by the second template argument. The value for the key, “one” is 10, that for “two” is 20 and that for “three” is 30.

In the above program, the following code segment adds three elements (key/value pairs) to the map.

        myMap["one"] = 10;
        myMap["two"] = 20;
        myMap["three"] = 20;

The square brackets have been used as with an array, but with the key instead of index. In the above program, the following segment returns the three values from the three elements in the map:

        cout << myMap["one"] << '\n';
        cout << myMap["two"]  << '\n';
        cout << myMap["three"]  << '\n';

The values are returned similar to the way they are returned with arrays. However, instead of using index, you use word. The returned values can be assign to object identifiers as in the following code segment:

    int value1 = myMap["one"];
    int value2 = myMap["two"];
    int value3 = myMap["three"];

Unique Keys
No two keys should be the same with the map, which supports unique keys.

That is all there is to accessing map elements. This is a good place to take a break. We continue in the next part.

Chrys

Related Courses

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

Comments

Become the Writer's Fan
Send the Writer a Message