Broad Network


Map Operations in C++

Associative Container in C++ Simplified – Part 6

Forward: In this part of the series, we look at map operations in C++.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is part 6 of my series, Associative Container in C++ Simplified. In this part of the series, we look at map operations in C++. I assume that you have read all the prerequisites, and the previous parts of the series.

Let us make the distinction between key_type and value_type. key_type is the object type for the key. value_type is the object (type) for the pair, which has the key and value.

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.

iterator find(const key_type& x);
The argument to this method is the key. The method looks for the position of the key in the map and returns the iterator for the element that has the key. Read and try the following code (that does not display anything).

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aa"] = 10;
        myMap["bb"] = 20;
        myMap["cc"] = 30;
        myMap["dd"] = 40;
        myMap["ee"] = 50;

        _Rb_tree_iterator<pair <const char* const, int> > iter = myMap.find("dd");

        return 0;
    }

const_iterator find(const key_type& x) const;
Same as above but returns an iterator that is constant. Try,

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aa"] = 10;
        myMap["bb"] = 20;
        myMap["cc"] = 30;
        myMap["dd"] = 40;
        myMap["ee"] = 50;

        const _Rb_tree_iterator<pair <const char* const, int> > iter = myMap.find("dd");

        return 0;
    }

bool operator==(const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
If the map x and the map y have the same size and the corresponding elements are equal (keys and values), then the == operator returns true; otherwise it returns false. Try,

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aa"] = 10;
        myMap["bb"] = 20;

        map<const char*, int> herMap;
        herMap["aa"] = 10;
        herMap["bb"] = 20;

        if (myMap == herMap)
            {
                cout << "The two maps are equal.";
            }

        return 0;
    }

bool operator!=(const map<Key,T,Compare,Allocator>& x, const map<Key,T,Compare,Allocator>& y);
The != operator is the opposite of ==. Read and try the following code, which illustrates it:

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aa"] = 10;
        myMap["bb"] = 20;

        map<const char*, int> herMap;
        herMap["aa"] = 10;
        herMap["bb"] = 35;

        if (myMap != herMap)
            {
                cout << "The two maps are not equal.";
            }
        else
            {
                cout << "The two maps are equal.";
            }

        return 0;
    }

For this simple tutorial, let us end here. We continue in the next part, in a new division.

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