How to loop through a vector without the last element in C++

1 Answer

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

int main() {

	std::vector<int> vec{1, 2, 3, 4, 5, 6};
	
	for (std::vector<int>::iterator it = vec.begin(); it != std::prev(vec.end()); it++) {
        std::cout << *it << " ";
	}
}



/*
run:

1 2 3 4 5 

*/

 



answered Dec 11, 2024 by avibootz
...