mardi 9 mars 2021

Test if std::iterator is at start of range without referring to collection

With simple code like this:

#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
    auto vi = v.begin();
    if (some_condition_or_other)
        vi++;

    if (vi != v.begin())
        std::cout << *std::prev(vi) << '\n';
}

Can the if (vi != v.begin()) be replaced by an expression using vi only? It would be nice if there was something like if (!vi.at_begin()) (and at_end() for symmetry), so is there an easy way to achieve this?

Aucun commentaire:

Enregistrer un commentaire