190

I know in C++11 they added the feature to initialize a variable to zero as such

double number = {}; // number = 0 int data{}; // data = 0 

Is there a similar way to initialize a std::vector of a fixed length to all zero's?

3
  • 24
    std::vector<int> vec(42); Commented Oct 28, 2012 at 15:27
  • 12
    btw, this is not new in c++11, you can do this in previous versions as well Commented Oct 28, 2012 at 15:58
  • Related: stackoverflow.com/q/5922797. Commented Dec 30, 2020 at 10:54

4 Answers 4

360

You don't need initialization lists for that:

std::vector<int> vector1(length, 0); std::vector<double> vector2(length, 0.0); 
Sign up to request clarification or add additional context in comments.

6 Comments

You don't need to explicitly state the 0 do you? just vector(length) should work?
@jozefg: Yes, it would work, though there is no harm in being explicit about what you want the code to do.
@ronag i wrote my own answer because being explicit in this case is not necessarily a good example if int is not the type he is working with. gereral programming is available in c++ but when i was reminded that you can omit the argument altogether, i removed my answer.
Is it possible to re-initialized Global Vector's all element withZERO with efficiently without using for loops??????
@AnkitMishra Yes, use std:fill(vector2.begin(), vector2.end(), 0)
|
6

With recent versions of c++ you can go with std::fill.

I noticed someone mentioned it as comment. But should be an answer and encourage to use standard library algorithms which are mentioned by experts, very well tested and proven.

 std::vector<int> vecOfInts; vecOfInts.resize(10); std::fill(vecOfInts.begin(), vecOfInts.end(), 0); for (auto const& intVal : vecOfInts) { std::cout << intVal << " "; } 

4 Comments

What's putting 10 values in your vector is the resize method, not the fill which just replaces existing values.
The OP specifies "fixed length" in his question. 10 is an example.
This is a method to assign a vector to 0's, but the OP specified 'initialize'.
You needn't fill. vecOfInts.resize(10, 0) does the job
4

Initializing a vector having struct, class or Union can be done this way

std::vector<SomeStruct> someStructVect(length); memset(someStructVect.data(), 0, sizeof(SomeStruct)*length); 

2 Comments

This is OK for built-in types (int etc) and PODs, but will bring huge problems for classes with either 1. virtual methods (because vtable pointer will be set to 0) or 2. invariant that some field is not 0 (which is usually enforced by constructors and methods).
As well as being dangerous for anything except trivial types, what you wrote is pointless: constructing the vector with size length already default-initialises all of the new elements. If it were needed to default-initialise all elements later, or to set them all to some other value, std::fill() should be used because it's actually C++ and type-safe.
-9

For c++: Let's say that the vector has a maximum of 100 int elements. You can initialize it this way:

int vector[100]={0};

2 Comments

That is NOT a vector.
More importantly, you actually get zero initialization for free with arrays. Try int array[100]; — it's zero-filled.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.