开发者

Fill A Variable Size Array

开发者 https://www.devze.com 2023-01-16 07:20 出处:网络
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
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=gn开发者_运维百科u++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?


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;


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消