📜  C++ STL-list

📅  最后修改于: 2020-10-17 07:42:56             🧑  作者: Mango

C++ List

  • List是连续的容器,而vector是非连续的容器,即list将元素存储在连续的存储器中,而vector存储在不连续的存储器中。
  • 向量中间的插入和删除非常昂贵,因为转移所有元素都需要花费大量时间。链接列表克服了这个问题,它是使用列表容器实现的。
  • List支持双向,并为插入和删除操作提供了一种有效的方法。
  • 在列表中遍历速度很慢,因为列表元素是按顺序访问的,而向量支持随机访问。

列表模板

#include
#include
using namespace std;
int main()
{
   list l;
}

它创建一个空的整数类型值列表。

列表也可以使用参数初始化。

#include
#include
using namespace std;
int main()
{
   list l{1,2,3,4};
}

列表可以通过两种方式初始化。

list  new_list{1,2,3,4};
or
list new_list = {1,2,3,4};

C++列表函数

以下是列表的成员函数:

Method Description
insert() It inserts the new element before the position pointed by the iterator.
push_back() It adds a new element at the end of the vector.
push_front() It adds a new element to the front.
pop_back() It deletes the last element.
pop_front() It deletes the first element.
empty() It checks whether the list is empty or not.
size() It finds the number of elements present in the list.
max_size() It finds the maximum size of the list.
front() It returns the first element of the list.
back() It returns the last element of the list.
swap() It swaps two list when the type of both the list are same.
reverse() It reverses the elements of the list.
sort() It sorts the elements of the list in an increasing order.
merge() It merges the two sorted list.
splice() It inserts a new list into the invoking list.
unique() It removes all the duplicate elements from the list.
resize() It changes the size of the list container.
assign() It assigns a new element to the list container.
emplace() It inserts a new element at a specified position.
emplace_back() It inserts a new element at the end of the vector.
emplace_front() It inserts a new element at the beginning of the list.