In this part of the series, we look at map capacity in C++.
Associative Container in C++ Simplified - Part 5
Division 1
Introduction
This is part 5 of my series, Associative Container in C++ Simplified. In this part of the series, we look at map capacity in C++. I assume that you have read all the prerequisites, and the previous parts of the series.
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.
size_type size() const;
This method returns the number of elements (length or size) in the map. size_type can be considered as an int. Try the following code:
#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';
int mSize = myMap.size();
cout << mSize;
return 0;
}
bool empty() const;
This method is used to test whether or not a map is empty. Empty means the map has no element. It returns true if there are no elements in the map, otherwise it returns false. Read and try the following two code samples:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<const char*, char> myMap;
if (myMap.empty())
{
cout << "The map has no element and it is empty.";
}
return 0;
}
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<const char*, char> myMap;
myMap["aa"] = 'A';
myMap["bb"] = 'B';
if (myMap.empty())
{
cout << "The map has no element and it is empty.";
}
else
{
cout << "The map has at least 1 element and it is not empty.";
}
return 0;
}
This is a short tutorial. We rest here and continue in the next part of the series.
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 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...
its an tutorial to make a virus /worm in C language...
cracking unix system password all about unix cracking it quite easily...
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....