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()
?
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()
?
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
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.
#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:
#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
Do this:
bool first = true;
for (int p : colourPos)
{
if (first)
{ first = false; continue; }
// ...
}
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)
.
| std::views::drop(1)
this only works on contiguous containers. –
Nuclei 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;
...
}
© 2022 - 2024 — McMap. All rights reserved.
bool first = true; for (int p : colourPos) { if (first) { first = false; continue; } // ... code here }
– Groceryfirst = 1;
thenif (first == 1) { first = 0; continue; }
? – Tatestd::for_each
instead of a range-based for loop? – Tolerablestd::for_each
suggestion.std::foreach(std::begin(colourPos)+1, std::end(colourPos), [](){//...});
would be cleaner. – Dyna[](){//...});
what do I use here? – Gib[](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 tostd::for_each
. It's just an different way of sayingvoid doStuff(int p){//...} std::foreach(std::begin(colourPos)+1, std::end(colourPos), doStuff);
– Dyna