在线文档教程

SequenceContainer

C++概念:顺序容器

SequenceContainerContainer它以线性方式存储相同类型的对象。

所需

类型X满足SequenceContainer如果。

  • 类型X满足Container,和给予。

  • T的元素类型X

  • A的分配程序类型X*X::allocator_type如果存在,则为std::allocator<T>

  • a,,,r值类型表达式X

  • p,一个有效的Const迭代器进入a

  • q,一个有效的可取消引用的Const迭代器a

  • q1q2,两个Const迭代器进入a使...[q1, q2)是有效范围。

  • ij,,,InputIterator斯使...[i, j)是一个有效的范围,迭代器引用隐式可转换为value_type

  • il,类型的对象std::initializer_list<value_type>

  • n,类型值X::size_type

  • t,类型的值或值X::value_type

  • rv,类型的非参数值。X::value_type

  • Args*模板参数包

  • args*带有模式的函数参数包Arg&&

以下表达式必须对所有序列容器有效并具有指定的效果std::array*

expressionreturn typeeffectspreconditionpostcondition
X(n, t) X a(n, t).Constructs the sequence container holding n copies of tT CopyInsertable into Xstd::distance(begin(),end()) == n
X(i, j) X a(i, j).Constructs the sequence container equal, element-wise, to the range [i,j)T is EmplaceConstructible from *i into X (only for std::vector) If the iterators are not ForwardIterators, T must be CopyInsertable.std::distance(begin(),end()) == std::distance(i,j)
X(il)X(il.begin(), il.end())
a = ilX&Assigns the range represented by il into a1T CopyInsertable and CopyAssignableExisting elements of a are destroyed or assigned to
a.emplace(p,args)iteratorInsert an object of type T, constructed with std::forward<Args>(args) before pT CopyInsertable (for std::vector and std::deque) T MoveAssignable and MoveInsertable.Returned iterator points at the element constructed from args into a
a.insert(p,t)iteratorInserts a copy of t before pT CopyInsertable (for std::vector and std::deque) T CopyAssignable or MoveAssignable.Returned iterator points at the copy of t inserted into a
a.insert(p,rv)iteratorInserts a copy of rv before p, possibly using move semanticsT MoveInsertable (for std::vector and std::deque) T MoveAssignable.Returned iterator points at the copy of rv inserted into a
a.insert(p,n,t)iteratorInserts n copies of t before pT CopyInsertable and CopyAssignableReturned iterator points at the copy of the first element inserted into a or is p for n==0
a.insert(p,i,j)iteratorInserts copies of elements in [i, j) before pT EmplaceConstructible and i and j are not in a (only for std::vector) If the iterators are not ForwardIterators, T must be MoveInsertable and MoveAssignable.Each iterator in [i,j) is dereferenced once. Returned iterator points at the copy of the first element inserted into a or is p for i==j
a.insert(p, il)iteratora.insert(p,il.begin(),il.end())Returned iterator points at the copy of the first element inserted into a or is p if il is empty.
a.erase(q)iteratorErases the element pointed to by q(std::deque, std::vector) T MoveAssignableReturned iterator points at the element that was immediately following q prior to erasure, or a.end() if no such element exists.
a.erase(q1,q2)iteratorErases elements in [p,q)(std::deque, std::vector) T MoveAssignableReturned iterator points at the element that was pointed by q2 prior to any erasure, or a.end() if no such element exists.
a.clear()voidDestroys all elements in aAll references, pointers, and iterators are invalidated, including the end iterator. a.empty() == true.
a.assign(i,j)voidReplaces elements in a with a copy of [i, j)T EmplaceConstructible and i, j not in a (std::vector) If not ForwardIterator. T MoveInsertable.Each iterator in [i,j) is dereferenced once
a.assign(il)voida.assign(il.begin(),il.end())
a.assign(n,t)voidReplaces elements in a with n copies of tT CopyInsertable and CopyAssignable

注记

数组支持从大括号内列表分配,但不支持从std::初始化器分配。[医]列单

  • std::array支持大括号内列表的赋值,但不支持std::initializer_list

下列表达式必须对命名为序列容器的序列容器有效并具有其指定的效果:

expressionreturn typeeffectspreconditionscontainers
a.front()reference const_reference for const a.Equivalent to *a.begin()(all)
a.back()reference const_reference for const a.Equivalent to { auto tmp = a.end( --tmp; return *tmp; }std::basic_string std::array std::deque std::list std::vector
a.emplace_front(args)voidPrepends a T constructed with std::forward<Args>(args)...T EmplaceConstructible into X from argsstd::deque std::forward_list std::list
a.emplace_back(args)voidAppends a T constructed with std::forward<Args>(args)...T EmplaceConstructible into X from args (std::vector only) T MoveInsertable into X.std::deque std::list std::vector
a.push_front(t)voidPrepends a copy of tT CopyInsertable into Xstd::deque std::forward_list std::list
a.push_front(rv)voidPrepends a copy of rv, possibly using move semanticsT MoveInsertable into Xstd::deque std::forward_list std::list
a.push_back(t)voidAppends a copy of tT CopyInsertable into Xstd::basic_string std::deque std::list std::vector
a.push_back(rv)voidAppends a copy of rv, possibly using move semanticsT MoveInsertable into Xstd::basic_string std::deque std::list std::vector
a.pop_front()voidDestroys the first element.a.empty() == falsestd::deque std::forward_list std::list
a.pop_back()voidDestroys the last elementa.empty() == falsestd::basic_string std::deque std::list std::vector
anreference const_reference for const a.Equivalent to *(n + a.begin())std::basic_string std::array std::deque std::vector
a.at(n)reference const_reference for const a.Equivalent to *(n + a.begin()), except that out_of_range is thrown if n>=size()std::basic_string std::array std::deque std::vector

此外,对于每个序列容器,接受两个输入迭代器和成员函数模板重载的构造函数模板insert(),,,append(),,,assign(),,,replace()如果相应的模板参数不满足,则两个输入迭代器不参与重载解析。InputIterator...

标准库中的顺序发货人

basic_stringstores and manipulates sequences of characters (class template)
array (since C++11)static contiguous array (class template)
vectordynamic contiguous array (class template)
dequedouble-ended queue (class template)
forward_list (since C++11)singly-linked list (class template)
listdoubly-linked list (class template)

权衡/使用说明

std::arrayFast access but fixed number of elements
std::vectorFast access but mostly inefficient insertions/deletions
std::liststd::forward_listEfficient insertion/deletion in the middle of the sequence
std::dequeEfficient insertion/deletion at the beginning and at the end of the sequence

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cpPreference.com/w/cpp/Concept/SequenceContainer