I know how to create array Dynamically, but I wants to initialization of every element with negative one (-1)
Asked
Active
Viewed 74 times
-1
-
3Remove one of the language tags so we'll know if you want an answer for C or for C++. – Matti Virkkunen Sep 30 '14 at 18:20
2 Answers
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