Johnny
Johnny

Reputation: 1983

Fill A Variable Size Array

int* HT;
int HTc = 500;
HT = new int[HTc] = {-1}; //Fill array with -1

I get the warning:

extended initializer lists only available with -std=c++0x or =std=gnu++0x

I'll assume this means it isn't compatible with the ANSI standard, which my prof. is nuts for. How else would I do this though?

Upvotes: 1

Views: 467

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490138

Unless you have some truly outstanding reason to do otherwise, the preferred method would be to not only use a vector, but also specify the initial value when you invoke the ctor: std::vector<int> HT(500, -1);

Upvotes: 2

Todd Gardner
Todd Gardner

Reputation: 13521

Use std::fill. It would be better to use a std::vector than a c style array, but just for demonstration:

#include <algorithm>

int HTc = 500;
int HT[] = new int[HTc];
std::fill(HT, HT+HTc, -1);
// ...
delete[] HT;

Upvotes: 6

Mark Ransom
Mark Ransom

Reputation: 308206

I'm not sure this would work the way you want even if you used the recommended options - wouldn't it initialize the first array element to -1 and the rest to 0?

Just loop through all the elements and set them individually.

Upvotes: 2

Related Questions