Search code examples
c++c++11foreachiterationiota

Iterator or std::iota with regular C++ Array?


Is there a way to achieve the same effect as this,

std::list<int> l(10);
std::iota(l.begin(), l.end(), -4);

With a regular int a[]?

Or, is the following the only way around:

for (iterator itr = begin; itr != end; ++itr)
    /* ... visit *itr here ... */

Solution

  • C++11 added std::begin and std::end. Since then there is no difference:

    std::list<int> l(10);
    std::iota(std::begin(l),std::end(l), -4);
    int a[10];
    std::iota(std::begin(a),std::end(a), -4);