|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ASequenceContainer is aContainer that stores objects of the same type in a linear arrangement.
Contents |
Given the following types and values:
| Type | Definition |
C | a sequence container class |
T | the element type ofC |
A | the allocator type ofC:
|
R(since C++23) | a type that modelscontainer-compatible-range <T> |
Args(since C++11) | a template parameter pack |
Iter | C::iterator |
Ref | C::reference |
CRef | C::const_reference |
| Value | Definition |
| v | a value of typeC |
| cv | a value of typeconst C |
| i,j | LegacyInputIterators such that[i, j) is avalid range and that the iterators refer to elements implicitly convertible toC::value_type |
| rg(since C++23) | a value of typeR |
| il(since C++11) | a value of typestd::initializer_list<C::value_type> |
| n | a value of typeC::size_type |
| p | a validconst iterator intov |
| q | avalid dereferenceable const iterator intov |
| q1,q2 | const iterators intov such that[q1, q2) is a valid range |
| t | a value(until C++11)anlvalue or const rvalue(since C++11) of typeC::value_type |
| rv(since C++11) | a non-const rvalue of typeC::value_type |
| args(since C++11) | a function parameter pack with the patternArg&& |
C satisfies the requirements ofSequenceContainer if all following conditions are satisfied:
C satisfies the requirements ofContainer.| Basic operations (required for all sequence containers in thestandard library exceptstd::array(since C++11)) | |||
|---|---|---|---|
| Statement | Semantics[1] | ||
| C c(n, t); | Effect | Constructs the sequence container holdingn copies oft. | |
| Precondition |
| ||
| Postcondition | std::distance(c.begin(), c.end()) isn. | ||
| C c(i, j); | Effect | Constructs the sequence container equal, element-wise, to the range[i, j).
| |
| Precondition |
| ||
| Postcondition | std::distance(c.begin(), c.end()) isstd::distance(i, j). | ||
| Expression | Type | Semantics | |
| C(std::from_range, rg) (since C++23) | C | Effect | Constructs the sequence container equal, element-wise, to the rangerg.
|
| Precondition | T isEmplaceConstructible intoX from*ranges::begin(rg). | ||
| Postcondition | std::distance(begin(), end()) isranges::distance(rg). | ||
| C(il) (since C++11) | C | Equivalent toC(il.begin(), il.end()). | |
| v= il (since C++11) | C& | Effect | Assigns the range represented byil intov.[2] |
| Return value | *this | ||
| Precondition | T isCopyInsertable intoC andCopyAssignable. | ||
| Postcondition | Existing elements ofv are either destroyed or assigned to. | ||
| v.emplace(p, args) (since C++11) | Iter | Effect | Insert an object of typeT, constructed withstd::forward<Args>(args)... beforep. |
| Return value | An iterator that points to the new element constructed fromargs intov. | ||
| Precondition | T isEmplaceConstructible intoC fromargs. | ||
| v.insert(p, t) | Iter | Effect | Inserts a copy oft beforep. |
| Return value | An iterator that points to the copy oft inserted intov. | ||
| Precondition |
| ||
| v.insert(p, rv) (since C++11) | Iter | Effect | Inserts a copy ofrv beforep, possibly using move semantics. |
| Return value | An iterator that points to the copy ofrv inserted intov. | ||
| Precondition | T isMoveInsertable intoC. | ||
| v.insert(p, n, t) | Iter | Effect | Insertsn copies oft beforep. |
| Return value | An iterator that points to the copy of the first element inserted intov, orp ifn is0. | ||
| Precondition |
| ||
| v.insert(p, i, j) | Iter | Effect | Inserts copies of elements in[i, j) beforep.
|
| Return value | An iterator that points to the copy of the first element inserted intov, orp ifi== j istrue. | ||
| Precondition |
| ||
| v.insert_range(p, rg) (since C++23) | Iter | Effect | Inserts copies of elements inrg beforep.
|
| Return value | An iterator that points to the copy of the first element inserted intov, orp ifrg is empty. | ||
| Precondition |
| ||
| v.insert(p, il) (since C++11) | Iter | Equivalent tov.insert(p, il.begin(), il.end()). | |
| v.erase(q) | Iter | Effect | Erases the element pointed to byq. |
| Return value | An iterator that points to the element immediately followingq prior to the element being erased, orv.end() if no such element exists. | ||
| v.erase(q1, q2) | Iter | Effect | Erases elements in[q1, q2). |
| Return value | An iterator that points to the element pointed to byq2 prior to any elements being erased, orv.end() if no such element exists. | ||
| v.clear() | void | Effect | Destroys all elements inv.
|
| Postcondition | v.empty() istrue. | ||
| Complexity | Linear. | ||
| v.assign(i, j) | void | Effect | Replaces elements inv with a copy of[i, j).
|
| Precondition |
| ||
| v.assign_range(rg) (since C++23) | void | Effect | Replaces elements inv with a copy of each element inrg.
|
| Precondition |
| ||
| v.assign(il) (since C++11) | void | Equivalent tov.assign(il.begin(), il.end()). | |
| v.assign(n, t) | void | Effect | Replaces elements inv withn copies oft. |
| Precondition |
| ||
| Extra operations[3] (only required for specified sequence containers, omitting std::) | |||
| Expression | Type | Semantics | |
| v.front() | Ref | Containers | basic_string,array,vector,inplace_vector,deque,list,forward_list |
| Return value | *v.begin() | ||
| cv.front() | CRef | Containers | basic_string,array,vector,inplace_vector,deque,list,forward_list |
| Return value | *cv.begin() | ||
| v.back() | Ref | Containers | basic_string,array,vector,inplace_vector,deque,list |
| Equivalent toauto tmp= v.end();--tmp;return*tmp;[4]. | |||
| cv.back() | CRef | Containers | basic_string,array,vector,inplace_vector,deque,list |
| Equivalent toauto tmp= cv.end();--tmp;return*tmp;[5]. | |||
| v.emplace_front(args) (since C++11) | void | Containers | deque,list,forward_list |
| Effect | Prepends an object of typeT constructed withstd::forward<Args>(args).... | ||
| Return value | v.front() | ||
| Precondition | T isEmplaceConstructible intoC fromargs. | ||
| v.emplace_back(args) (since C++11) | void | Containers | vector,inplace_vector,deque,list |
| Effect | Appends an object of typeT constructed withstd::forward<Args>(args).... | ||
| Return value | v.back() | ||
| Precondition | T isEmplaceConstructible intoC fromargs. | ||
| v.push_front(t) | void | Containers | deque,list,forward_list |
| Effect | Prepends a copy oft. | ||
| Precondition |
| ||
| v.push_front(rv) (since C++11) | void | Containers | deque,list,forward_list |
| Effect | Prepends a copy ofrv, possibly using move semantics. | ||
| Precondition | T isMoveInsertable intoC. | ||
| v.prepend_range(rg) (since C++23) | void | Containers | deque,list,forward_list |
| Effect | Inserts[6] copies of elements inrg beforev.begin().
| ||
| Precondition | T isEmplaceConstructible intoC from*ranges::begin(rg). | ||
| v.push_back(t) | void | Containers | basic_string,vector,inplace_vector,deque,list |
| Effect | Appends a copy oft. | ||
| Precondition |
| ||
| v.push_back(rv) (since C++11) | void | Containers | basic_string,vector,inplace_vector,deque,list |
| Effect | Appends a copy ofrv, possibly using move semantics. | ||
| Precondition | T isMoveInsertable intoC. | ||
| v.append_range(rg) (since C++23) | void | Containers | vector,inplace_vector,deque,list |
| Effect | Inserts[6] copies of elements inrg beforev.end().
| ||
| Precondition | T isEmplaceConstructible intoC from*ranges::begin(rg). | ||
| v.pop_front() | void | Containers | deque,list,forward_list |
| Effect | Destroys the first element. | ||
| Precondition | a.empty() isfalse. | ||
| v.pop_back() | void | Containers | basic_string,vector,inplace_vector,deque,list |
| Effect | Destroys the last element. | ||
| Precondition | a.empty() isfalse. | ||
| v[n] | Ref | Containers | basic_string,array,vector,inplace_vector,deque |
| Equivalent toreturn*(v.begin()+ n);. | |||
| cv[n] | CRef | Containers | basic_string,array,vector,inplace_vector,deque |
| Equivalent toreturn*(cv.begin()+ n);. | |||
| v.at(n) | Ref | Containers | basic_string,array,vector,inplace_vector,deque |
| Return value | *(v.begin()+ n) | ||
| Exceptions | Throwsstd::out_of_range ifn>= v.size() istrue. | ||
| cv.at(n) | CRef | Containers | basic_string,array,vector,inplace_vector,deque |
| Return value | *(cv.begin()+ n) | ||
| Exceptions | Throwsstd::out_of_range ifn>= cv.size() istrue. | ||
| Notes | |||
| |||
Additionally, for every sequence container:
insert,append,assign,replace that take two input iterators do not participate in overload resolution if the corresponding template argument does not satisfyLegacyInputIterator.
| (since C++17) |
The following standard library string types and containers satisfy theSequenceContainer requirements:
| stores and manipulates sequences of characters (class template)[edit] | |
(C++11) | fixed-sized inplace contiguous array (class template)[edit] |
| resizable contiguous array (class template)[edit] | |
(C++26) | resizable, fixed capacity, inplace contiguous array (class template)[edit] |
| double-ended queue (class template)[edit] | |
(C++11) | singly-linked list (class template)[edit] |
| doubly-linked list (class template)[edit] |
| Container | Pros | Cons |
|---|---|---|
| std::vector | Fast access, contiguous storage | Mostly inefficient insertions/deletions |
| std::inplace_vector | Fast access, inplace contiguous storage | Fixed capacity and mostly inefficient insertions/deletions |
| std::array | Fast access, inplace contiguous storage | Fixed number of elements and no insertion/deletion |
| std::deque | Fast access, efficient insertion/deletion at the beginning/end | Inefficient insertion/deletion in the middle of the sequence |
| std::list std::forward_list | Efficient insertion/deletion in the middle of the sequence | Access is mostly linear-time |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 139 | C++98 | the optional operations were not required to be implemented for the designated containers | required with amortized time |
| LWG 149 | C++98 | v.insert(p, t) returnedIter whilev.insert(p, n, t) andv.insert(p, n, t) returnedvoid | they all returnIter |
| LWG 151 | C++98 | q1 was required to be dereferenceable[1] | it can be non-dereferenceable |
| LWG 355 | C++98 | callingv.back() orv.pop_back() would execute--v.end(), which is dangerous[2] | decrements a copy ofv.end() instead |
| LWG 589 | C++98 | the elements thati andj refer to might not be convertible to C::value_type | they are implicitly convertible to C::value_type |
| LWG 2194 | C++11 | std::queue,std::priority_queue and std::stack were alsoSequenceContainers[3] | they are notSequenceContainers |
| LWG 2231 | C++11 | the complexity requirement ofv.clear() was mistakenly omitted in C++11 | complexity reaffirmed as linear |
| LWG 3927 | C++98 | operator[] had no implicit requirement | added the implicit requirement |