Namespaces
Variants

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

From 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) pos 앞에 value 의 복사본을 삽입합니다.
2) value pos 앞에 삽입하며, 이동 시맨틱을 사용할 수 있습니다.
3) pos 앞에 value 값을 count 개 삽입합니다.
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)