在C++ STL中的TojEngEnter()

转发列表::在()之后拼接 是CPP STL中的一个内置函数,用于将从第一个+1到最后一个范围内的元素从给定的前向_列表传输到另一个前向_列表。在参数中按位置指向的图元之后插入图元。

null

语法:

forwardlist1_name.splice_after(position iterator, forwardlist2_name,
                                    first iterator, last iterator) 

参数: 该函数接受以下四个参数:

  • 位置 –指定前向列表中插入新元素的位置。
  • 转发列表2_名称 –指定要从中插入元素的列表。
  • 第一 –指定要在其之后进行插入的迭代器。
  • 最后的 –指定要插入的迭代器。

返回值: 该函数没有返回值。

下面的程序演示了上述功能:

项目1:

// C++ program to illustrate
// splice_after() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialising the forward lists
forward_list< int > list1 = { 10, 20, 30, 40 };
forward_list< int > list2 = { 4, 9 };
// splice_after operation performed
// all elements except the first element in list1 is
// inserted in list 2 between 4 and 9
list2.splice_after(list2.begin(), list1,
list1.begin(), list1.end());
cout << "Elements are: " << endl;
// loop to print the elements of second list
for ( auto it = list2.begin(); it != list2.end(); ++it)
cout << *it << " " ;
return 0;
}


输出:

Elements are: 
4 20 30 40 9

项目2:

// C++ program to illustrate
// splice_after() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// initialising the forward lists
forward_list< int > list1 = { 10, 20, 30, 40 };
forward_list< int > list2 = { 4, 9 };
// splice_after operation performed
// all elements of list1 are inserted
// in list2 between 4 and 9
list2.splice_after(list2.begin(), list1,
list1.before_begin(), list1.end());
cout << "Elements are: " << endl;
// loop to print the elements of second list
for ( auto it = list2.begin(); it != list2.end(); ++it)
cout << *it << " " ;
return 0;
}


输出:

Elements are: 
4 10 20 30 40 9

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享