Contents
List là sequence container cho phép insert và delete các node data bất cứ nơi nào trong list, và lặp lại ở cả hai hướng.
List được implement như danh sách liên kết đôi (Double Link List). Danh sách liên kết đôi có thể lưu trữ từng phần tử trong các vị trí lưu trữ khác nhau và không liên quan nhau.
List tương tự như forward_list. Sự khác biệt chính là các object forward_list là các danh sách liên kết (Single Linked List).
So với các container khác ( như: array, vector và deque), list thực hiện tốt hơn trong việc insert, di chuyển các phần tử ở bất kỳ vị trí nào list.
Cú pháp
std::list<object_type> list_name;
object_type: kiểu dữ liệu (int, char*, float,…)
list_name: tên đối tượng list
Function
(constructor) | Hàm tạo list |
(destructor) | Hàm hủy list |
operator= | Toán tử gán |
Iterator
begin | Trả về iterator tới phần tử đầu trong list |
end | Trả về iterator tới phần tử cuối trong list |
rbegin | Trả về reverse iterator trỏ tới đầu list |
rend | Trả về reverse iterator trỏ tới cuối list |
cbegin (c++ 11) | Trả về const_iterator trỏ tới đầu list |
cend (c++ 11) | Trả về const_iterator trỏ tới cuối list |
crbegin (c++ 11) | Trả về const_reverse_iterator trỏ tới đầu list |
crend (c++ 11) | Trả về const_reverse_iterator trỏ tới cuối list |
Capacity
empty | Kiểm tra list rỗng hay không |
size | Trả về kích thước của list |
max_size | Trả về maximum size |
Elements sccess
front | Truy cập vào phần tử đầu trong list |
back | Truy cập vào phần tử cuối trong list |
Modifiers
assign | assign content cho list |
emplace_front (c++ 11) | Khởi tạo và chèn phần tử mới vào đầu list |
push_front | Chèn phần tử mới vào đầu list |
pop_front | Delete phần tử ở đầu list |
emplace_back (c++ 11) | Khởi tạo và chèn phần tử mới ở cuối list |
push_back | Chèn phần tử mới vào cuối list |
pop_back | Delete phần tử cuối list |
emplace (c++ 11) | Khởi tạo và chèn phần tử |
insert | Chèn phần tử vào list |
erase | Xóa phần tử trong list |
swap | swap nội dung giữa 2 list với nhau |
resize | Thay đổi kích thước của list |
clear | Xóa content của list |
Operations
splice | tranfer các phần tử từ list này sang list khác |
remove | Xóa phần tử có chỉ định value |
remove_if | Xóa phần tử có điều kiện |
unique | Xóa phần tử duplicate |
merge | Merge 2 list đã được sắp xếp |
sort | Sắp xếp các phần tử trong list |
reverse | Đảo ngược vị trí của các phần tử trong list |
Observers
get_allocator | Get allocator |
Ví dụ: Minh họa các function của list
#include <list> #include <string> #include <iostream> using namespace std; void printlist(list<std::string> words); int main() { std::list<std::string> words1, words2; std::list<std::string>::iterator it; /* push_front() & push_back()*/ words1.push_front("one"); words1.push_back("two"); words1.push_front("zero"); words1.push_back("three"); cout << "\npush_front() & push_back()"; printlist(words1); cout << "size() : " << words1.size() << endl; cout << "max_size() : " << words1.max_size() << endl; /* pop_front() */ words1.pop_front(); cout << "\npop_front()"; printlist(words1); cout << "size() : " << words1.size() << endl; cout << "max_size() : " << words1.max_size() << endl; /* insert() */ it = words1.begin(); words1.insert(it, "zero"); cout << "\ninsert()"; printlist(words1); cout << "size() : " << words1.size() << endl; cout << "max_size() : " << words1.max_size() << endl; /* splice */ words2.push_front("four"); words2.push_back("five"); it = words1.end(); words1.splice(it, words2); cout << "\nsplice()"; printlist(words1); cout << "size() : " << words1.size() << endl; cout << "max_size() : " << words1.max_size() << endl; /* remove() */ words1.remove("five"); cout << "\nremove()"; printlist(words1); cout << "size() : " << words1.size() << endl; cout << "max_size() : " << words1.max_size() << endl; system("pause"); return 0; } void printlist(list<std::string> words) { list<std::string>::iterator i; cout << "\nlist: "; for (i = words.begin(); i != words.end(); i++) { cout << *i << " "; } cout << endl; }
Kết quả:
Leave a Reply
You must be logged in to post a comment.