How to get an iterator to the end of a vector in C++

2 Answers

0 votes
#include <iostream>
#include <vector>

int main() 
{
    std::vector<int> v = { 5, 7, 3, 1, 9 };

    std::cout << *(std::end(v) - 1);
}



/*
run:

9

*/

 



answered Jun 21, 2020 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <algorithm>

int main() 
{
    std::vector<int> v = { 5, 7, 3, 1, 9 };

    if (std::find(std::begin(v), std::end(v), 3) != std::end(v)) {
        std::cout << "found";
    }
}



/*
run:

found

*/

 



answered Jun 21, 2020 by avibootz
...