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!
In that case, the only value you can set it to is false
with:
bool* a = new bool[100000]();
That said, I'm not sure why you'd think you can't use a loop. They're there for a reason. You should just use the ready-made function fill
or fill_n
(depending on taste).
Note using new
"raw" like that is terrible programming practice. Use a std::vector<bool>
*:
std::vector<bool> v;
v.resize(100000);
std::fill(v.begin(), v.end(), true); // or false
Or:
std::vector<bool> v;
v.reserve(100000);
std::fill_n(std::back_inserter(v), 100000, true); // or false
*Of course, std::vector<bool>
happens to break the proper container interface so doesn't actually store bool
's. If that's a problem use a std::vector<char>
instead.