Broad Network


Vector Capacity in C++

Container Library Sequences in C++ Simplified – Part 8

Forward: In this part of the series, we talk about some vector methods that are related to the vector size.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is the part 8 of my series, Containers Library Sequences in C++, Simplified. In this part of the series, we talk about some vector methods that are related to the vector size. I assume that you have read the previous parts of this 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 vector. size_type can be considered as an int. Try the following code:

#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<char> myVtor(5);
        myVtor[0] = 'A';    
        myVtor[1] = 'B';    
        myVtor[2] = 'C';    
        myVtor[3] = 'D';    
        myVtor[4] = 'E';    

        int vSize = myVtor.size();

        cout << vSize;

        return 0;
    }

void resize(size_type sz, T c = T());
This method changes the size of the vector. The first argument is the new size (int) you want. If the requested size is higher than the current size, then the second argument is the value (object) of the element type that will fill the cells added to the end of the vector list. If the new size is smaller than the current size, then the last elements are removed; in this case, the second argument is not needed. If you are increasing the size and you do not type the second argument, you would have default values for the added elements. Read and try the following two code samples:

#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<int> myVtor(2);
        myVtor[0] = 5;    
        myVtor[1] = 16;      

        myVtor.resize(4, 0);

        cout << myVtor[0] << '\n';
        cout << myVtor[1] << '\n';
        cout << myVtor[2] << '\n';
        cout << myVtor[3] << '\n';

        return 0;
    }


#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<char> myVtor(5);
        myVtor[0] = 'A';    
        myVtor[1] = 'B';  
        myVtor[2] = 'C';    
        myVtor[3] = 'D';  
        myVtor[4] = 'E';    


        myVtor.resize(3);

        cout << myVtor[0] << '\n';
        cout << myVtor[1] << '\n';
        cout << myVtor[2] << '\n';

        return 0;
    }

bool empty() const;
This method is used to test whether or not a vector is empty. Empty means the vector has no element. It returns true if there are no elements in the vector, otherwise it returns false. Read and try the following two code samples:

#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<char> myVtor;

        if (myVtor.empty())
            {
                cout << "The vector has no element and it is empty.";
            }

        return 0;
    }


#include <iostream>
#include <vector>

using namespace std;


int main()
    {
        vector<char> myVtor;

        myVtor.push_back('A');
        myVtor.push_back('B');

        if (myVtor.empty())
            {
                cout << "The vector has no element and it is empty.";
            }
        else
            {
                cout << "The vector has at least 1 element and it is not empty.";
            }

        return 0;
    }

We have seen three methods. Well, let us end 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