C++ forward_list 库 - splice_after() 函数



描述

C++ 函数 std::forward_list::splice_after() 将范围内的元素转移到firstlastx*this。这些元素将插入到由position.

指向的元素之后。

声明

以下是来自 std::forward_list 头文件的 std::forward_list::splice_after() 函数的声明。

void splice_after (const_iterator position, forward_list& x,
				   const_iterator first, const_iterator last);

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

C++11

  • 参数

  • position − forward_list 中要插入新元素之后的那个位置。

  • x − 另一个相同类型的 forward_list 对象。

  • first − 输入迭代器,指向范围内的起始位置。

last − 输入迭代器,指向范围内的结束位置。

返回值

异常

此成员函数从不抛出异常。

时间复杂度

线性,即 O(n)

示例

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4};
   forward_list<int> fl2 = {1, 5};

   fl2.splice_after(fl2.begin(), fl1, fl1.begin(), fl1.end());

   cout << "List contains following elements" << endl;

   for (auto it = fl2.begin(); it != fl2.end(); ++it)
      cout << *it << endl;

   return 0;
}

实时演示

List contains following elements
1
2
3
4
5
让我们编译并运行以上程序,这将产生以下结果:
打印页面
广告