Broad Network


Map Modifiers in C++

Associative Container in C++ Simplified – Part 4

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

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

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

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.

The pair Object
Each element of a map is actually an object called the pair object. C++ has a pre-described class called the pair class. This class has its particular nature. The class allows you to group (instantiate) any two objects of the same type or of different types as a unit. All map elements are indirect instantiated objects of the pair class. The pair class can be got from the map header file. You can instantiate your own pair object independently of the associated container. The syntax is,

        pair<key, T> pairName(keyValue, TValue);

With this you can create one object (key/value pair) for a map and then insert the object into the map. The template arguments for the pair should be the same template arguments for the map and the same template arguments for the iterator, used by the map.

iterator insert(iterator position, const value_type& x);
This method inserts a pair object in a map, where the iterator is pointing to. There are two arguments, the first one is the iterator position. The second one is the pair object. Read and try the following code, which illustrates this:

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        pair<const char*, int> myPr("eee", 25);

        map<const char*, int> myMap;

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

        myMap.insert(iter, myPr);

        cout << myMap["eee"] << '\n';

        return 0;
    }

size_type erase(const key_type& x);
You can use the key of an element to erase the element completely. This is what this method does. Read and try the following code:

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aaa"] = 47;

        myMap.erase("aaa");

        return 0;
    }

The element with key, “aaa” is erased.

void erase(iterator position);
This erase method works like the above, but it takes an iterator as argument. Read and try the following code:

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aaa"] = 47;

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

        myMap.erase(iter);

        return 0;
    }

void erase(iterator first, iterator last);
This erases a consecutive set of elements. Read and try the following code:

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, int> myMap;
        myMap["aaa"] = 47;
        myMap["bbb"] = 58;
        myMap["ccc"] = 63;

        _Rb_tree_iterator<pair <const char* const, int> > iterFirst = myMap.begin();
        _Rb_tree_iterator<pair <const char* const, int> > iterLast = myMap.end();

        myMap.erase(iterFirst, iterLast);

        return 0;
    }

You can still use the same method to erase a range of elements within the map list.

void swap(map<Key,T,Compare,Allocator>&);
This method exchanges the content (list elements) of two maps. The two maps should be of the same type. The method takes the name of one map as argument. Read and try,

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, char> myMap;
        myMap["one"] = 'A';    
        myMap["two"] = 'B';      
        myMap["three"] = 'C';      
        myMap["four"] = 'D';      
        myMap["five"] = 'E';    

        map<const char*, char> hisMap;
        hisMap["aa"] = 'F';
        hisMap["bb"] = 'G';
        hisMap["cc"] = 'H';
  
        myMap.swap(hisMap);

        cout << myMap["aa"] << '\n';
        cout << myMap["bb"] << '\n';
        cout << myMap["cc"] << '\n';
        cout << '\n';

        cout << hisMap["one"] << '\n';
        cout << hisMap["two"] << '\n';
        cout << hisMap["three"] << '\n';  
        cout << hisMap["four"] << '\n';
        cout << hisMap["five"] << '\n';  

        return 0;
    }

void clear();
This method removes all the elements that are in the map list, making the size of the map zero. The following program illustrates the use of the clear method (nothing is displayed):

#include <iostream>
#include <map>

using namespace std;

int main()
    {
        map<const char*, char> myMap;
        myMap["one"] = 'A';    
        myMap["two"] = 'B';    
        myMap["three"] = 'C';    
        myMap["four"] = 'D';    
        myMap["five"] = 'E';    

        myMap.clear();

        return 0;
    }

That is what I have for Map Modifiers in C++. We continue in the next part of the series.

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