Initialize a Vector in C++

Filed Under: C++
Initialize Vector Cpp

In this article, we’ll take a look at some of the ways to initialize a vector in C++. There are a bunch of ways to do this, so we’ll go through each approach.

Let’s get started!


Method 1: (Recommended): Use an Initializer List (C++11 and above)

If your compiler supports the C++ version above C++11, you can simply initialize the vector using the {} notation.

Since std::vector is a class, to initialize an object of this class using the above fashion, this refers to an Initializer List in C++.

Here is an example using the initializer list declaration:

std::vector<int> vec = { 1, 2, 3, 4, 5 };

Since initializer lists were introduced only in C++11, you need a minimum compiler version supporting at least C++11 to use this method.

Here is an example program to demonstrate this type of initialization:

#include <iostream>
#include <vector>

int main() {
    // Initialize a vector<int> using Initializer Lists
    std::vector<int> vec = { 1, 2, 3, 4, 5 };
    
    // Use a range based for loop to print elements of the vector
    for (const auto &i: vec) {
        // Using access by reference to avoid copying
        // Using const since we're not modifying elements of the vector
        std::cout << i << std::endl;
    }

    return 0;
}

Here, we use a range-based for-loop to print the vector elements.

Output

1
2
3
4
5

Method 2: Initialize a Vector in C++ with the help of an array (C++0x)

If you’re using a C++0x (C++03, C++07, etc) based compiler, you can still achieve the initialization using arrays.

int tmp[] = { 1, 2, 3, 4, 5 };
std::vector<int> vec( tmp, tmp + sizeof(tmp)/sizeof(tmp[0]) );

Test this on a C++0x based compiler, to verify that it works.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    // Initialize a vector<int> using Arrays
    int tmp[] = { 1, 2, 3, 4, 5 };
    std::vector<int> vec(tmp, tmp + sizeof(tmp)/sizeof(tmp[0]));
    
    // Range based for loops not supported in older compilers!
    // Nor is auto!
    for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << std::endl;
    }

    return 0;
}

If you’re on Linux (g++ based compiler), compile and run using:

gcc -o test.out test.cpp -std=c++0x
./test.out

Output

1
2
3
4
5

Method 3: Using the <boost> library (C++11 and above)

We can also use the <boost> library to achieve this.

The <boost/assign> namespace has the list_of construct to initialize vectors.

#include <boost/assign/list_of.hpp>

// Initialize a vector of {1, 2, 3, 4, 5}
std::vector<int> vec = boost::assign::list_of(1)(2)(3)(4)(5);

We can also use the overloaded + operator to declare a vector and add elements.

// Courtesy: https://stackoverflow.com/a/2236233

#include <boost/assign/list_of.hpp>

// Declare a vector
std::vector<int> vec;

// Add elements to it
vec += 1, 2, 3, 4, 5;

However, this type of operator overloading is not advisable, as it can be confusing for many readers, as a person reading this code may also think that the first element will add to 1, the second element adding to 2, etc.


Conclusion

In this article, we learned how we could initialize a vector in the C++ language in different ways.


References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages