Przejdź do głównej zawartości

std::deque push_back() method

// (1) Non const version only
void push_back( const T& value );

// (2) Non const version only
void push_back( T&& value );

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.
Invalidation

All iterators, including the past-the-end iterator, are invalidated.
No references are invalidated.

Parameters

  • value - the value of the element to append

Type requirements

  • (1) - T (the container's element type) must meet the requirements of CopyInsertable.
  • (2) - T (the container's element type) must meet the requirements of MoveInsertable.

Return value

(none)

Complexity

Constant - O(1).

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

Main.cpp
#include <deque>
#include <iostream>
#include <iomanip>
#include <string>

int main()
{
std::deque<std::string> letters;

letters.push_back("abc");
std::string s{"def"};
letters.push_back(std::move(s));

std::cout << "std::deque `letters` holds: ";
for (auto&& e : letters) std::cout << std::quoted(e) << ' ';

std::cout << "\nMoved-from string `s` holds: " << std::quoted(s) << '\n';
}
Output
std::deque `letters` holds: "abc" "def"
Moved-from string `s` holds: ""
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::deque push_back() method

// (1) Non const version only
void push_back( const T& value );

// (2) Non const version only
void push_back( T&& value );

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.
Invalidation

All iterators, including the past-the-end iterator, are invalidated.
No references are invalidated.

Parameters

  • value - the value of the element to append

Type requirements

  • (1) - T (the container's element type) must meet the requirements of CopyInsertable.
  • (2) - T (the container's element type) must meet the requirements of MoveInsertable.

Return value

(none)

Complexity

Constant - O(1).

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

Main.cpp
#include <deque>
#include <iostream>
#include <iomanip>
#include <string>

int main()
{
std::deque<std::string> letters;

letters.push_back("abc");
std::string s{"def"};
letters.push_back(std::move(s));

std::cout << "std::deque `letters` holds: ";
for (auto&& e : letters) std::cout << std::quoted(e) << ' ';

std::cout << "\nMoved-from string `s` holds: " << std::quoted(s) << '\n';
}
Output
std::deque `letters` holds: "abc" "def"
Moved-from string `s` holds: ""
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.