Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

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

      [edit template]
       
       
       
      std::inplace_vector
      Member types
      Member functions
      Non-member functions
       
      constexprvoid assign( size_type count,const T& value);
      (1)(since C++26)
      template<class InputIt>
      constexprvoid assign( InputIt first, InputIt last);
      (2)(since C++26)
      constexprvoid assign(std::initializer_list<T> ilist);
      (3)(since C++26)

      Replaces the contents of the container.

      1) Replaces the contents withcount copies of valuevalue.
      2) Replaces the contents with copies of those in the range[firstlast).
      This overload participates in overload resolution only ifInputIt satisfies the requirements ofLegacyInputIterator.
      Iffirst orlast is an iterator into*this, the behavior is undefined.
      3) Replaces the contents with the elements fromilist.
      This section is incomplete

      Contents

      [edit]Parameters

      count - the new size of the container
      value - the value to initialize elements of the container with
      first, last - the pair of iterators defining the sourcerange of elements to copy
      ilist -std::initializer_list to copy the values from

      [edit]Complexity

      1) Linear incount.
      2) Linear in distance betweenfirst andlast.
      3) Linear inilist.size().

      Exceptions

      1)std::bad_alloc, ifcount> capacity().
      2)std::bad_alloc, ifranges::distance(first, last)> capacity().
      3)std::bad_alloc, ifilist.size()> capacity().
      1-3) Any exception thrown by initialization of inserted elements.

      [edit]Example

      The following code usesassign to add several characters to astd::inplace_vector<char,5>:

      Run this code
      #include <inplace_vector>#include <iterator>#include <new>#include <print> int main(){std::inplace_vector<char,5> chars;     chars.assign(4,'a');// overload (1)std::println("{}", chars); constchar extra[3]{'a','b','c'};    chars.assign(std::cbegin(extra),std::cend(extra));// overload (2)std::println("{}", chars);     chars.assign({'C','+','+','2','6'});// overload (3)std::println("{}", chars); try{        chars.assign(8,'x');// throws: count > chars.capacity()}catch(conststd::bad_alloc&){std::println("std::bad_alloc #1");} try{constchar bad[8]{'?'};// ranges::distance(bad) > chars.capacity()        chars.assign(std::cbegin(bad),std::cend(bad));// throws}catch(conststd::bad_alloc&){std::println("std::bad_alloc #2");} try{constauto l={'1','2','3','4','5','6'};        chars.assign(l);// throws: l.size() > chars.capacity()}catch(conststd::bad_alloc&){std::println("std::bad_alloc #3");}}

      Output:

      ['a', 'a', 'a', 'a']['a', 'b', 'c']['C', '+', '+', '2', '6']std::bad_alloc #1std::bad_alloc #2std::bad_alloc #3

      [edit]See also

      assigns a range of values to the container
      (public member function)[edit]
      assigns values to the container
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/inplace_vector/assign&oldid=175321"

      [8]ページ先頭

      ©2009-2025 Movatter.jp