Namespaces
Variants

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

cppreference.net에서
 
 
 
 
constexpr iterator insert( const_iterator pos, const T& value );
(1) (C++26부터)
constexpr iterator insert( const_iterator pos, T&& value );
(2) (C++26부터)
constexpr iterator insert( const_iterator pos, size_type count, const T& value );
(3) (C++26부터)
template< class InputIt >
constexpr iterator insert( const_iterator pos, InputIt first, InputIt last );
(4) (C++26부터)
constexpr iterator insert( const_iterator pos, std::initializer_list<T> ilist );
(5) (C++26부터)

지정된 위치에 요소를 컨테이너에 삽입합니다.

1) 의 복사본을 value 앞에 pos.
2) 를 삽입합니다 value 앞에 pos, 이동 의미론을 사용할 수 있습니다.
3) 를 삽입합니다 count 개의 value 앞에 pos.
4) 범위에서 요소를 삽입합니다 [first, last) 앞에 pos. 이 오버로드는 오버로드 해결에 참여하는 경우는 오직 InputIt 는 LegacyInputIterator (오버로드 (3)과의 모호성을 피하기 위해).
의 각 반복자는 [first, last) 한 번 역참조됩니다.
만약 first 와 last 는로의 반복자입니다 *this, 동작은 정의되지 않습니다.
5) 초기화 목록에서 요소를 삽입합니다 ilist 앞에 pos. 동등한 표현: insert(pos, ilist.begin(), ilist.end());.

매개변수

pos - 내용이 삽입될 반복자 앞 위치 ( pos 는 end() 반복자일 수 있음)
value - 삽입할 요소 값
count - 삽입할 요소 개수
first, last - 삽입할 요소들의 소스 범위 를 정의하는 반복자 쌍
ilist - std::initializer_list 로부터 값을 삽입
타입 요구사항
-
T 는 오버로드 (1) 사용을 위해 CopyInsertable 요구사항을 충족해야 함
-
T 는 오버로드 (2) 사용을 위해 MoveInsertable 요구사항을 충족해야 함
-
T 는 오버로드 (3) 사용을 위해 CopyAssignable 와 CopyInsertable 요구사항을 충족해야 함
-
T 는 오버로드 (4,5) 사용을 위해 EmplaceConstructible 요구사항을 충족해야 함

반환값

1,2) 삽입된 값을 가리키는 value 반복자.
3) 삽입된 첫 번째 요소를 가리키는 반복자, 또는 pos 만약 count == 0 인 경우.
4) 삽입된 첫 번째 요소를 가리키는 반복자, 또는 pos (만약 first == last 인 경우).
5) 삽입된 첫 번째 요소를 가리키는 반복자, 또는 pos (만약 ilist 가 비어 있는 경우).

복잡도

삽입된 원소의 수에 더해 컨테이너의 pos 와 end() 사이의 거리에 선형적으로 비례합니다.

예외

예제

#include <initializer_list>
#include <inplace_vector>
#include <iterator>
#include <new>
#include <print>
int main()
{
    std::inplace_vector<int, 14> v(3, 100);
    std::println("1. {}", v);
    auto pos = v.begin();
    pos = v.insert(pos, 200); // 오버로드 (1)
    std::println("2. {}", v);
    v.insert(pos, 2, 300); // 오버로드 (3)
    std::println("3. {}", v);
    int arr[] = {501, 502, 503};
    v.insert(v.begin(), arr, arr + std::size(arr)); // 오버로드 (4)
    std::println("4. {}", v);
    v.insert(v.end(), {601, 602, 603}); // 오버로드 (5)
    std::println("5. {}", v);
    const auto list = {-13, -12, -11};
    try
    {
        v.insert(v.begin(), list); // 예외 발생: 공간 부족
    }
    catch(const std::bad_alloc&)
    {
        std::println("bad_alloc: v.capacity()={} < v.size()={} + list.size()={}",
                     v.capacity(), v.size(), list.size());
    }
}

출력:

1. [100, 100, 100]
2. [200, 100, 100, 100]
3. [300, 300, 200, 100, 100, 100]
4. [501, 502, 503, 300, 300, 200, 100, 100, 100]
5. [501, 502, 503, 300, 300, 200, 100, 100, 100, 601, 602, 603]
bad_alloc: v.capacity()=14 < v.size()=12 + list.size()=3

참고 항목

제자리에서 요소를 생성
(public member function)
요소들의 범위를 삽입
(public member function)