2 ms·
Even better, so you can't accidentally move out of bounds: for (int i : nums) { nums[i] = rng(); } Edit: Ooops, I'm an idiot, this doesn't do anythi
by tsimionescu 4y ago
Even better, so you can't accidentally move out of bounds:
for (int i : nums) {
nums[i] = rng();
}
Edit: Ooops, I'm an idiot, this doesn't do anything useful (i is every value in nums, not the indices of nums...)
- leshow 4y agoDoesn't that not work because `nums` is all initialized to 0 though? Like, wouldn't it be for (int num : nums) { num = rng(); } (I don't know any c++). I kind of expect the above not to work though because it's doing an assignment. I suppose this is a good argument to just use a `fill` or `generate` function...
- tsimionescu 4y agoYou're right, I completely forgot what it meant... The loop I wrote was basically equivalent to : for (int i = 0; i < nums.size(); ++i) { nums[nums[i]] = rng(); } Which happens not to go out of bounds simply because nums[i] is initially 0 for every i in the range - so it sets nums[0] to a different random number 1000 times.