How can I skip the first iteration of range-based for loops?
Asked Answered
G

5

26

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()?

Gib answered 3/9, 2014 at 19:46 Comment(6)
bool first = true; for (int p : colourPos) { if (first) { first = false; continue; } // ... code here }Grocery
first = 1; then if (first == 1) { first = 0; continue; }?Tate
maybe you would prefer to use std::for_each instead of a range-based for loop?Tolerable
I agree with the std::for_each suggestion. std::foreach(std::begin(colourPos)+1, std::end(colourPos), [](){//...}); would be cleaner.Dyna
@Dyna Thanks, I don't quite understand the bit at the end, [](){//...}); what do I use here?Gib
@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);Dyna
O
27

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.

Opportuna answered 18/4, 2021 at 6:4 Comment(0)
T
17

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
Terryn answered 3/9, 2014 at 19:49 Comment(1)
This does not support lvalues, which limits the applications.Spangler
L
16

Do this:

bool first = true;

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

    // ...
}
Lockjaw answered 3/9, 2014 at 19:50 Comment(0)
D
2

You can use std::span from C++20 onwards. This works for contiguous containers such as std::vector.

for (int p : std::span(colourPos).subspan(1)) {
  ...
}

This is a clean way to iterate over any index range [x, y).

Directions answered 4/12, 2023 at 11:42 Comment(1)
Unlike | std::views::drop(1) this only works on contiguous containers.Nuclei
F
1

This is another way using iterators.

std::vector<int> colourPos { 1, 2, 3 };
for (auto ii = ++colourPos.begin(); ii != colourPos.end(); ii++)
{
  auto p = *ii;
  ...
}
Fearfully answered 23/2 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.