跳到主要内容

std::shift_right() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt
shift_right( ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );

// (2)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt
shift_right( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );
  • (1) 将元素向范围末尾移动

    • 如果 n == 0 || n >= last - first,则没有效果。
    • 如果 n < 0,则行为未定义.

    否则,对于 [0; last - first - n) 中的每个整数 i,将原来位于位置 first + i 的元素移动到位置 first + n + i。移动按 i 的递增顺序执行,从 0 开始。

    如果 ForwardIt 满足 LegacyBidirectionalIterator 的要求,则移动按 i 的递减顺序执行,从 last - first - n - 1 开始。

  • (2)(1) 相同,但根据 policy 执行。

重载决议

这些重载仅当 std::is_execution_policy_v>true 时才参与重载决议。

参数

first
last

原始范围。

n

移动的位置数量。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

类型要求

ForwardItLegacyBidirectionalIterator

ValueSwappable

解引用 ForwardIt 的类型必须满足 MoveAssignable 的要求。

返回值

结果范围的开始

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

复杂度

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

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行时抛出异常且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

示例

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::shift_left(begin(a), end(a), 3);
std::shift_left(begin(b), end(b), 3);
std::shift_left(begin(c), end(c), 3);
std::cout << a << " " << b << " " << c << '\n';

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

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

// std::shift_left(begin(a), end(a), -3); // UB, e.g. segfault.)
}
输出
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::shift_right() 算法

// (1)
template< class ForwardIt >
constexpr ForwardIt
shift_right( ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );

// (2)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt
shift_right( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );
  • (1) 将元素向范围末尾移动

    • 如果 n == 0 || n >= last - first,则没有效果。
    • 如果 n < 0,则行为未定义.

    否则,对于 [0; last - first - n) 中的每个整数 i,将原来位于位置 first + i 的元素移动到位置 first + n + i。移动按 i 的递增顺序执行,从 0 开始。

    如果 ForwardIt 满足 LegacyBidirectionalIterator 的要求,则移动按 i 的递减顺序执行,从 last - first - n - 1 开始。

  • (2)(1) 相同,但根据 policy 执行。

重载决议

这些重载仅当 std::is_execution_policy_v>true 时才参与重载决议。

参数

first
last

原始范围。

n

移动的位置数量。

policy

要使用的执行策略。有关详细信息,请参阅执行策略

类型要求

ForwardItLegacyBidirectionalIterator

ValueSwappable

解引用 ForwardIt 的类型必须满足 MoveAssignable 的要求。

返回值

结果范围的开始

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

复杂度

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

异常

带有模板参数 ExecutionPolicy 的重载报告错误如下

  • 如果作为算法一部分调用的函数执行时抛出异常且 ExecutionPolicy标准策略之一,则调用 std::terminate。对于其他 ExecutionPolicy,行为是实现定义的.
  • 如果算法未能分配内存,则抛出 std::bad_alloc

示例

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::shift_left(begin(a), end(a), 3);
std::shift_left(begin(b), end(b), 3);
std::shift_left(begin(c), end(c), 3);
std::cout << a << " " << b << " " << c << '\n';

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

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

// std::shift_left(begin(a), end(a), -3); // UB, e.g. segfault.)
}
输出
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 页面。它可能为了改进或编辑偏好而有所修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。