This tutorial will discuss how to create a vector object in C++.

To create a vector object, first, we need to include the vector header file. Like this,

#include <vector>

Then, we need to decide what type of vector we want to create. It means determining the type of elements the vector object is going to hold. That data type needs to be passed as a template parameter while creating a vector object.

std::vector<data_type> vecObj;

For instance, if we want to create a vector of integers, then we need to pass “int” as the data type. If we want to create a vector of strings, then we need to pass “string” as the template parameter.

Like this:

std::vector<std::string> vecObj;

It will create a vector of strings. Now, if you want to create a vector of integers, you need to pass “int” as the template parameter:

std::vector<int> numbers;

In the example below, we will create a vector of integers. We then call the size() function of the vector, which returns the number of elements in the vector. As we created an empty vector (and neither initialized it with any elements nor inserted any elements into it), this vector has no elements inside. Thus, the size() function will return zero.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    // Creating an empty vector
    std::vector<int> vecObj;

    std::cout << "Number of Elements in Vector: " << vecObj.size() << "n";

    return 0;
}

Output

Number of Elements in Vector: 0

Now Let’s discuss about unique ways to create a vector with default value in C++.

Create an empty vector and add N default values

In this solution, we will create an empty vector, and then we will fill N default values in it using the STL Algorithm std::fill(). It is a little less efficient solution. But it is always good to know the different kind of techniques.

Let’s see the complete example,

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{

    // Create an vector of size 10
    std::vector<int> vecObj(10);

    int defaultValue = 99;

    // Replace all values in vector with the default value
    std::fill (vecObj.begin(), vecObj.end(), defaultValue);

    // print all elements of vector
    for(auto elem : vecObj) {
        std::cout<<elem << ", ";
    }
    std::cout<< std::endl;


    return 0;
}

Output :

99, 99, 99, 99, 99, 99, 99, 99, 99, 99,

Create & Initialize Vector using constructor

In one of the overloaded implementation of vector constructor, it accepts two arguments,

  • N: Number of elements
  • default_value: Defaul value of elements

It creates a vector object with N elements, and each element will have the given default value.

In this example, we will create a vector with 10 integers, and each value will be 99.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    int defaultValue = 99;

    // Create an vector of size 10 and
    // with default value 99
    std::vector<int> vecObj(10, defaultValue);

    // print all elements of vector
    for(auto elem : vecObj) {
        std::cout<<elem << ", ";
    }
    std::cout<< std::endl;

    return 0;
}

Output :

99, 99, 99, 99, 99, 99, 99, 99, 99, 99,

Create & Initialize Vector with Hard coded default Values

Suppose we want to create a vector and initialize it with some hardcoded values. We can do that using an initializer list.

For instance, in the following line, we will create a vector of integers and initialize it with 5 different integer values.

std::vector<int> vecObj {10, 20, 30, 40, 50};

To do this, we pass the default values in curly braces while creating the vector object. Thus, our vector will be initialized with the given elements in the initializer list. If we then call the size() function of the vector, it will return 5, as we initialized our vector with 5 distinct elements.

In the example below, we create a vector and initialize it with 5 different integers. We then print its size, and use a range-based for loop to iterate over all its elements. The iteration of elements in the vector will be discussed in different articles.

std::vector<int> vecObj {10, 20, 30, 40, 50};

std::cout << "Size of Vector: " << vecObj.size() << std::endl;

for (const auto &num : vecObj)
{
    std::cout << num << ", ";
}

Output:

10, 20, 30, 40, 50,

This code initializes a vector with 5 integers, prints its size, and then iterates over its elements to print each one.

Let’s see the complete example,

#include <iostream>
#include <vector>

int main()
{
    // Creating a vector with an initializer list.
    std::vector<int> vecObj {10, 20, 30, 40, 50};

    std::cout << "Size of Vector: " << vecObj.size() << std::endl;

    std::cout << "Contents of Vector: ";
    for (const auto &num : vecObj)
    {
        std::cout << num << ", ";
    }
    std::cout << "n";

    return 0;
}

Output

Size of Vector: 5
Contents of Vector: 10, 20, 30, 40, 50,

Summary

Today we learned about several ways to create a vector with default value in C++. Thanks.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.