c++STL常用容器之List容器——合并链表splice()

之前有写过List的总结:

c++STL常用容器之List容器——全面总结(附案例解析)(十六)


但是怎么合并两个链表呢:

函数:splice()

看一个案例

合并两个有序增长链表并倒序输出:

有List L1包括1,3,6,9

有List L2包括2,4,5,12,19

合并L1,L2并倒叙输出

#include
#include
using namespace std;
void printList(const list& L) {
	for (list::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}
int main() {
	listL1{1,3,6,9};
	listL2{2,4,5,12,19};
	L1.splice(L1.end(), L2);
	L1.sort();
	L1.reverse();
	printList(L1);

	system("pause");
	return 0;
}

c++STL常用容器之List容器——合并链表splice()_第1张图片

用splice()将L2全体放在L1的末尾end()处。

排序

反转

输出即可!

你可能感兴趣的:(C/C++)