Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

forward_list::splice_after

公众成员函数  <forward_list>
entire list (1)
void splice_after (const_iterator position, forward_list& fwdlst);
void splice_after (const_iterator position, forward_list&& fwdlst);
single element (2)
void splice_after (const_iterator position, forward_list& fwdlst, const_iterator i);
void splice_after (const_iterator position, forward_list&& fwdlst, const_iterator i);
element range (3)
void splice_after (const_iterator position, forward_list& fwdlst,
                   const_iterator first, const_iterator last);
void splice_after (const_iterator position, forward_list&& fwdlst,
                   const_iterator first, const_iterator last);

从另一个forward_list转移元素

将元素从fwdlst转移到this容器中,并将它们插入到指定位置的元素之后。

这个操作将会把这些元素插入到容器中,并从fwdlst中删除它们,从而改变两个容器的大小。 该操作不涉及任何元素的构造或销毁。 不管fwdlst是左值还是右值,也不管value_type是否支持move-construction,它们都会被转移。

第一个版本(1)将fwdlst的所有元素转移到this容器中。

第二个版本(2)只将i所指向的元素从fwdlst转移到this容器中。

第三个版本(3)将范围(first,last)从fwdlst转移到this容器中。

☲  参数


position
在容器内插入fwdlst元素的位置。 成员类型const_iterator是指向const元素的前向迭代器。
fwdlst
相同类型的forward_list对象(即具有相同的模板参数,T和Alloc)。
如果position指向一个没有实际拼接的元素, 则该参数可以是* This: 对于两个参数版本(1),永远不会是这种情况,但对于其他版本,这是可能的.
注意,无论传递的是左值还是右值引用,该函数都会修改fwdlst,从其中删除元素。
i
迭代器指向fwdlst中要转移的元素之前的元素。只有后面的单个元素被转移。
成员类型const_iterator是指向const元素的前向迭代器。
first,last
在fwdlst中指定元素的开放区间的迭代器。函数将range (first,last)中的元素转移到position。
注意,在这个函数中,范围是开放的,包括first和last之间的所有元素,但不包括first和last本身。
成员类型const_iterator是指向const元素的前向迭代器。

☉  返回值



none

☣  示例



// forward_list::splice_after
#include <iostream>
#include <forward_list>

int main ()
{
  std::forward_list<int> first = { 1, 2, 3 };
  std::forward_list<int> second = { 10, 20, 30 };

  auto it = first.begin();  // points to the 1

  first.splice_after ( first.before_begin(), second );
                          // first: 10 20 30 1 2 3
                          // second: (empty)
                          // "it" still points to the 1 (now first's 4th element)

  second.splice_after ( second.before_begin(), first, first.begin(), it);
                          // first: 10 1 2 3
                          // second: 20 30

  first.splice_after ( first.before_begin(), second, second.begin() );
                          // first: 30 10 1 2 3
                          // second: 20
                          // * notice that what is moved is AFTER the iterator

  std::cout << "first contains:";
  for (int& x: first) std::cout << " " << x;
  std::cout << std::endl;

  std::cout << "second contains:";
  for (int& x: second) std::cout << " " << x;
  std::cout << std::endl;

  return 0;
}


输出:
first contains: 30 10 1 2 3
second contains: 20

✥ 复杂度



转移的元素数可达线性.

☣ 迭代器的有效性



在调用之前,与容器相关的迭代器、指针和引用没有变化。
指向已转移元素的迭代器、指针和引用继续指向相同的元素,但迭代器现在将迭代到已转移元素所在的容器中.

⇄ 数据竞争


this容器和fwdlst都被修改.
并发访问或修改它们的元素是安全的,但迭代x或包含position的范围是不安全的。

☂ 异常安全性



如果两个容器中的分配器比较不相等,如果指定的任何迭代器或范围无效, 或者(1)中的x是*this,或者(3)中的position在范围[first,last)内,则会导致未定义的行为。
否则,函数永远不会抛出异常(no-throw保证)。

🍄  另请参阅



forward_list::resize 改变大小(公共成员函数)
forward_list::erase_after 删除元素 (公共成员函数)
forward_list::merge 合并已排序的容器 (公共成员函数)
forward_list::swap 交换内容 (公共成员函数)
forward_list::insert_after 插入元素 (公共成员函数)
forward_list::emplace_after 构造并插入元素 (公共成员函数)

联系我们 免责声明 关于CandCplus 网站地图