How do you initialize a vector in a class in C++?

// Method One
class ClassName
{
public:
    ClassName() : m_vecInts() {}

private:
    std::vector<int> m_vecInts;
}

// Method Two
class ClassName
{
public:
    ClassName() {} // do nothing

private:
    std::vector<int> m_vecInts;
}

Question> What is the correct way to initialize the vector member variable of the class? Do we have to initialize it at all?

Drew Noakes

289k160 gold badges662 silver badges727 bronze badges

asked Jul 30, 2012 at 16:16

3

See http://en.cppreference.com/w/cpp/language/default_initialization

Default initialization is performed in three situations:

  1. when a variable with automatic storage duration is declared with no initializer
  2. when an object with dynamic storage duration is created by a new-expression without an initializer
  3. when a base class or a non-static data member is not mentioned in a constructor initializer list and that constructor is called.

The effects of default initialization are:

  • If T is a class type, the default constructor is called to provide the initial value for the new object.
  • If T is an array type, every element of the array is default-initialized.
  • Otherwise, nothing is done.

Since std::vector is a class type its default constructor is called. So the manual initialization isn't needed.

answered Jul 30, 2012 at 16:19

ZetaZeta

101k13 gold badges191 silver badges231 bronze badges

3

It depends. If you want a size 0 vector, then you don't have to do anything. If you wanted, say, a size N vector fill of 42s then use the constructor initializer lists:

ClassName() : m_vecInts(N, 42) {}

answered Jul 30, 2012 at 16:18

juanchopanzajuanchopanza

219k31 gold badges390 silver badges464 bronze badges

Since C++11, you can also use list-initialization of a non-static member directly inside the class declaration:

class ClassName
{
public:
    ClassName() {}

private:
    std::vector<int> m_vecInts {1, 2, 3}; // or = {1, 2, 3}
}

answered Sep 16, 2019 at 17:37

mrtsmrts

14.5k6 gold badges83 silver badges67 bronze badges

2

You do not have to initialise it explcitly, it will be created when you create an instance of your class.

answered Jul 30, 2012 at 16:18

When you're working with a collection of variables or data in programming, you usually store them in specific data types.

In C++, you can store them in arrays, structures, vectors, strings and so on. While these data structures have their distinctive features, we'll focus mainly on the various methods of initializing vectors.

Before that, let's talk briefly about vectors and what makes them stand out when dealing with data collections in C++.

What are Vectors in C++?

Unlike arrays in C++ where the memory allocated to the collection is static, vectors let us create more dynamic data structures.

Here's an array in C++:

#include <iostream>
using namespace std;

int main() {
    string names[2] = {"Jane", "John"};
    cout << names[1];
    // John
}

The array in the code above was created and allocated space enough to contain only two items. Attempting to assign new values through a new index would throw an error our way.

With vectors, things are a bit different. We don't have to specify the vector's capacity when it's defined. Under the hood, the vector's allocated memory changes as the size of the vector changes.

Syntax for Vectors in C++

Declaring a vector is different from initializing it. Declaring a vector means creating a new vector while initializations involves passing items into the vector.

Here's what the syntax looks like:

vector <data_type> vector_name

Every new vector must be declared starting with the vector keyword. This is followed by angle brackets which contain the the type of data the vector can accept like strings, integers, and so on. Lastly, the vector name - we can call this whatever we want.

Note that you must put include <vector> at the top of your file to be able to use vectors.

In this section, we'll go over the different ways of initializing a vector in C++. We'll divide them into sub-sections with some examples for each sub-section.

Let's start with the most basic.

How to Initialize a Vector in C++ Using the push_back() Method

push_back() is one out of the many methods you can use to interact with vectors in C++. It takes in the new item to be passed in as a parameter. This allows us to push new items to the last index of a vector.

Here's an example:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> myVector;

	myVector.push_back(5);
	myVector.push_back(10);
	myVector.push_back(15);

	for (int x : myVector)
		cout << x << " ";
		// 5 10 15 
}

In the code above, we created an empty vector: vector<int> myVector;.

Using the push_back(), we passed in three new numbers to the vector.

We the looped through these new numbers and logged them out to the console.

How to Initialize a Vector When Declaring the Vector in C++

Just like arrays, we can assign values to a vector when it is being declared.

Here's an example:

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> myVector{ 5, 10, 15 };

	for (int x : myVector)
		cout << x << " ";
		// 5 10 15 
}

In this example, both declaration and initialization were done at the same time.

At the point of declaring the vector, we passed in three numbers and then looped through and printed them out.

You'll notice that I put int between the angle brackets. This is to show that the data the vector will hold is specifically integers.

How to Initialize a Vector From an Array in C++

In this section, we'll first create and initialize an array. Then we'll copy all the items in the array into our vector using two vector methods – begin() and end().

Let's see how that works.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int myArray[] = { 5, 10, 15 };
    
    vector<int> myVector(begin(myArray), end(myArray));


	for (int x : myVector)
		cout << x << " ";
		// 5 10 15 
}

We can also initialize a vector from another vector using the same methods above. You'll have to define the first vector then use the begin() and end() methods to copy its values into the second vector.

How to Initialize a Vector by Specifying the Size and Value in C++

We can specify the size and items of a vector during its declaration. This is mainly in situations where the vector is required to have a specific value all through.

Here's an example:

#include <iostream>
#include <vector>
using namespace std;

int main() {

  int num_of_items = 5; 
  
  vector<int> myVector(num_of_items, 2); 
  
  	for (int x : myVector)
		cout << x << " ";
// 		2 2 2 2 2 
}

In the code above, we first defined a variable and passed a value of 5 to it. This acts as the maximum number of values the vector will have.

We then declared our vector: vector myVector(num_of_items, 2);. The first parameter is the maximum number of items variable while the second parameter is the actual item to be stored in the vector.

We looped through and logged out the items in the vector. We got 2 printed five times.

How to Initialize a Vector Using a Constructor in C++

We can also initialize vectors in constructors. We can make the values to be a bit dynamic. This way, we don't have to hardcode the vector's items.

Here's an example:

#include <iostream>
#include <vector>
using namespace std;

class Vector {
	vector<int> myVec;

public:
	Vector(vector<int> newVector) {
	    myVec = newVector;
	}
	
	void print() {
		for (int i = 0; i < myVec.size(); i++)
			cout << myVec[i] << " ";
	}
	
};

int main() {
    vector<int> vec;
    
	vec.push_back(5);
	vec.push_back(10);
	vec.push_back(15);
	
	Vector vect(vec);
	vect.print();
	// 5 10 15 
}

Let's break the code down.

class Vector {
	vector<int> myVec;

public:
	Vector(vector<int> newVector) {
	    myVec = newVector;
	}
	
	void print() {
		for (int i = 0; i < myVec.size(); i++)
			cout << myVec[i] << " ";
	}
	
};

We created a class called Vector. Then we created a vector variable called myVec.

After that, we defined our constructor. The constructor has two methods – one that takes in an initialized vector and another that prints out the items in the vector.

int main() {
    vector<int> vec;
    
	vec.push_back(5);
	vec.push_back(10);
	vec.push_back(15);
	
	Vector vect(vec);
	vect.print();
	// 5 10 15 
}

Lastly, as you can see in the code above, we created a new vector and pushed in new items to it. We then went on to log these items to the console.

The logic in main was created in the Vector class which also has a constructor.

Conclusion

In this article, we talked about vectors in C++.

We started by differentiating arrays and vectors. Arrays have a static size while vectors are more dynamic and can expand as items are added.

We then went over a few methods which we can use to initialize a vector in C++ with examples for each section.

Happy coding!

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

How do you initialize a vector in class?

How to initialize a vector in C++.
Pushing the values one-by-one. All the elements that need to populate a vector can be pushed, one-by-one, into the vector using the vector class method​ push_back . ... .
Using the overloaded constructor of the vector class. ... .
Using arrays. ... .
Using another, already initialized, vector..

What is the correct way to initialize vector in?

Begin Declare v of vector type. Call push_back() function to insert values into vector v. Print “Vector elements:”. for (int a : v) print all the elements of variable a.

How do you initialize a vector of an object?

How to Initialize a Vector in C++ Using the push_back() Method. push_back() is one out of the many methods you can use to interact with vectors in C++. It takes in the new item to be passed in as a parameter. This allows us to push new items to the last index of a vector .

Does std::vector need to be initialized?

when you create a vector it gets default initialized, so it's up to you if you want to initialize it with user default values or not. You will always get an empty vector container if you don't initialize it.