#include#include int main() { std::vector my_vector{1, 2, 3, 4, 5}; auto it = my_vector.begin() + 2; // iterator pointing to element with value 3 my_vector.erase(it); for (auto& elem : my_vector) { std::cout << elem << ' '; } return 0; } // Output: 1 2 4 5
#includeIn this example, we create a list of integers and remove a range of elements (2 and 3) using iterators pointing to their positions. After that, we print the remaining elements of the list. Package library: Standard Template Library (STL)#include
int main() { std::list my_list{1, 2, 3, 4, 5}; auto it_start = my_list.begin() + 1; // iterator pointing to element with value 2 auto it_end = my_list.begin() + 3; // iterator pointing to element with value 4 my_list.erase(it_start, it_end); for (auto& elem : my_list) { std::cout << elem << ' '; } return 0; } // Output: 1 4 5