Is there a snazzy way to create a numbered array in C/C++?
For instance, If I were to make an array of length 10, is there a fast way to have the contents of that array numbering consecutively e.g. {0,1,2,3,4,5,6,7,8,9} or do I have to manually set it with a loop?
>>55991844
this should have gone in DPT oops, i'll do that next time.
But still if someone knows pls do tell
#include <iostream>
#include <numeric>
int main(int argc, char **argv)
{
int arr[10];
std::iota(arr.begin(), arr.end(), 0); // http://en.cppreference.com/w/cpp/algorithm/iota
for (int i = 0; i < 10; i++) {
std::cout << arr[i] << ' ';
}
std::cout << '\n';
return 0;
}
>>55992790
Damn, I'm retarded.std::iota(arr.begin(), arr.end(), 0);
should bestd::iota(arr, arr + 10, 0);
>>55991844
C++17 way:#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v(10);
int n = {0};
std::generate(v.begin(), v.end(), [&n]{ return n++; });
}
>>55992842
>C++17 way
or not, don't know.