1.list() 构造空的list 2.list (size_type n, const value_type& val = value_type()) 构造的list中包含n个值为val的元素 3.list (const list& x) 拷贝构造函数 4.list (InputIterator first, InputIterator last) 用[first, last)区间中的元素构造list
#include<iostream> #include<list> int main() { list<int> l1;//调用默认构造函数 list<int> l2(5,10);//l2中放5个值为100的元素 list<int> l3 (l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构 std::list<int> l4 (l3); // 用l3拷贝构造l4 // 以数组为迭代器区间构造l5 int array[] = {16,2,77,29}; list<int> l5 (array, array + sizeof(array) / sizeof(int) ); // 用迭代器方式打印l5中的元素 for(list<int>::iterator it = .begin(); it != l5.end(); it++) :cout << *it << " "; cout<<endl; //范围for打印 for(auto& ch:l5) :cout<< ch << " "; cout<<endl; }1.begin() + end() 返回第一个元素的迭代器+返回最后一个元素下一个位置的迭代器 2.rbegin() + rend() 返回第一个元素的reverse_iterator,即end位置,返回最后一个元素下一个位置的 reverse_iterator,即begin位置
这里可以把迭代器理解成指针的使用
#include <iostream> using namespace std; #include <list> void print_list(const list<int>& l) { // 注意这里调用的是list的 begin() const,返回list的const_iterator对象 for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it) { cout << *it << " "; // *it = 10; 编译不通过 } cout << endl; } int main() { int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; list<int> l(array, array + sizeof(array) / sizeof(array[0])); // 使用正向迭代器正向list中的元素 for (list<int>::iterator it = l.begin(); it != l.end(); ++it) cout << *it << " "; cout << endl; // 使用反向迭代器逆向打印list中的元素 for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it) cout << *it << " "; cout << endl; return 0; }1.empty 检测list是否为空,是返回true,否则返回false 2.size 返回list中有效节点的个数 3.front 返回list的第一个节点中值的引用 4.back 返回list的最后一个节点中值的引用 5.push_front 在list首元素前插入值为val的元素 6.pop_front 删除list中第一个元素 7.push_back 在list尾部插入值为val的元素 8.pop_back 删除list中最后一个元素 9.insert 在list position 位置中插入值为val的元素 10.erase 删除list position位置的元素 11.swap 交换两个list中的元素 12.clear 清空list中的有效元素
#include <list> void PrintList(list<int>& l) { for (auto& e : l) cout << e << " "; cout << endl; } // push_back/pop_back/push_front/pop_front void TestList1() { int array[] = { 1, 2, 3 }; list<int> L(array, array+sizeof(array)/sizeof(array[0])); // 在list的尾部插入4,头部插入0 L.push_back(4); L.push_front(0); PrintList(L); // 删除list尾部节点和头部节点 L.pop_back(); L.pop_front(); PrintList(L); } // insert /erase void TestList3() { int array1[] = { 1, 2, 3 }; list<int> L(array1, array1+sizeof(array1)/sizeof(array1[0])); // 获取链表中第二个节点 auto pos = ++L.begin(); cout << *pos << endl; // 在pos前插入值为4的元素 L.insert(pos, 4); PrintList(L); // 在pos前插入5个值为5的元素 L.insert(pos, 5, 5); PrintList(L); // 在pos前插入[v.begin(), v.end)区间中的元素 vector<int> v{ 7, 8, 9 }; L.insert(pos, v.begin(), v.end()); PrintList(L); // 删除pos位置上的元素 L.erase(pos); PrintList(L); // 删除list中[begin, end)区间中的元素,即删除list中的所有元素 L.erase(L.begin(), L.end()); PrintList(L); } // resize/swap/clear void TestList4() { // 用数组来构造list int array1[] = { 1, 2, 3 }; list<int> l1(array1, array1+sizeof(array1)/sizeof(array1[0])); PrintList(l1); // 交换l1和l2中的元素 l1.swap(l2); PrintList(l1); PrintList(l2); // 将l2中的元素清空 l2.clear(); cout<<l2.size()<<endl; }前面说过,此处大家可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节 点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代 器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
void TestListIterator1() { int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; list<int> l(array, array+sizeof(array)/sizeof(array[0])); auto it = l.begin(); while (it != l.end()) { // erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给 其赋值 l.erase(it); ++it; } } // 改正 void TestListIterator() { int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; list<int> l(array, array+sizeof(array)/sizeof(array[0])); auto it = l.begin(); while (it != l.end()) { it = l.erase(it); } }