22

I would like to do something like this:

for (int p : colourPos[i+1])

How do I skip the first iteration of my colourPos vector?

Can I use .begin() and .end()?

honk
  • 9,137
  • 11
  • 75
  • 83
mrmike
  • 398
  • 2
  • 4
  • 14
  • `bool first = true; for (int p : colourPos) { if (first) { first = false; continue; } // ... code here }` – NetVipeC Sep 03 '14 at 19:48
  • `first = 1;` then `if (first == 1) { first = 0; continue; }`? – Marc B Sep 03 '14 at 19:48
  • 1
    maybe you would prefer to use `std::for_each` instead of a range-based for loop? – YoungJohn Sep 03 '14 at 19:50
  • I agree with the `std::for_each` suggestion. `std::foreach(std::begin(colourPos)+1, std::end(colourPos), [](){//...});` would be cleaner. – Paul Sep 03 '14 at 19:56
  • @Paul Thanks, I don't quite understand the bit at the end, `[](){//...});` what do I use here? – mrmike Sep 03 '14 at 20:01
  • @mrmike, I made a small mistake. That should be `[](int p){//...}`. You should put whatever you were going to do in the for loop where I have `//...`. `[](){}` is the c++11 syntax for lambda functions. It's just an unnamed function that you are passing to `std::for_each`. It's just an different way of saying `void doStuff(int p){//...} std::foreach(std::begin(colourPos)+1, std::end(colourPos), doStuff);` – Paul Sep 03 '14 at 20:07

3 Answers3

20

Since C++20 you can use the range adaptor std::views::drop from the Ranges library together with a range-based for loop for skipping the first element, as follows:

std::vector<int> colourPos { 1, 2, 3 };

for (int p : colourPos | std::views::drop(1)) {
    std::cout << "Pos = " << p << std::endl;
}

Output:

Pos = 2
Pos = 3

Code on Wandbox

Note: I would not recommend using a solution that contains begin() and/or end(), because the idea of a range-based for loop is to get rid of iterators. If you need iterators, then I would stick with an interator-based for loop.

honk
  • 9,137
  • 11
  • 75
  • 83
16

Live demo link.

#include <iostream>
#include <vector>
#include <iterator>
#include <cstddef>

template <typename T>
struct skip
{
    T& t;
    std::size_t n;
    skip(T& v, std::size_t s) : t(v), n(s) {}
    auto begin() -> decltype(std::begin(t))
    {
        return std::next(std::begin(t), n);
    }
    auto end() -> decltype(std::end(t))
    {
        return std::end(t);
    }
};

int main()
{
    std::vector<int> v{ 1, 2, 3, 4 };

    for (auto p : skip<decltype(v)>(v, 1))
    {
        std::cout << p << " ";
    }
}

Output:

2 3 4

Or simpler:

Yet another live demo link.

#include <iostream>
#include <vector>

template <typename T>
struct range_t
{
    T b, e;
    range_t(T x, T y) : b(x), e(y) {}
    T begin()
    {
        return b;
    }
    T end()
    {
        return e;
    }
};

template <typename T>
range_t<T> range(T b, T e)
{
    return range_t<T>(b, e);
}

int main()
{
    std::vector<int> v{ 1, 2, 3, 4 };

    for (auto p : range(v.begin()+1, v.end()))
    {
        std::cout << p << " ";
    }
}

Output:

2 3 4
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
14

Do this:

bool first = true;

for (int p : colourPos)
{
    if (first)
    { first = false; continue; }

    // ...
}
hyil
  • 190
  • 5