Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/push back"

From cppreference.com
m (fmt)
m (+reallocation note for vector, there are still some duplications on vector page, WIP)
Line 28: Line 28:
  
 
@1@ The new element is initialized as a copy of {{c|value}}.
 
@1@ The new element is initialized as a copy of {{c|value}}.
 
 
@2@ {{c|value}} is moved into the new element.
 
@2@ {{c|value}} is moved into the new element.
  
{{cpp/container/note_iterator_invalidation|{{{1|}}}|push_back}}
+
{{cpp/container/vector/reallocation note|{{{1|}}}}}
 +
{{cpp/container/note iterator invalidation|{{{1|}}}|push_back}}
  
 
===Parameters===
 
===Parameters===
Line 55: Line 55:
 
{{#ifeq:{{{1|}}}|vector|
 
{{#ifeq:{{{1|}}}|vector|
 
{{rrev|since=c++11|
 
{{rrev|since=c++11|
If {{tt|T}}'s move constructor is not {{c|noexcept}} and T is not {{named req|CopyInsertable}} into {{c|*this}}, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified.
+
If {{tt|T}}'s move constructor is not {{c|noexcept}} and {{tt|T}} is not {{named req|CopyInsertable}} into {{c|*this}}, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified.
 
}}
 
}}
 
<!---->
 
<!---->
 
===Notes===
 
===Notes===
Calling {{tt|push_back}} causes reallocation if {{lc|size}}{{tt|1=() ==}}{{nbspt}}{{lc|capacity}}{{tt|()}}. Some implementations throw {{lc|std::length_error}} when {{tt|push_back}} causes a reallocation that exceeds {{lc|max_size}} (due to an implicit call to an equivalent of {{lc|reserve}}{{tt|(}}{{lc|size}}{{tt|() + 1))}}.
+
Some implementations throw {{lc|std::length_error}} when {{tt|push_back}} causes a reallocation that exceeds {{lc|max_size}} (due to an implicit call to an equivalent of {{lc|reserve}}{{tt|(}}{{lc|size}}{{tt|() + 1))}}.
 
}}
 
}}
 
<!---->
 
<!---->

Revision as of 06:11, 10 August 2023

void push_back( const T& value );
(1) (since {std})
void push_back( T&& value );
(2) (since C++11)

Appends the given element value to the end of the container.

1) The new element is initialized as a copy of value.
2) value is moved into the new element.


Contents

Parameters

value - the value of the element to append
Type requirements
-
T must meet the requirements of CopyInsertable in order to use overload (1).
-
T must meet the requirements of MoveInsertable in order to use overload (2).

Return value

(none)

Complexity

Exceptions

If an exception is thrown (which can be due to Allocator::allocate() or element copy/move constructor/assignment), this function has no effect (strong exception guarantee).

Example

#include <iomanip>
#include <iostream>
#include <string>
#include <list>
 
int main()
{
    std::list<std::string> letters;
 
    letters.push_back("abc");
    std::string s{"def"};
    letters.push_back(std::move(s));
 
    std::cout << "std::list letters holds: ";
    for (auto&& e : letters)
        std::cout << std::quoted(e) << ' ';
 
    std::cout << "\nMoved-from string s holds: " << std::quoted(s) << '\n';
}

Possible output:

std::list letters holds: "abc" "def"
Moved-from string s holds: ""

See also

constructs an element in-place at the end
(public member function of std::{{{1}}}) [edit]
inserts an element to the beginning
(public member function of std::{{{1}}}) [edit]
removes the last element
(public member function of std::{{{1}}}) [edit]
creates a std::back_insert_iterator of type inferred from the argument
(function template) [edit]