A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../../cpp/../cpp/container/inplace_vector/push_back.html below:

std::inplace_vector<T,N>::push_back - cppreference.com

constexpr reference push_back( const T& value );

(1) (since C++26)

constexpr reference push_back( T&& value );

(2) (since C++26)

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.

No iterators or references are invalidated, except end(), which is invalidated if the insertion occurs.

[edit] 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). [edit] Return value

back(), i.e. a reference to the inserted element.

[edit] Complexity

Constant.

[edit] Exceptions

If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).

[edit] Example
#include <inplace_vector>
#include <new>
#include <print>
#include <string>
 
int main()
{
    std::inplace_vector<std::string, 2> fauna;
    std::string dog{"\N{DOG}"};
 
    fauna.push_back("\N{CAT}"); // overload (1)
    fauna.push_back(std::move(dog)); // overload (2)
    std::println("fauna = {}", fauna);
 
    try
    {
        fauna.push_back("\N{BUG}"); // throws: there is no space
    }
    catch(const std::bad_alloc& ex)
    {
        std::println("{}", ex.what());
    }
    std::println("fauna = {}", fauna);
}

Possible output:

fauna = ["🐈", "🐕"]
std::bad_alloc
fauna = ["🐈", "🐕"]
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4