如何跳过基于范围的for循环的第一次迭代?

24

我想做类似这样的事情:

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

我如何跳过我的 colourPos 向量的第一次迭代?

我可以使用.begin().end()吗?


bool first = true; for (int p : colourPos) { if (first) { first = false; continue; } // ... code here } - NetVipeC
first = 1; 然后 if (first == 1) { first = 0; continue; } - Marc B
1
也许您更喜欢使用std::for_each而不是基于范围的for循环? - YoungJohn
我同意使用std::for_each建议。std::foreach(std::begin(colourPos)+1, std::end(colourPos), [](){//...});会更加简洁。 - Paul
@Paul 谢谢,我不太理解末尾的[](){//...});这一部分,我需要在这里使用什么? - mrmike
@mrmike,我犯了一个小错误。应该是[](int p){//...}。你应该把你要做的事情放在我写有//...的for循环里。[](){}是C++11语法中lambda函数的表示方式。它只是一个未命名的函数,你将其传递给了std::for_each。这只是一种不同的说法,相当于void doStuff(int p){//...} std::foreach(std::begin(colourPos)+1, std::end(colourPos), doStuff); - Paul
3个回答

22
自从C++20之后,您可以使用Ranges库中的范围适配器std::views::drop,结合range-based for loop跳过第一个元素,如下所示:
std::vector<int> colourPos { 1, 2, 3 };

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

输出:

位置 = 2
位置 = 3

Wandbox上的代码

注意: 我不建议使用包含begin()和/或end()的解决方案,因为基于范围的for循环的思想是摆脱迭代器。如果你需要迭代器,那么我建议仍然使用基于迭代器的for循环。


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 << " ";
    }
}

输出:

2 3 4

更简单的方法是: 这里 有一个新的实时演示链接。
#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 << " ";
    }
}

输出:

2 3 4

1
这不支持lvalues,这限制了应用程序。 - Felix Dombek

14

做这个:

bool first = true;

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

    // ...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接