顺序容器之list

1、内存结构

list

这是list的内存结构。使用list的时候要包含头文件#include <list> ,要使用命名空间std。

2、list API

OperationEffect
list<Elem> cDefault constructor; creates an empty list without any elements
list<Elem> c(c2)Copy constructor; creates a new list as a copy of c2 (all elements are copied)
list<Elem> c = c2Copy constructor; creates a new list as a copy of c2 (all elements are copied)
list<Elem> c(rv)Move constructor; creates a new list, taking the contents of the rvalue rv (since C++11)
list<Elem> c = rvMove constructor; creates a new list, taking the contents of the rvalue rv (since C++11)
list<Elem> c(n)Creates a list with n elements created by the default constructor
list<Elem> c(n,elem)Creates a list initialized with n copies of element elem
list<Elem> c(beg,end)Creates a list initialized with the elements of the range [beg,end)
list<Elem> c(initlist)Creates a list initialized with the elements of initializer list initlist (since C++11)
list<Elem> c = initlistCreates a list initialized with the elements of initializer list initlist (since C++11)
c.~list()Destroys all elements and frees the memory
OperationEffect
c.empty()Returns whether the container is empty (equivalent to size()==0 but might be faster)
c.size()Returns the current number of elements
c.max_size()Returns the maximum number of elements possible
c1 == c2Returns whether c1 is equal to c2 (calls == for the elements)
c1 != c2Returns whether c1 is not equal to c2 (equivalent to !(c1==c2))
c1 < c2Returns whether c1 is less than c2
c1 > c2Returns whether c1 is greater than c2 (equivalent to c2<c1)
c1 <= c2Returns whether c1 is less than or equal to c2 (equivalent to !(c2<c1))
c1 >= c2Returns whether c1 is greater than or equal to c2 (equivalent to !(c1<c2))
OperationEffect
c = c2Assigns all elements of c2 to c
c = rvMove assigns all elements of the rvalue rv to c (since C++11)
c = initlistAssigns all elements of the initializer list initlist to c (since C++11)
c.assign(n,elem)Assigns n copies of element elem
c.assign(beg,end)Assigns the elements of the range [beg,end)
c.assign(initlist)Assigns all the elements of the initializer list initlist
c1.swap(c2)Swaps the data of c1 and c2
swap(c1,c2)Swaps the data of c1 and c2
OperationEffect
c.front()Returns the first element (no check whether a first element exists)
c.back()Returns the last element (no check whether a last element exists)
OperationEffect
c.begin()Returns a bidirectional iterator for the first element
c.end()Returns a bidirectional iterator for the position after the last element
c.cbegin()Returns a constant bidirectional iterator for the first element (since C++11)
c.cend()Returns a constant bidirectional iterator for the position after the last element (since C++11)
c.rbegin()Returns a reverse iterator for the first element of a reverse iteration
c.rend()Returns a reverse iterator for the position after the last element of a reverse iteration
c.crbegin()Returns a constant reverse iterator for the first element of a reverse iteration (since C++11)
c.crend()Returns a constant reverse iterator for the position after the last element of a reverse iteration (since C++11)
OperationEffect
c.push_back(elem)Appends a copy of elem at the end
c.pop_back()Removes the last element (does not return it)
c.push_front(elem)Inserts a copy of elem at the beginning
c.pop_front()Removes the first element (does not return it)
c.insert(pos,elem)Inserts a copy of elem before iterator position pos and returns the position of the new element
c.insert(pos,n,elem)Inserts n copies of elem before iterator position pos and returns the position of the first new element (or pos if there is no new element)
c.insert(pos,beg,end)Inserts a copy of all elements of the range [beg,end) before iterator position pos and returns the position of the first new element (or pos if there is no new element)
c.insert(pos,initlist)Inserts a copy of all elements of the initializer list initlist before iterator position pos and returns the position of the first new element (or pos if there is no new element; since C++11)
c.emplace(pos,args...)Inserts a copy of an element initialized with args before iterator position pos and returns the position of the new element (since C++11)
c.emplace_back(args...)Appends a copy of an element initialized with args at the end (returns nothing; since C++11)
c.emplace_front(args...)Inserts a copy of an element initialized with args at the beginning (returns nothing; since C++11)
c.erase(pos)Removes the element at iterator position pos and returns the position of the next element
c.erase(beg,end)Removes all elements of the range [beg,end) and returns the position of the next element
c.remove(val)Removes all elements with value val
c.remove_if(op)Removes all elements for which op(elem) yields true
c.resize(num)Changes the number of elements to num (if size() grows new elements are created by their default constructor)
c.resize(num,elem)Changes the number of elements to num (if size() grows new elements are copies of elem)
c.clear()Removes all elements (empties the container)
OperationEffect
c.unique()Removes duplicates of consecutive(连续的) elements with the same value 去除相邻的一样的数据
c.unique(op)Removes duplicates of consecutive elements, for which op() yields true
c.splice(pos,c2)Moves all elements of c2 to c in front of the iteratorposition pos
c.splice(pos,c2,c2pos)Moves one element at c2pos in c2 in front of pos of list c (c and c2 may be identical)
c.splice (pos,c2, c2beg,c2end)Moves all elements of the range [c2beg,c2end) in c2 in front of pos of list c (c and c2 may be identical)
c.sort()Sorts all elements with operator <
c.sort(op)Sorts all elements with op()
c.merge(c2)Assuming that both containers contain the elements sorted, moves all elements of c2 into c so that all elements are merged and still sorted
c.merge(c2,op)Assuming that both containers contain the elements sorted due to the sorting criterion op(), moves all elements of c2 into c so that all elements are merged and still sorted according to op()
c.reverse()Reverses the order of all elements

上面的unique是唯一的意思,如果我们想要只留下list中重复元素的一个,我们需要先进行排序sort在使用unique就好了。
splice 是拼接的意思。拼接到插入位置的前面,此时c2会消失。
reverse就是逆转
merge是合并的意思和splice 很像,如果想有序排列,使用merge之前应该先对两个list进行sort排序,这样就是有序的了。

3、list的高级主题

不提供下标访问([], at),随机访问是低效的,第一和最后一个元素访问是高效的。list 容器由于采用了双向迭代器,不支持随机访问,所以标准库的merge(), sort()等功能函数都不适用,list 单独实现了merge(),sort()等函数。
另外在vector中会有迭代器失效的问题,在list中没有,而且在list中删除最好不要用erase循环,用remove和remove_if就够了。

4、bind函数

在c++11之前,要绑定某个函数、函数对象或者成员函数的不同参数值需要用到不同的转换器,如bind1st、bind2nd、fun_ptr、mem_fun和mem_fun_ref等.在c++11中,绑定参数的方法得以简化.c++11提供了"一站式"绑定模板bind,其用法为:

#include <functional>
std::bind(待绑定的函数对象/函数指针/成员函数指针,参数绑定值1,参数绑定值2,...,参数绑定值n);

bind的第一个参数是待绑定的函数对象或者函数指针,之后跟随多个参数以设定待绑定函数的参数绑定方式。待绑定函数有多少个参数,则bind后便需要多少个参数以一一声明其参数的绑定方法.当参数绑定为某一固定值时,则其对应参数绑定值可以使一个变量或常量.当需要将参数与绑定所生成的函数对象的某个参数相关联时,则需要用到在标准中预定义的几个常量_1、_2、_3等.这些常量声明在std::placeholders命名空间内.

Last modification:September 16th, 2019 at 07:09 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment