跳到主要内容

std::ranges::shift_left() 算法

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

// (2)
constexpr ranges::borrowed_subrange_t<R>
shift_left( 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 + n + i 的元素移动到 first + i。移动按 i0 开始递增的顺序执行。
  • ** (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 + (last - first - n)
  • 否则,为 first

复杂度

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

异常

(无)

可能的实现

shift_left(1) 和 shift_left(2)
struct shift_left_fn
{
template<std::bidirectional_iterator I, std::sentinel_for<I> S>
requires std::permutable<I>
constexpr I operator()(I first, S last) const
{
auto last2 {ranges::next(first, last)};
for (auto tail {last2}; !(first == tail or first == --tail); ++first)
ranges::iter_swap(first, tail);
return last2;
}

template<ranges::bidirectional_range R>
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r) const
{
return (*this)(ranges::begin(r), ranges::end(r));
}
};

inline constexpr shift_left_fn shift_left {};

备注

ranges::shift_left / ranges::shift_right 在常见实现中具有更好的效率,如果 I 实现了 bidirectional_iteratorrandom_access_iterator

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

示例

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_left() 算法

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

// (2)
constexpr ranges::borrowed_subrange_t<R>
shift_left( 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 + n + i 的元素移动到 first + i。移动按 i0 开始递增的顺序执行。
  • ** (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 + (last - first - n)
  • 否则,为 first

复杂度

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

异常

(无)

可能的实现

shift_left(1) 和 shift_left(2)
struct shift_left_fn
{
template<std::bidirectional_iterator I, std::sentinel_for<I> S>
requires std::permutable<I>
constexpr I operator()(I first, S last) const
{
auto last2 {ranges::next(first, last)};
for (auto tail {last2}; !(first == tail or first == --tail); ++first)
ranges::iter_swap(first, tail);
return last2;
}

template<ranges::bidirectional_range R>
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r) const
{
return (*this)(ranges::begin(r), ranges::end(r));
}
};

inline constexpr shift_left_fn shift_left {};

备注

ranges::shift_left / ranges::shift_right 在常见实现中具有更好的效率,如果 I 实现了 bidirectional_iteratorrandom_access_iterator

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

示例

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 页面。它可能为了改进或编辑偏好而有所修改。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。