1
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?

2
  • 1
    Seriously, stop what you're doing and make your own vector and string class. Then just use std::fill over it. (Or if your professor is stupid in that regard as well, make your own fill.) Commented Sep 12, 2010 at 5:44
  • I wouldn't recommend that you use variable length arrays. EDIT: Oh, you are using dynamic memory. Commented Sep 12, 2010 at 9:54

3 Answers 3

6

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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

2 Comments

His professor doesn't allow it, apparently. How do terrible programmers end up teaching?
I guess it fits the old line about "those who can, do; those who can't, teach; those who can't teach, teach PE" -- only in this case, they apparently teach programming. Which, in turn, brings to mind another old line, to the effect that: "Free verse is the most difficult poetry to write acceptably -- and therefore the most often attempted by those who cannot write any poetry acceptably."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.