public member function
<list>
std::list::splice
entire list (1) | void splice (iterator position, list& x); |
---|
single element (2) | void splice (iterator position, list& x, iterator i); |
---|
element range (3) | void splice (iterator position, list& x, iterator first, iterator last); |
---|
entire list (1) | void splice (const_iterator position, list& x);void splice (const_iterator position, list&& x); |
---|
single element (2) | void splice (const_iterator position, list& x, const_iterator i);void splice (const_iterator position, list&& x, const_iterator i); |
---|
element range (3) | void splice (const_iterator position, list& x, const_iterator first, const_iterator last);void splice (const_iterator position, list&& x, const_iterator first, const_iterator last); |
---|
Transfer elements from list to list
將 x 中的元素轉移到容器中,並將它們插入到 position。
這有效地將這些元素插入到容器中,並從 x 中移除它們,從而改變兩個容器的大小。此操作不涉及任何元素的構造或析構。它們會被轉移,無論 x 是左值還是右值,也無論value_type是否支援移動構造。
第一個版本 (1) 將 x 的所有元素轉移到容器中。
第二個版本 (2) 僅將 i 指向的元素從 x 轉移到容器中。
第三個版本 (3) 轉移範圍[first,last)從 x 到容器中。
引數
- position
- x 中的元素將被插入到容器內的位置。
成員型別iterator和const_iterator是 雙向迭代器 型別,指向元素。
- x
- 一個 list 物件,具有相同的型別(即相同的模板引數,T和Alloc).
此引數可以是*this如果 position 指向一個實際上未被拼接的元素(對於第一個版本,情況永遠不會如此,但對於其他版本則可能)。
- i
- 指向 x 中某個元素的迭代器。僅轉移此單個元素。
iterator是成員型別,定義為 雙向迭代器 型別。
成員型別iterator和const_iterator是 雙向迭代器 型別,指向元素。
- first,last
- 指定 x 中元素範圍的迭代器。將範圍內的元素轉移[first,last)到 position。
請注意,範圍包括 first 和 last 之間的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
成員型別iterator和const_iterator是 雙向迭代器 型別,指向元素。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
// splicing lists
#include <iostream>
#include <list>
int main ()
{
std::list<int> mylist1, mylist2;
std::list<int>::iterator it;
// set some initial values:
for (int i=1; i<=4; ++i)
mylist1.push_back(i); // mylist1: 1 2 3 4
for (int i=1; i<=3; ++i)
mylist2.push_back(i*10); // mylist2: 10 20 30
it = mylist1.begin();
++it; // points to 2
mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)
mylist2.splice (mylist2.begin(),mylist1, it);
// mylist1: 1 10 20 30 3 4
// mylist2: 2
// "it" is now invalid.
it = mylist1.begin();
std::advance(it,3); // "it" points now to 30
mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end());
// mylist1: 30 3 4 1 10 20
std::cout << "mylist1 contains:";
for (it=mylist1.begin(); it!=mylist1.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
std::cout << "mylist2 contains:";
for (it=mylist2.begin(); it!=mylist2.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
|
輸出
mylist1 contains: 30 3 4 1 10 20
mylist2 contains: 2
|
複雜度
對於 (1) 和 (2) 是常數。
對於 (3),最多為線性(相對於轉移的元素數量)。
迭代器有效性
呼叫前,容器的迭代器、指標和引用沒有變化。
先前指向被轉移元素的迭代器、指標和引用將繼續指向這些相同的元素,但迭代器現在將指向已轉移到容器中的元素。
資料競爭
容器和 x 都被修改。
併發訪問或修改它們的元素是安全的,但迭代 x 或包含 position 的範圍則不安全。
異常安全
如果兩個容器中的 分配器 不相等,如果指定的任何迭代器或範圍無效,或者如果 x 是*this在 (1) 中,或者如果 position 在範圍[first,last)在 (3) 中,將導致 未定義行為。
否則,該函式永遠不會丟擲異常(無丟擲保證)。