Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an array in C++ using new and initialize each element?

Without iterating through each element, how do I create an array using new and initialize each element to a certain value?

bool* a = new bool[100000];

Using VS 2008.

Thanks!

like image 266
B Seven Avatar asked Jan 11 '11 05:01

B Seven


People also ask

How do you create and initialize an element in an array?

To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.

How do you initiate initialize an array in C?

Array Initialization Using a Loop The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C. // declare an array. int my_array[5];

How do I create a new array in C?

To create an array, define the data type (like int ) and specify the name of the array followed by square brackets []. To insert values to it, use a comma-separated list, inside curly braces: int myNumbers[] = {25, 50, 75, 100};

How do you initialize an array in New?

Initializing an array In order to store values in the array, we must initialize it first, the syntax of which is as follows: datatype [ ] arrayName = new datatype [size];


1 Answers

In addition to what GMan said above, I believe you can specify an initial value for each value in your vector on construction like this..

vector<bool> a (100000, true);
like image 157
rchanley Avatar answered Nov 04 '22 05:11

rchanley