In this part of the series, we look at map operations in C++.
Associative Container in C++ Simplified - Part 6
Division 1
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
To arrive at any of the parts of this series, just type the corresponding title below and my name, Chrys, in the Search Box of this page and click Search (use menu if available):
Map Associative Container in C++
Map Element Access in C++
Map Methods Returning Iterators in C++
Map Modifiers in C++
Map Capacity in C++
Map Operations in C++
Differences between Associative Containers in C++
Written by Chrys
In this article I introduce you to a tutorial series titled, Some Features of C++ Entities....
C++ is a computer language I want to teach in these tutorials. C++ is a very developed language...
In C++ an array is a set of consecutive objects of the same type, in memory. We see how to crea...
A database is a set of related tables. This is part 1, division 1 of a series I have on databas...
To easy way lean java programming language with a basic concepts which will help to build basic...
A database is a set of related tables. This is part 1, division 1 of a series I have on databas...
In C++ an array is a set of consecutive objects of the same type, in memory. We see how to crea...
C++ is a computer language I want to teach in these tutorials. C++ is a very developed language...
In this article I give you reasons why website hosting has become cheap....
In this article I explain different free software types....