I'm trying to generate an an array in descending order, as in actually fill it from scratch. I am not trying to sort given numbers. I wanted to know if there is a way to do it without a for loop. I was able to generate an array in ascending order of number using std::iota, but I haven't been able to (nor do I think I can) use that for generating a descending array. Is there a one liner out there for generating a array of size in descending order?
EDIT: I was able to create an array in ascending order using:
void generateAscendingArray(int values[], size_t size){
std::iota(values, values+size, 1);
}
So it fills the array with numbers corresponding to the size of the array. I am simply looking for an easy way to generate a descending array without a loop, or with minimal lines and work, similar to ascending. I want it to fill the array in accordance to the size of the array just in descending order.
The easiest solution may be to use
std::iota
but to fill your array from back to front. Usestd::rbegin
andstd::rend
to get reverse iterators that iterate in reverse order :The output :
Edit : It seems you are using a pointer to the first element of an array and a size instead of using a proper container object. In that case,
std::rbegin
andstd::rend
cannot deduce the size of the pointed array using the pointer alone. You will need to construct your reverse iterators manually as is done infoo
in the following example :However, you would be better off ditching arrays as containers in favor of standard containers like
std::vector
or using iterators to express ranges of elements.