Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/Container"

From cppreference.com
< cpp‎ | named req
(spelling)
m (improved formatting)
Line 5: Line 5:
  
 
===Requirements===
 
===Requirements===
* {{ttb|C}} Container type
+
* {{ttb|C}} container type;
* {{ttb|T}} Element type
+
* {{ttb|T}} element type;
* {{ttb|a}}, {{ttb|b}} Objects of type {{ttb|C}}
+
* {{ttb|a}}, {{ttb|b}} objects of type {{ttb|C}}.
 +
 
 
====Types====
 
====Types====
 
{|class=wikitable
 
{|class=wikitable
Line 27: Line 28:
 
|{{tt|size_type}}||unsigned integer||large enough to represent all positive values of {{ttb|difference_type}}
 
|{{tt|size_type}}||unsigned integer||large enough to represent all positive values of {{ttb|difference_type}}
 
|}
 
|}
 +
 
====Methods and operators====
 
====Methods and operators====
 
{|class=wikitable
 
{|class=wikitable
 
!expression||return type||semantics||conditions||complexity
 
!expression||return type||semantics||conditions||complexity
 
|-
 
|-
|{{c|C()}}||C||Creates an empty container|| Post: C().empty() == true||Constant
+
|{{c|C()}}||C||creates an empty container|| Post: C().empty() == true||Constant
 
|-
 
|-
|{{c|C(a)}}||C||Create a copy of {{ttb|a}}||Pre: T must be {{concept|CopyInsertable}}<br/>Post: a == C(a)||Linear
+
|{{c|C(a)}}||C||create a copy of {{ttb|a}}||Pre: T must be {{concept|CopyInsertable}}<br/>Post: a == C(a)||Linear
 
|-
 
|-
|{{c|a {{=}} b}}||C&||All elements of {{ttb|a}} are destroyed or move assigned to elements of b||Post: a == b||Linear
+
|{{c|a {{=}} b}}||C&||all elements of {{ttb|a}} are destroyed or move assigned to elements of b||Post: a == b||Linear
 
|-
 
|-
|{{c|(&a)->~C()}}||void||Destroy all elements and free all memory||  ||Linear
+
|{{c|(&a)->~C()}}||void||destroy all elements and free all memory||  ||Linear
 
|-
 
|-
 
|{{c|a.begin()}}||(const_)iterator||Iterator to the first element|| ||Constant
 
|{{c|a.begin()}}||(const_)iterator||Iterator to the first element|| ||Constant
Line 49: Line 51:
 
|{{c|a {{==}} b}} || convertible to bool || {{c|std::equal(a.begin(), a.end(), b.begin(), b.end())}}{{mark since c++14}} ||Pre: T must be {{concept|EqualityComparable}} || Constant<ref>always linear for {{lc|std::forward_list}}</ref> if {{tt|a.size() !{{=}} b.size()}}, linear otherwise
 
|{{c|a {{==}} b}} || convertible to bool || {{c|std::equal(a.begin(), a.end(), b.begin(), b.end())}}{{mark since c++14}} ||Pre: T must be {{concept|EqualityComparable}} || Constant<ref>always linear for {{lc|std::forward_list}}</ref> if {{tt|a.size() !{{=}} b.size()}}, linear otherwise
 
|-
 
|-
|{{c|a !{{=}} b}} || convertible to bool || {{c|!(a{{==}}b)}} || || Linear
+
|{{c|a !{{=}} b}} || convertible to bool || {{c|!(a {{==}} b)}} || || Linear
 
|-
 
|-
 
|{{c|a.swap(b)}} || void || exchanges the values of a and b ||  
 
|{{c|a.swap(b)}} || void || exchanges the values of a and b ||  
 
| Constant<ref name="array">{{mark since c++11}} Linear for {{lc|std::array}}</ref><ref name="should">{{mark until c++11}} Not strictly constant</ref>
 
| Constant<ref name="array">{{mark since c++11}} Linear for {{lc|std::array}}</ref><ref name="should">{{mark until c++11}} Not strictly constant</ref>
 
|-
 
|-
|{{c|swap(a,b)}} || void || {{c|a.swap(b)}} || || Constant<ref name="array"/>
+
|{{c|swap(a, b)}} || void || {{c|a.swap(b)}} || || Constant<ref name="array"/>
 
|-
 
|-
|{{c|a.size()}} || size_type || {{c|distance(a.begin(),a.end())}} || || Constant<ref name="should" />
+
|{{c|a.size()}} || size_type || {{c|distance(a.begin(), a.end())}} || || Constant<ref name="should" />
 
|-
 
|-
 
|{{c|a.max_size()}} || size_type || b.size() where b is the largest possible container || || Constant<ref name="should" />
 
|{{c|a.max_size()}} || size_type || b.size() where b is the largest possible container || || Constant<ref name="should" />
Line 70: Line 72:
 
{{rev |since=c++14|
 
{{rev |since=c++14|
 
Given
 
Given
 
 
* {{tt|i}} and {{tt|j}}, objects of a container's {{tt|iterator}} type,
 
* {{tt|i}} and {{tt|j}}, objects of a container's {{tt|iterator}} type,
 
+
in the expressions {{c|i {{==}} j}}, {{c|i !{{=}} j}}, {{c|i < j}}, {{c|i <{{=}} j}}, {{c|i >{{=}} j}}, {{c|i > j}}, {{c|i - j}}, either or both may be replaced by an object of the container's {{tt|const_iterator}} type referring to the same element with no change in semantics.
In the expressions {{c|i {{==}} j}}, {{c|i !{{=}} j}}, {{c|i < j}}, {{c|i <{{=}} j}}, {{c|i >{{=}} j}}, {{c|i > j}}, {{c|i - j}}, either or both may be replaced by an object of the container's {{tt|const_iterator}} type referring to the same element with no change in semantics.
+
 
}}
 
}}
 
{{rev end}}
 
{{rev end}}

Revision as of 01:09, 2 February 2016

Template:cpp/concept/title Template:cpp/concept/navbar

A Container is an object used to store other objects and taking care of the management of the memory used by the objects it contains.

Contents

Requirements

  • C container type;
  • T element type;
  • a, b objects of type C.

Types

name type notes
value_type T Template:concept
reference T&
const_reference const T&
iterator iterator pointing to T Template:concept
convertible to const_iterator
const_iterator const iterator pointing to T Template:concept
difference_type signed integer must be the same as iterator_traits::difference_type for iterator and const_iterator
size_type unsigned integer large enough to represent all positive values of difference_type

Methods and operators

expression return type semantics conditions complexity
C() C creates an empty container Post: C().empty() == true Constant
C(a) C create a copy of a Pre: T must be Template:concept
Post: a == C(a)
Linear
a = b C& all elements of a are destroyed or move assigned to elements of b Post: a == b Linear
(&a)->~C() void destroy all elements and free all memory Linear
a.begin() (const_)iterator Iterator to the first element Constant
a.end() (const_)iterator Iterator to one past the last element Constant
a.cbegin()(since C++11) const_iterator const_cast<const C&>(a).begin() Constant
a.cend()(since C++11) const_iterator const_cast<const C&>(a).end() Constant
a == b convertible to bool std::equal(a.begin(), a.end(), b.begin(), b.end())(since C++14) Pre: T must be Template:concept Constant[1] if a.size() != b.size(), linear otherwise
a != b convertible to bool !(a == b) Linear
a.swap(b) void exchanges the values of a and b Constant[2][3]
swap(a, b) void a.swap(b) Constant[2]
a.size() size_type distance(a.begin(), a.end()) Constant[3]
a.max_size() size_type b.size() where b is the largest possible container Constant[3]
a.empty() convertible to bool a.begin() == a.end() Constant
notes
  1. always linear for std::forward_list
  2. 2.0 2.1 (since C++11) Linear for std::array
  3. 3.0 3.1 3.2 (until C++11) Not strictly constant

Given

  • i and j, objects of a container's iterator type,

in the expressions i == j, i != j, i < j, i <= j, i >= j, i > j, i - j, either or both may be replaced by an object of the container's const_iterator type referring to the same element with no change in semantics.

(since C++14)

Container data races

see container thread safety

Other concepts

C
T