C++ stl 学习3-- list
list 是双向链表,两端都可以插入元素。
list常用的函数如下

list 由于是链表实现的,所以不支持下标访问,一般用迭代器访问
insert() // list的insert不会使迭代器失效
The container is extended by inserting new elements before the element at the specified position
返回指向新元素首地址的迭代器
An iterator that points to the first of the newly inserted elements
erase() // 删除元素,与删除元素相关的所有迭代器失效,无关的不影响
Removes from the list container either a single element (position) or a range of elements ([first,last)).
#include <iostream> #include <list> int main () { std::list<int> mylist; std::list<int>::iterator it1,it2; for (int i = 1; i < 10; ++i) { mylist.push_back(i * 10); } // 10 20 30 40 50 60 70 80 90 it1 = it2 = mylist.begin(); advance (it2,6); // 指向70 ++it1; // 指向20 it1 = mylist.erase(it1); // 删除20后指向30 it2 = mylist.erase(it2); // 删除70后指向80 // 10 30 40 50 60 80 90 ++it1; // 指向40 --it2; // 指向60 mylist.erase(it1,it2); // 删除40到60之间的,不包括60 0 30 60 80 90 std::cout << "mylist contains:"; for (it1 = mylist.begin(); it1 != mylist.end(); ++it1) { std::cout << ' ' << *it1; } std::cout << endl; return 0;
}
resize() // 重新设置大小,n比原来大时,扩展的元素为0或者是设置的值,n比原来小时n后面的元素全部被删
void resize (size_type n);
void resize (size_type n, const value_type& val);
clear() // 删除所有的元素
splice() // 将另一个链表的所有或者部分元素转移当前链表某个位置,两个链表的大小都有改变,没有构造析构函数调用,也不管元素类型支不支持移动构造
Transfers elements from x into the container, inserting them at position.
This effectively inserts those elements into the container and removes them from x, altering the sizes of both containers. The operation does not involve the construction or destruction of any element. They are transferred, no matter whether x is an lvalue or an rvalue, or whether the value_type supports move-construction or not.
void splice (const_iterator position, list& x); // 将x里面所有元素放入到指定的迭代器位置
void splice (const_iterator position, list& x, const_iterator i); // 将x里面的迭代器i位置的元素放到指定position处
void splice (const_iterator position, list& x, const_iterator first, const_iterator last); // 将x里面的 [first, last)位置的元素放到指定position处
remove() // 按值删除list中所有与val相等的元素,这比<algorithm>里面提供的remove函数还要快
void remove (const value_type& val);
remove_if() // 根据条件删除元素
unique() 删除相邻的相等元素,只保留一个元素,有下面2种形式
void unique();
template <class BinaryPredicate> void unique (BinaryPredicate binary_pred); // 定义什么叫做相等
sort() // 稳定排序,所有的元素是移动过去的,不会有构造析构拷贝函数调用
void sort(); // 默认排序是升序
template <class Compare> void sort (Compare comp);
The resulting order of equivalent elements is stable: i.e., equivalent elements preserve the relative order they had before the call.
The entire operation does not involve the construction, destruction or copy of any element object. Elements are moved within the container
reverse() // 翻转链表
merge() 将x 里面的元素移动到当前链表中,
void merge (list& x);
template <class Compare> void merge (list&& x, Compare comp);
#include <iostream> #include <list> using namespace std; ostream& operator<<(ostream& out, list<int> x) { for (auto i : x) { out << i << " "; } return out; } int main () { list<int> mylist{1, 2, 3, 4, 5, 6, 7, 8, 9}; mylist.remove(2); //1, 3, 4,5,6,7,8,9 mylist.remove_if([](int num) { return num % 2;}); // 移除奇数 4 6 8 cout << mylist << endl;
list<int> a{1, 3, 3, 4, 5, 5, 5, 1, 3, 6, 9};
a.unique(); // 1 3 4 5 1 3 6 9
list<int> b{1, 3, 3, -3, 1, 4, -4, 4, -4 ,5};
b.unique([](int num_a, int num_b) {return abs(num_a) == abs(num_b);}); // 1 3 1 4 5
list<int> c{1,3,5,7,9};
list<int> d{2,4,6,8,10};
c.splice(c.begin(), d); // c是2 4 6 8 10 1 3 5 7 9 ;d为空
list<int> e{1,3,5,7,9};
list<int> f{2,4,6,8,10};
e.merge(f); // e 1~10 f 空 return 0;
}

浙公网安备 33010602011771号