Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |container‎ |inplace vector

      [edit template]
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
       
      constexprvoid resize( size_type count);
      (1)(since C++26)
      constexprvoid resize( size_type count,const value_type& value);
      (2)(since C++26)

      Resizes the container to containcount elements:

      • Ifcount is equal to the current size, does nothing.
      • If the current size is greater thancount, the container is reduced to its firstcount elements.
      • If the current size is less thancount, then:
      1) Additionaldefault-inserted elements are appended.
      2) Additional copies ofvalue are appended.

      Contents

      [edit]Parameters

      count - new size of the container
      value - the value to initialize the new elements with
      Type requirements
      -
      If the following condition is satisfied, the behavior is undefined:
      1)T is notDefaultInsertable intoinplace_vector.
      2)T is notCopyInsertable intoinplace_vector.

      [edit]Complexity

      Linear in the difference between the current size andcount.

      Exceptions

      1,2) Throwsstd::bad_alloc ifcount> N.

      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 <print> int main(){std::inplace_vector<int,6> v(6,9);std::println("Initially, v = {}", v);     v.resize(2);std::println("After resize(2), v = {}", v);     v.resize(4);std::println("After resize(4), v = {}", v);     v.resize(6,-1);std::println("After resize(6, -1), v = {}", v); try{std::print("Trying resize(13): ");        v.resize(13);// throws, because count > N; v is left unchanged}catch(conststd::bad_alloc& ex){std::println("ex.what(): {}", ex.what());}std::println("After exception, v = {}", v);}

      Possible output:

      Initially, v = [9, 9, 9, 9, 9, 9]After resize(2), v = [9, 9]After resize(4), v = [9, 9, 0, 0]After resize(6, -1), v = [9, 9, 0, 0, -1, -1]Trying resize(13): ex.what(): std::bad_allocAfter exception, v = [9, 9, 0, 0, -1, -1]


      [edit]See also

      [static]
      returns the maximum possible number of elements
      (public static member function)[edit]
      returns the number of elements
      (public member function)[edit]
      [static]
      returns the number of elements that can be held in currently allocated storage
      (public static member function)[edit]
      checks whether the container is empty
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector/resize&oldid=175137"

      [8]ページ先頭

      ©2009-2026 Movatter.jp