forward_list: danh sách liên kết đơn, cho phép chèn/xóa O(1) tại bất kỳ vị trí.
forward_list::clear()
Xóa tất cả phần tử, size = 0. Không tham số, không trả về.
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<int> myList = {10, 20, 30, 40};
myList.clear();
for (auto i = myList.begin(); i != myList.end(); ++i) cout << ' ' << *i;
cout << "List is cleared";
return 0;
}
Đầu ra
List is cleared
forward_list::erase_after()
Xóa phần tử sau vị trí chỉ định. Giảm size tương ứng.
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<int> myList = {10, 20, 30, 40, 50};
auto i = myList.begin();
myList.erase_after(i);
cout << "Elements are: ";
for (auto i = myList.begin(); i != myList.end(); ++i) cout << ' ' << *i;
return 0;
}
Đầu ra
Elements are: 10 30 40 50