std::vector<T,Allocator>:: insert
|
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) |
컨테이너의 지정된 위치에 요소를 삽입합니다.
|
(C++11부터) |
-
T가 CopyAssignable 가 아닌 경우.
-
T가 MoveInsertable 가 아니거나vector에 삽입될 수 없는 경우. -
T가 MoveAssignable 가 아닌 경우.
|
(C++11부터) |
-
T가 CopyAssignable 이 아닌 경우.
[
first
,
last
)
범위의 요소들을
pos
앞에 삽입합니다.
|
|
(C++11 이전) |
|
이 오버로드는
|
(C++11 이후) |
|
(C++11부터) |
- first 또는 last 가 * this 의 반복자인 경우.
만약 연산 후 새로운
size()
가 기존
capacity()
보다 크면 재할당이 발생하며, 이 경우 모든 반복자(including the
end()
iterator)와 모든 요소에 대한 참조가 무효화됩니다. 그렇지 않으면 삽입 지점 이전의 반복자와 참조만 유효하게 유지됩니다.
목차 |
매개변수
| pos | - | 내용이 삽입될 반복자 앞 위치 |
| value | - | 삽입할 요소 값 |
| count | - | 삽입할 요소 개수 |
| first, last | - | 삽입할 요소들의 범위 를 정의하는 반복자 쌍 |
| ilist | - | std::initializer_list 로부터 값을 삽입 |
반환값
복잡도
재할당이 발생하는 경우, 삽입 후
vector
의 요소 수에 선형적으로 비례합니다; 그렇지 않은 경우, 삽입된 요소 수에
std::
distance
(
pos, end
(
)
)
을 더한 값에 선형적으로 비례합니다.
예외
다른 곳에서 예외가 발생한 경우
-
T의 복사 생성자,
|
(C++11부터) |
-
T의 복사 할당 연산자,
|
(C++11부터) |
-
모든
InputIt연산,
이러한 함수들은 아무런 효과가 없습니다( strong exception safety guarantee ).
|
끝에 단일 요소를 삽입할 때 예외가 발생하고,
|
(since C++11) |
예제
#include <iostream> #include <iterator> #include <string_view> #include <vector> namespace stq { void println(std::string_view rem, const std::vector<int>& container) { std::cout << rem.substr(0, rem.size() - 2) << '['; bool first{true}; for (const int 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); // 오버로드 (1) stq::println("2. {}", c1); c1.insert(pos, 2, 300); // 오버로드 (3) stq::println("3. {}", c1); // pos는 더 이상 유효하지 않음, 새로 가져옴: pos = c1.begin(); std::vector<int> c2(2, 400); c1.insert(std::next(pos, 2), c2.begin(), c2.end()); // 오버로드 (4) stq::println("4. {}", c1); int arr[] = {501, 502, 503}; c1.insert(c1.begin(), arr, arr + std::size(arr)); // 오버로드 (4) stq::println("5. {}", c1); c1.insert(c1.end(), {601, 602, 603}); // 오버로드 (5) stq::println("6. {}", c1); }
출력:
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]
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 149 | C++98 | 오버로드 ( 3 ) 와 ( 4 ) 는 아무것도 반환하지 않음 | 반복자를 반환함 |
| LWG 247 | C++98 | 복잡도가 오버로드 ( 3 ) 에 대해서만 명시됨 | 모든 오버로드에 대해 명시됨 |
| LWG 406 | C++98 |
강력한 예외 보장이
InputIt
연산에서 예외가 발생한 경우에도 적용됨
|
이 경우 보장되지 않음 |
참고 항목
|
(C++11)
|
제자리에서 요소를 생성
(public member function) |
|
끝에 요소를 추가
(public member function) |
|
|
인자에서 추론된 타입의
std::insert_iterator
를 생성
(function template) |