Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::inplace_vector<T,N>::push_back

      From cppreference.com
      <cpp‎ |container‎ |inplace vector
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
       
      constexpr reference push_back(const T& value);
      (1)(since C++26)
      constexpr reference push_back( T&& value);
      (2)(since C++26)

      Appends the given elementvalue to the end of the container.

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

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

      Contents

      [edit]Parameters

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

      [edit]Return value

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

      [edit]Complexity

      Constant.

      [edit]Exceptions

      • std::bad_alloc ifsize()== capacity() before invocation.
      • Any exception thrown by initialization of inserted element.

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

      [edit]Example

      Run this code
      #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(conststd::bad_alloc& ex){std::println("{}", ex.what());}std::println("fauna = {}", fauna);}

      Possible output:

      fauna = ["🐈", "🐕"]std::bad_allocfauna = ["🐈", "🐕"]

      [edit]See also

      constructs an element in-place at the end
      (public member function)[edit]
      adds a range of elements to the end
      (public member function)[edit]
      tries to add an element to the end
      (public member function)[edit]
      tries to construct an element in-place at the end
      (public member function)[edit]
      tries to add a range of elements to the end
      (public member function)[edit]
      unconditionally adds an element to the end
      (public member function)[edit]
      unconditionally constructs an element in-place at the end
      (public member function)[edit]
      removes the last element
      (public member function)[edit]
      creates astd::back_insert_iterator of type inferred from the argument
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector/push_back&oldid=176284"

      [8]ページ先頭

      ©2009-2026 Movatter.jp