-1

I know how to create array Dynamically, but I wants to initialization of every element with negative one (-1)

2 Answers2

4

std::vector accepts a second parameter for the value to use to initialize elements:

std::vector<int> mydata(1000, -1); // 1000 elements, each initialized to -1
6502
  • 112,025
  • 15
  • 165
  • 265
3

std::vector has a constructor that takes a size and a value to use as an initializer:

// create a vector of 10 integers with the value -1
std::vector<int> v(10, -1);
Chad
  • 18,706
  • 4
  • 46
  • 63