Hàm erase() dùng để xóa mục khỏi vector C++ STL với giá trị xác định.
Thuật toán
Begin
Khai báo vector v và iterator it trỏ đến vector.
Khởi tạo vector.
Erase() dùng để xóa mục từ cuối.
In các phần tử còn lại.
End.
Ví dụ mã
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v{ 6,7,8,9,10};
vector<int>::iterator it;
it = v.end();
it--;
v.erase(it);
for (auto it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
return 0;
}
Kết quả
The current content of the vector is : 6 7 8 9 10 Please enter the element to be deleted -> 7 The current content of the vector is : 6 8 9 10