174

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?

pyCthon
  • 11,746
  • 20
  • 73
  • 135

4 Answers4

335

You don't need initialization lists for that:

std::vector<int> vector1(length, 0);
std::vector<double> vector2(length, 0.0);
ronag
  • 49,529
  • 25
  • 126
  • 221
  • 66
    You don't need to explicitly state the 0 do you? just `vector(length)` should work? – daniel gratzer Oct 28 '12 at 15:29
  • 84
    @jozefg: Yes, it would work, though there is no harm in being explicit about what you want the code to do. – ronag Oct 28 '12 at 15:30
  • 1
    @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. – Johannes Schaub - litb Oct 28 '12 at 15:32
  • Is it possible to re-initialized Global Vector's all element with`ZERO` with efficiently without using for loops?????? – Ankit Mishra Jul 10 '20 at 05:39
  • Well, I just tried exactly that, and the resulting vector is full of garbage values : `std::vector inputBuffer(totalSize, 0);` – Cyan Mar 31 '21 at 17:42
  • 3
    @AnkitMishra Yes, use `std:fill(vector2.begin(), vector2.end(), 0)` – TYeung Aug 31 '21 at 05:11
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);
Peter
  • 1,124
  • 14
  • 17
  • 7
    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). – Alexander Malakhov Feb 20 '21 at 14:38
  • 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. – underscore_d Nov 29 '21 at 11:10
0

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 << " ";
    }
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
-5

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};