한 목록에서 다른 목록으로 요소를 전송합니다.
요소는 복사 (copy) 되거나 이동(move) 되지 않으며, 목록 내의 포인터만 다시 연결될 뿐입니다.
void splice( const_iterator pos, list& other );
void splice( const_iterator pos, list&& other ); // C++11부터
void splice( const_iterator pos, list& other, const_iterator it );
void splice( const_iterator pos, list&& other, const_iterator it ); // C++11 부터
void splice( const_iterator pos, list& other,
const_iterator first, const_iterator last);
void splice( const_iterator pos, list&& other,
const_iterator first, const_iterator last );
pos : 내용을 삽입할 곳의 반복자
other : 삽입할 다른 list
it : it이 가르키고있는 부분만 전송함
first, last : 범위단위로 전송함
std::list<int> list1{ 1,2,3,4,5 };
std::list<int> list2{ 10,20,30,40,50 };
auto it = list1.begin();
std::advance(it, 2);
list1.splice(it, list2);
//1, 2, 10, 20, 30, 40, 50, 3, 4, 5 -> void splice( const_iterator pos, list& other )
// 즉 it의 앞부분에 list 전체를 전송시킴
std::list<int> list1{ 1,2,3,4,5 };
std::list<int> list2{ 10,20,30,40,50 };
auto it = list1.begin();
std::advance(it, 2);
auto list2it = list2.begin();
list1.splice(it, list2, list2it);
//1, 2, 10, 3, 4, 5 -> void splice( const_iterator pos, list& other, const_iterator it )
std::list<int> list1{ 1,2,3,4,5 };
std::list<int> list2{ 10,20,30,40,50 };
auto it = list1.begin();
std::advance(it, 2);
auto list2it = list2.begin();
std::advance(list2it, 1);
list1.splice(it, list2, list2it, list2.end());
// 1, 2, 20, 30, 40, 50, 3, 4, 5 -> void splice ( const_iterator pos, list& other, const_iterator first, const_iterator last)
// 이 결과, list2 에는 10이라는 값만 남음
std::advance 는 iterator를 받아, 우측의 숫자만큼 iterator를 증가시킨다
std::advance(it, 2) == list1의 3 요소와 같음
복잡도는 O(1) 입니다.
'C++ > STL' 카테고리의 다른 글
| STL) 나만의 Allocator( 할당자 ) 만들기 - 2 (0) | 2019.04.17 |
|---|---|
| STL) 나만의 Allocator( 할당자 ) 만들기 - 1 (0) | 2019.04.16 |
| STL) 반복자 어댑터 ( Iterator Adaptor ) (0) | 2019.04.12 |
| STL) 분할, 정렬, 힙에 대한 추가 연산들 (stable_*, is_*, is_*_until) (0) | 2019.02.20 |
| STL) 순열(permutation)과 관련된 함수들 (std::next_permutation, std::prev_permutation, std::rotate, std::shuffle) (0) | 2019.02.20 |