In this article we will discuss different ways to initialize a std::vector in C++.

Initializing std::vector elements with same value

std::vector<int> vec_1(5, 10);

It will create a vector of 5 element and initialize  each element is initialized to 10.

Initializing std::vector elements with passed argument list

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

It will create a vector of 5 element with given values. Will work with C++11 only.

Initializing std::vector from an array

int arr[] = {1,2,3,4,5,6,7,8,9};

std::vector<int> vec_3(arr+ 1, arr+5);

It will initialize a vector through a range in array  or any other vector.

Initializing std::vector with Custom Calculated Values using std::generate

Suppose we want to initialize a vector with odd values from a given range only. This kind of functionality can be achieved using STL Algorithm std::generate and any user specific Function Object.

 

Let’s first create a function object that will give the next Odd number from a given range in each call.

class NumberGenerator
{
private:
	int m_start;
	int m_end;
	bool m_isOdd;
	int m_last;
public:
	NumberGenerator(int start, int end, bool isOdd) :
			m_start(start), m_end(end), m_isOdd(isOdd)
	{
		m_last = m_start - 1;
	}
	int operator()()
	{
		while(m_last != m_end)
		{
			m_last++;
			if(m_isOdd && (m_last % 2 == 1))
				return m_last;
			if(!m_isOdd && (m_last % 2 == 0))
				return m_last;
		}
	}
};

Now lets use this Function Object with STL Algorithm std::generate to initialize the values of a vector.
std::vector<int> vec_4(5);
std::generate(vec_4.begin(),vec_4.end(), NumberGenerator(10,100,true));

Complete Working Code is as follows,
#include <iostream>
#include <vector>
#include <algorithm>

void displayVector(const std::vector<int> &vec)
{
	for(auto x : vec)
		std::cout<<x<<" , ";
	std::cout<<std::endl;
}

class NumberGenerator
{
private:
	int m_start;
	int m_end;
	bool m_isOdd;
	int m_last;
public:
	NumberGenerator(int start, int end, bool isOdd) :
			m_start(start), m_end(end), m_isOdd(isOdd)
	{
		m_last = m_start - 1;
	}
	int operator()()
	{
		while(m_last != m_end)
		{
			m_last++;
			if(m_isOdd && (m_last % 2 == 1))
				return m_last;
			if(!m_isOdd && (m_last % 2 == 0))
				return m_last;
		}
	}
};
int main()
{
	// Will create a vector of 5 element and
	// each element is initialized to 10.

	std::vector<int> vec_1(5, 10);

	displayVector(vec_1);


	// Will create a vector of 5 element with
	// given values. Will work with C++11 only.
	std::vector<int> vec_2 = {10,20,30,40,50};

	displayVector(vec_2);


	//Initialize a vector through a range in array
	// or any other vector

	int arr[] = {1,2,3,4,5,6,7,8,9};

	std::vector<int> vec_3(arr+ 1, arr+5);
	displayVector(vec_3);

	// Initializing vector based on some logic and calculation
	// Like initializing vector with odd values from a given range only
	// This kind of functionality can be achieved using STL Algorithm std::generate
	// and any user specific Function Object.

	std::vector<int> vec_4(5);
	std::generate(vec_4.begin(),vec_4.end(), NumberGenerator(10,100,true));

	displayVector(vec_4);


}


To compile the above code use following command,

g++ –std=c++11 vec_init.cpp

Output:

10 , 10 , 10 , 10 , 10 ,
10 , 20 , 30 , 40 , 50 ,
2 , 3 , 4 , 5 ,
11 , 13 , 15 , 17 , 19 ,