Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::vector<T,Allocator>::insert

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

      [edit template]
       
       
       
      std::vector
      Member types
      Member functions
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
      Deduction guides(C++17)
       
      iterator insert( const_iterator pos,const T& value);
      (1)(constexpr since C++20)
      iterator insert( const_iterator pos, T&& value);
      (2)(since C++11)
      (constexpr since C++20)
      iterator insert( const_iterator pos,
                       size_type count,const T& value);
      (3)(constexpr since C++20)
      template<class InputIt>
      iterator insert( const_iterator pos, InputIt first, InputIt last);
      (4)(constexpr since C++20)
      iterator insert( const_iterator pos,std::initializer_list<T> ilist);
      (5)(since C++11)
      (constexpr since C++20)

      Inserts elements at the specified location in the container.

      1) Inserts a copy ofvalue beforepos.
      If any of the following conditions is satisfied, the behavior is undefined:
      (since C++11)
      2) Insertsvalue beforepos, possibly using move semantics.
      If any of the following conditions is satisfied, the behavior is undefined:
      3) Insertscount copies of thevalue beforepos.
      If any of the following conditions is satisfied, the behavior is undefined:
      (since C++11)
      4) Inserts elements from range[firstlast) beforepos.

      This overload has the same effect as overload(3) ifInputIt is an integral type.

      (until C++11)

      This overload participates in overload resolution only ifInputIt satisfies the requirements ofLegacyInputIterator.

      (since C++11)
      If any of the following conditions is satisfied, the behavior is undefined:
      (since C++11)
      • first orlast are iterators into*this.
      5) Inserts elements from initializer listilist beforepos.
      Equivalent toinsert(pos, ilist.begin(), ilist.end()).


      If after the operation the newsize() is greater than oldcapacity() a reallocation takes place, in which case all iterators (including theend() iterator) and all references to the elements are invalidated. Otherwise, only the iterators and references before the insertion point remain valid.

      Contents

      [edit]Parameters

      pos - iterator before which the content will be inserted
      value - element value to insert
      count - number of elements to insert
      first, last - the pair of iterators defining therange of elements to insert
      ilist -std::initializer_list to insert the values from

      [edit]Return value

      1,2) Iterator pointing to the insertedvalue.
      3-5) Iterator pointing to the first element inserted, orpos if no element is inserted.

      [edit]Complexity

      If reallocation happens, linear in the number of elements of thevector after insertion; otherwise, linear in the number of elements inserted plusstd::distance(pos, end()).

      [edit]Exceptions

      If an exception is thrown other than by

      • the copy constructor ofT,
      • the move constructor ofT,
      (since C++11)
      • the copy assignment operator ofT,
      • the move assignment operator ofT,
      (since C++11)
      • anyInputIt operation,

      these functions have no effect (strong exception safety guarantee).

      If an exception is thrown when inserting a single element at the end, andT isCopyInsertable into*this orstd::is_nothrow_move_constructible<T>::value istrue, this function has no effect (strong exception guarantee). Otherwise, if an exception is thrown by the move constructor of a non-CopyInsertableT, the effects are unspecified.

      (since C++11)

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <string_view>#include <vector> namespace stq{void println(std::string_view rem,conststd::vector<int>& container){std::cout<< rem.substr(0, rem.size()-2)<<'[';bool first{true};for(constint x: container)std::cout<<(first? first=false,"":", ")<< x;std::cout<<"]\n";}} int main(){std::vector<int> c1(3,100);    stq::println("1. {}", c1); auto pos= c1.begin();    pos= c1.insert(pos,200);// overload (1)    stq::println("2. {}", c1);     c1.insert(pos,2,300);// overload (3)    stq::println("3. {}", c1); // pos no longer valid, get a new one:    pos= c1.begin(); std::vector<int> c2(2,400);    c1.insert(std::next(pos,2), c2.begin(), c2.end());// overload (4)    stq::println("4. {}", c1); int arr[]={501,502,503};    c1.insert(c1.begin(), arr, arr+std::size(arr));// overload (4)    stq::println("5. {}", c1);     c1.insert(c1.end(),{601,602,603});// overload (5)    stq::println("6. {}", c1);}

      Output:

      1. [100, 100, 100]2. [200, 100, 100, 100]3. [300, 300, 200, 100, 100, 100]4. [300, 300, 400, 400, 200, 100, 100, 100]5. [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100]6. [501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100, 601, 602, 603]

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 149C++98overloads(3) and(4) returned nothingreturns an iterator
      LWG 247C++98the complexity was only specified for overload(3)specified for all overloads
      LWG 406C++98the strong exception guarantee also applied if
      the exception is thrown by anInputIt operation
      no guarantee in this case

      [edit]See also

      (C++11)
      constructs element in-place
      (public member function)[edit]
      adds an element to the end
      (public member function)[edit]
      creates astd::insert_iterator of type inferred from the argument
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/vector/insert&oldid=121661"

      [8]ページ先頭

      ©2009-2025 Movatter.jp