目錄
- 一、list 簡介
- 二、list 的常用接口
- 1. 構造函數(constructor )
- 2. 迭代器(iterator)
- 3. 容量、修改和訪問(capacity 、modify and access)
一、list 簡介
??簡單來說,list 就是數據結構初階中學習的鏈表,還是所有特性都具備的帶頭雙向循環鏈表。帶頭是為了更好地適應迭代器,雙向循環是為了插入和刪除的效率。與之前學習的 list 相比,本次學習的 list 升級成為了類模板且增加了迭代器。
二、list 的常用接口
??下面介紹一下 list 各方面的常用接口。
1. 構造函數(constructor )
??下面是 list 常用的四個構造函數的聲明和使用。
(1)函數聲明
// list 構造函數聲明// 1. 默認構造函數
list();
// 2. 指定個數和初始值
list(size_t n, const T& value = T());
// 3. 迭代器構造函數
template<class Iterator>
list(Iterator first, Iterator last);
// 4. 復制構造函數
list(const list<T>& lt);
(2)使用演示
// 1. constructor
void test1()
{// 1. 默認構造函數list<int> lt1;cout << "lt1.size: " << lt1.size() << endl << endl;// 2. 指定個數和初始值list<int> lt2(5, 1);cout << "lt2.size: " << lt2.size() << endl;cout << "lt2: ";for (const auto& e : lt2)cout << e << " ";cout << endl << endl;// 3. 迭代器構造函數vector<int> vt_i;for (int i = 1; i < 5; ++i)vt_i.push_back(i);list<int> lt3(vt_i.begin(), vt_i.end());cout << "lt3.size: " << lt3.size() << endl;cout << "lt3: ";for (const auto& e : lt3)cout << e << " ";cout << endl << endl;// 4. 拷貝構造函數list<int> lt4(lt3);cout << "lt4.size: " << lt4.size() << endl;cout << "lt4: ";for (const auto& e : lt4)cout << e << " ";cout << endl << endl;
}
(3)運行結果
2. 迭代器(iterator)
??下面介紹 list 常用的四個迭代器。反向迭代器參考正向迭代器的用法。
(1)函數聲明
// 1. 普通迭代器
iterator begin();
iterator end();
// 2. const 迭代器
const_iterator begin() const;
const_iterator end() const;
// 3. 反向迭代器
reverse_iterator rbegin();
reverse_iterator rend();
// 4. const 反向迭代器
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;
(2)使用演示
// 2. 迭代器
void test2()
{list<int> lt1;for (int i = 1; i < 10; ++i)lt1.push_back(i);// 1. 正向迭代器遍歷list<int>::iterator it = lt1.begin();while (it != lt1.end()){cout << *it << " ";++it;}cout << endl;// 2. 反向迭代器遍歷list<int>::reverse_iterator rit = lt1.rbegin();while (rit != lt1.rend()){cout << *rit << " ";++rit;}cout << endl;
}
(3)運行結果
3. 容量、修改和訪問(capacity 、modify and access)
??下面分別介紹 list 的 2 個與容量有關的接口、2 個與訪問有關的接口,8 個與修改有關的接口。
(1)函數聲明
??下面的 T 是模版中的類型參數。
// 1. capacity
size_t size() const;
bool empty() const;// 2. access
T& front();
T& back();// 3. modify
void push_front(const T& value);
void push_back(const T& value);
void pop_front();
void pop_back();
iterator insert(iterator pos, const T& value); // 在 pos 位置前插入
iterator erase(iterator pos); // 刪除 pos 位置
(2)使用演示
// 3. capacity、access and modify
void test3()
{// 1. capacitylist<int> lt1;if (lt1.empty()){cout << "lt1 is empty.\n";}for (int i = 1; i < 10; ++i)lt1.push_back(i);cout << "lt1.size: " << lt1.size() << endl << endl;// 2. accesscout << "lt1.front: " << lt1.front() << endl;cout << "li1.back: " << lt1.back() << endl;// 3. modifylist<int> lt2;// 插入lt2.push_back(1);lt2.push_front(2);// 打印for (const auto& e : lt2)cout << e << " ";cout << endl;// 插入lt2.insert(lt2.begin(), 10);lt2.insert(lt2.end(), 99);// 打印for (const auto& e : lt2)cout << e << " ";cout << endl;// 刪除lt2.pop_back();lt2.pop_front();// 打印for (const auto& e : lt2)cout << e << " ";cout << endl;// 刪除lt2.erase(lt2.begin());lt2.erase(--lt2.end());// 打印for (const auto& e : lt2)cout << e << " ";cout << endl;
}
(3)運行結果