Map Capacity in C++

Sep 18th, 2010 by Chrys

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++
 

Chrys

Written by Chrys

Rate this Article:

Be the first to rate me.

Add new comment

* You must be logged in order to leave comments, please Sign in or join us.

Comments

No comments yet, be the first to comment on this article.

Chrys has 531 articles online

Related Content