How to print last 3 elements of a vector in C++

1 Answer

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

int main()
{
    std::vector<int> v = {12, 98, 80, 50, 88, 35, 70, 60, 97, 85, 89};
 
    for (auto i = v.end() - 3; i != v.end(); i++)
        std::cout << *i << ' ';

    return 0;
}
   
   
   
   
/*
run:
   
97 85 89 
   
*/

 



answered Jun 9, 2021 by avibootz

Related questions

1 answer 160 views
1 answer 160 views
1 answer 124 views
1 answer 164 views
1 answer 94 views
...