Broad Network


Map Capacity in C++

Associative Container in C++ Simplified – Part 5

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

By: Chrysanthus Date Published: 24 Aug 2012

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

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