跳到主要内容

std::forward_list splice_after() 方法

// (1) Non const version only
void splice_after( const_iterator pos, forward_list& other );

// (1) Non const version only
void splice_after( const_iterator pos, forward_list&& other );

// (2) Non const version only
void splice_after( const_iterator pos, forward_list& other, const_iterator it );

// (2) Non const version only
void splice_after( const_iterator pos, forward_list&& other, const_iterator it );

// (3) Non const version only
void splice_after(
const_iterator pos, forward_list& other,
const_iterator first, const_iterator last
);

// (3) Non const version only
void splice_after(
const_iterator pos, forward_list&& other,
const_iterator first, const_iterator last
);

将元素从另一个 forward_list 移动到 *this

没有元素被复制。pos 必须是 *this 中可解引用的有效迭代器,或者为 before_begin() 迭代器(特别地,end() 不是 pos 的有效参数)。

  • (1) other 中的所有元素移动到 *this
    元素插入到 pos 所指向的元素之后。操作完成后,容器 other 将变为空。

    未定义行为

    行为未定义

    如果 other 指向的对象与 *this 相同。

  • (2) other 中由其后迭代器指向的元素移动到 *this
    元素插入到 pos 所指向的元素之后。如果 pos == itpos == ++it,则不执行任何操作。

  • (3) other 中范围 (first, last) 内的元素移动到 *this
    元素插入到 pos 所指向的元素之后。由 first 指向的元素不会被移动。

    未定义行为

    行为未定义

    如果 pos 是范围 **( first, last )** 中的迭代器。

重要

没有迭代器或引用会失效,除了被移动的元素的迭代器现在指向 *this 而不是 other

未定义行为

行为未定义

)。如果 get_allocator() != other.get_allocator()

参数

  • pos - 内容将被插入的元素之后的位置
  • other - 要从中移动内容的另一个容器
  • it - 指向前一个要从 other 移动到 *this 的元素的迭代器
  • first, last - 要从 other 移动到 *this 的元素范围

返回值

(无)

复杂度

  • (1) 线性于 other 的大小 - O(other.size())
  • (2) 常数 - O(1)
  • (3) 线性于 std::distance(first, last) - O(std::distance(first, last))

异常

(无)

示例

Main.cpp
#include <algorithm>
#include <cassert>
#include <forward_list>
#include <initializer_list>
#include <iostream>

using F = std::forward_list<int>;

std::ostream& operator<< (std::ostream& os, F const& l) {
for (int e : l) os << e << ' ';
return os;
}

int main()
{
{
F l1 = { 1, 2, 3, 4, 5 };
F l2 = { 10, 11, 12 };

l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend());
// not equivalent to l2.splice_after(l2.cbegin(), l1);
// which is equivalent to
// l2.splice_after(l2.cbegin(), l1, l1.cbefore_begin(), l1.end());

std::cout << "l1: " << l1 << "\n"
"l2: " << l2 << '\n';
}


// Compare two given lists and abort the program if they are not equal.
auto equ = [] (F const& p, std::initializer_list<int> const& q) {
assert(std::ranges::equal(p, q));
};

// The following code demonstrates all three overloads (1),..(3).

{
F x = { 1, 2, 3, 4, 5 };
F y = { 10, 11, 12 };
x.splice_after(x.cbegin(), y); // (1)
equ( x, { 1, 10, 11, 12, 2, 3, 4, 5 } );
equ( y, { } );
}

{
F x = { 1, 2, 3, 4, 5 };
F y = { 10, 11, 12 };
x.splice_after(x.cbegin(), y, y.cbegin()); // (2)
equ( x, { 1, 11, 2, 3, 4, 5 } );
equ( y, { 10, 12 } );
}

{
F x = { 1, 2, 3, 4, 5 };
F y = { 10, 11, 12 };
x.splice_after(x.cbegin(), y, y.cbegin(), y.cend()); // (3)
equ( x, { 1, 11, 12, 2, 3, 4, 5 } );
equ( y, { 10 } );
}
}
输出
l1: 1
l2: 10 2 3 4 5 11 12
本文来源于 CppReference 页面。可能已为改进或编辑偏好进行了修改。点击“编辑此页面”查看对此文档所做的所有更改。
悬停查看原始许可证。

std::forward_list splice_after() 方法

// (1) Non const version only
void splice_after( const_iterator pos, forward_list& other );

// (1) Non const version only
void splice_after( const_iterator pos, forward_list&& other );

// (2) Non const version only
void splice_after( const_iterator pos, forward_list& other, const_iterator it );

// (2) Non const version only
void splice_after( const_iterator pos, forward_list&& other, const_iterator it );

// (3) Non const version only
void splice_after(
const_iterator pos, forward_list& other,
const_iterator first, const_iterator last
);

// (3) Non const version only
void splice_after(
const_iterator pos, forward_list&& other,
const_iterator first, const_iterator last
);

将元素从另一个 forward_list 移动到 *this

没有元素被复制。pos 必须是 *this 中可解引用的有效迭代器,或者为 before_begin() 迭代器(特别地,end() 不是 pos 的有效参数)。

  • (1) other 中的所有元素移动到 *this
    元素插入到 pos 所指向的元素之后。操作完成后,容器 other 将变为空。

    未定义行为

    行为未定义

    如果 other 指向的对象与 *this 相同。

  • (2) other 中由其后迭代器指向的元素移动到 *this
    元素插入到 pos 所指向的元素之后。如果 pos == itpos == ++it,则不执行任何操作。

  • (3) other 中范围 (first, last) 内的元素移动到 *this
    元素插入到 pos 所指向的元素之后。由 first 指向的元素不会被移动。

    未定义行为

    行为未定义

    如果 pos 是范围 **( first, last )** 中的迭代器。

重要

没有迭代器或引用会失效,除了被移动的元素的迭代器现在指向 *this 而不是 other

未定义行为

行为未定义

)。如果 get_allocator() != other.get_allocator()

参数

  • pos - 内容将被插入的元素之后的位置
  • other - 要从中移动内容的另一个容器
  • it - 指向前一个要从 other 移动到 *this 的元素的迭代器
  • first, last - 要从 other 移动到 *this 的元素范围

返回值

(无)

复杂度

  • (1) 线性于 other 的大小 - O(other.size())
  • (2) 常数 - O(1)
  • (3) 线性于 std::distance(first, last) - O(std::distance(first, last))

异常

(无)

示例

Main.cpp
#include <algorithm>
#include <cassert>
#include <forward_list>
#include <initializer_list>
#include <iostream>

using F = std::forward_list<int>;

std::ostream& operator<< (std::ostream& os, F const& l) {
for (int e : l) os << e << ' ';
return os;
}

int main()
{
{
F l1 = { 1, 2, 3, 4, 5 };
F l2 = { 10, 11, 12 };

l2.splice_after(l2.cbegin(), l1, l1.cbegin(), l1.cend());
// not equivalent to l2.splice_after(l2.cbegin(), l1);
// which is equivalent to
// l2.splice_after(l2.cbegin(), l1, l1.cbefore_begin(), l1.end());

std::cout << "l1: " << l1 << "\n"
"l2: " << l2 << '\n';
}


// Compare two given lists and abort the program if they are not equal.
auto equ = [] (F const& p, std::initializer_list<int> const& q) {
assert(std::ranges::equal(p, q));
};

// The following code demonstrates all three overloads (1),..(3).

{
F x = { 1, 2, 3, 4, 5 };
F y = { 10, 11, 12 };
x.splice_after(x.cbegin(), y); // (1)
equ( x, { 1, 10, 11, 12, 2, 3, 4, 5 } );
equ( y, { } );
}

{
F x = { 1, 2, 3, 4, 5 };
F y = { 10, 11, 12 };
x.splice_after(x.cbegin(), y, y.cbegin()); // (2)
equ( x, { 1, 11, 2, 3, 4, 5 } );
equ( y, { 10, 12 } );
}

{
F x = { 1, 2, 3, 4, 5 };
F y = { 10, 11, 12 };
x.splice_after(x.cbegin(), y, y.cbegin(), y.cend()); // (3)
equ( x, { 1, 11, 12, 2, 3, 4, 5 } );
equ( y, { 10 } );
}
}
输出
l1: 1
l2: 10 2 3 4 5 11 12
本文来源于 CppReference 页面。可能已为改进或编辑偏好进行了修改。点击“编辑此页面”查看对此文档所做的所有更改。
悬停查看原始许可证。