跳到主要内容

std::ranges::shift_right() 算法

// (1)
constexpr ranges::subrange<I>
shift_right( I first, S last, std::iter_difference_t<I> n );

// (2)
constexpr ranges::borrowed_subrange_t<R>
shift_right( R&& r, ranges::range_difference_t<R> n );

参数类型是泛型的,并具有以下约束

  • I - std::permutable
  • S - std::sentinel_for<I>
  • R - std::ranges::forward_range

此外,每个重载都有以下约束

  • (2) - std::permutable<ranges::iterator_t<R>>

(为方便阅读,此处省略了 std:: 命名空间)

将范围 [first; last) 或 r 中的元素移动 n 个位置。

  • (1) 将元素向范围末尾移动

    • 如果 n == 0 || n >= last - first,则没有效果。
    • 如果 n < 0,则行为未定义.
    • 否则,对于 [0; last - first - n) 中的每个整数 i,将原来位于 first + i 位置的元素移动到 first + n + i 位置。

    如果 I 模型化 bidirectional_iterator,则移动操作按 i 的递减顺序执行,从 last - first - n - 1 开始。

  • ** (2)** 与 (1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

未定义行为

行为未定义

如果 [first; last) 不是有效范围。

参数

first
last

原始范围。

r

要移动的元素范围。

n

移动的位置数量。

返回值

{
first,
/* NEW_LAST */
}

其中 NEW_LAST 是结果范围的末尾,等同于

  • 如果 n 小于 last - first,则为 first + n
  • 否则为 last

复杂度

最多 ranges::distance(first, last) - n 次赋值或交换。

异常

(无)

备注

ranges::shift_right / ranges::shift_rightI 模型化 bidirectional_iteratorrandom_access_iterator 时,在常见实现中具有更好的效率。

当迭代器类型模型化 contiguous_iterator 且交换其值类型不调用非平凡的特殊成员函数或 ADL 发现的 swap 时,实现(例如 MSVC STL)可以启用向量化。

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>

struct S
{
int value {0};
bool specified_state {true};

S(int v = 0) : value {v} {}
S(S const& rhs) = default;
S(S&& rhs) { *this = std::move(rhs); }
S& operator=(S const& rhs) = default;
S& operator=(S&& rhs)
{
if (this != &rhs)
{
value = rhs.value;
specified_state = rhs.specified_state;
rhs.specified_state = false;
}
return *this;
}
};

template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
{
for (const auto& s : v)
{
if constexpr (std::is_same_v<T, S>)
s.specified_state ? os << s.value << ' ' : os << ". ";
else if constexpr (std::is_same_v<T, std::string>)
os << (s.empty() ? "." : s) << ' ';
else
os << s << ' ';
}
return os;
}

int main()
{
std::cout << std::left;

std::vector<S> a {1, 2, 3, 4, 5, 6, 7};
std::vector<int> b {1, 2, 3, 4, 5, 6, 7};
std::vector<std::string> c {"α", "β", "γ", "δ", "ε", "ζ", "η"};

std::cout << "vector<S> \tvector<int> \tvector<string>\n";
std::cout << a << " " << b << " " << c << '\n';

std::ranges::shift_left(a, 3);
std::ranges::shift_left(b, 3);
std::ranges::shift_left(c, 3);
std::cout << a << " " << b << " " << c << '\n';

std::ranges::shift_right(a, 2);
std::ranges::shift_right(b, 2);
std::ranges::shift_right(c, 2);
std::cout << a << " " << b << " " << c << '\n';

std::ranges::shift_left(a, 8); // has no effect: n >= last - first
std::ranges::shift_left(b, 8); // ditto
std::ranges::shift_left(c, 8); // ditto
std::cout << a << " " << b << " " << c << '\n';

// std::ranges::shift_left(a, -3); // UB
}
输出
vector<S>       vector<int>     vector<string>
1 2 3 4 5 6 7 1 2 3 4 5 6 7 α β γ δ ε ζ η
4 5 6 7 . . . 4 5 6 7 5 6 7 δ ε ζ η . . .
. . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
. . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::ranges::shift_right() 算法

// (1)
constexpr ranges::subrange<I>
shift_right( I first, S last, std::iter_difference_t<I> n );

// (2)
constexpr ranges::borrowed_subrange_t<R>
shift_right( R&& r, ranges::range_difference_t<R> n );

参数类型是泛型的,并具有以下约束

  • I - std::permutable
  • S - std::sentinel_for<I>
  • R - std::ranges::forward_range

此外,每个重载都有以下约束

  • (2) - std::permutable<ranges::iterator_t<R>>

(为方便阅读,此处省略了 std:: 命名空间)

将范围 [first; last) 或 r 中的元素移动 n 个位置。

  • (1) 将元素向范围末尾移动

    • 如果 n == 0 || n >= last - first,则没有效果。
    • 如果 n < 0,则行为未定义.
    • 否则,对于 [0; last - first - n) 中的每个整数 i,将原来位于 first + i 位置的元素移动到 first + n + i 位置。

    如果 I 模型化 bidirectional_iterator,则移动操作按 i 的递减顺序执行,从 last - first - n - 1 开始。

  • ** (2)** 与 (1) 相同,但使用 r 作为范围,如同使用 ranges::begin(r) 作为 firstranges::end(r) 作为 last

本页描述的函数类实体是niebloids

未定义行为

行为未定义

如果 [first; last) 不是有效范围。

参数

first
last

原始范围。

r

要移动的元素范围。

n

移动的位置数量。

返回值

{
first,
/* NEW_LAST */
}

其中 NEW_LAST 是结果范围的末尾,等同于

  • 如果 n 小于 last - first,则为 first + n
  • 否则为 last

复杂度

最多 ranges::distance(first, last) - n 次赋值或交换。

异常

(无)

备注

ranges::shift_right / ranges::shift_rightI 模型化 bidirectional_iteratorrandom_access_iterator 时,在常见实现中具有更好的效率。

当迭代器类型模型化 contiguous_iterator 且交换其值类型不调用非平凡的特殊成员函数或 ADL 发现的 swap 时,实现(例如 MSVC STL)可以启用向量化。

示例

Main.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>

struct S
{
int value {0};
bool specified_state {true};

S(int v = 0) : value {v} {}
S(S const& rhs) = default;
S(S&& rhs) { *this = std::move(rhs); }
S& operator=(S const& rhs) = default;
S& operator=(S&& rhs)
{
if (this != &rhs)
{
value = rhs.value;
specified_state = rhs.specified_state;
rhs.specified_state = false;
}
return *this;
}
};

template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
{
for (const auto& s : v)
{
if constexpr (std::is_same_v<T, S>)
s.specified_state ? os << s.value << ' ' : os << ". ";
else if constexpr (std::is_same_v<T, std::string>)
os << (s.empty() ? "." : s) << ' ';
else
os << s << ' ';
}
return os;
}

int main()
{
std::cout << std::left;

std::vector<S> a {1, 2, 3, 4, 5, 6, 7};
std::vector<int> b {1, 2, 3, 4, 5, 6, 7};
std::vector<std::string> c {"α", "β", "γ", "δ", "ε", "ζ", "η"};

std::cout << "vector<S> \tvector<int> \tvector<string>\n";
std::cout << a << " " << b << " " << c << '\n';

std::ranges::shift_left(a, 3);
std::ranges::shift_left(b, 3);
std::ranges::shift_left(c, 3);
std::cout << a << " " << b << " " << c << '\n';

std::ranges::shift_right(a, 2);
std::ranges::shift_right(b, 2);
std::ranges::shift_right(c, 2);
std::cout << a << " " << b << " " << c << '\n';

std::ranges::shift_left(a, 8); // has no effect: n >= last - first
std::ranges::shift_left(b, 8); // ditto
std::ranges::shift_left(c, 8); // ditto
std::cout << a << " " << b << " " << c << '\n';

// std::ranges::shift_left(a, -3); // UB
}
输出
vector<S>       vector<int>     vector<string>
1 2 3 4 5 6 7 1 2 3 4 5 6 7 α β γ δ ε ζ η
4 5 6 7 . . . 4 5 6 7 5 6 7 δ ε ζ η . . .
. . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
. . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
本文源自此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。