std::ranges...shift_right(3)
β¨ std::ranges::shift_left, std::ranges::shift_right β¨
okay so bestie, welcome to the std::ranges...shift_right(3) page from the C++ Standard Libary manual, section 3. This is the one about shifting elements left and right, and it is lowkey iconic. Both names are std::ranges::shift_left,std::ranges::shift_right β yes, the page really runs them together like that π. No cap, we are keeping every overload, every βundefined behaviorβ warning, every parameter, every feature-test macro, so letβs go π
.
πΈ NAME πΈ
std::ranges::shift_left,std::ranges::shift_right β std::ranges::shift_left,std::ranges::shift_right (the page literally says it twice, periodt).
π¦ Synopsis π¦
Defined in header <algorithm>
Call signature
template< std::permutable I, std::sentinel_for<I> S >
constexpr ranges::subrange<I> (1)
(since C++23)
shift_left( I first, S last, std::iter_difference_t<I> n );
template< ranges::forward_range R >
requires std::permutable<ranges::iterator_t<R>> (2)
(since C++23)
constexpr ranges::borrowed_subrange_t<R>
shift_left( R&& r, ranges::range_difference_t<R> n );
template< std::permutable I, std::sentinel_for<I> S >
constexpr ranges::subrange<I> (3)
(since C++23)
shift_right( I first, S last, std::iter_difference_t<I> n );
template< ranges::forward_range R >
requires std::permutable<ranges::iterator_t<R>> (4)
(since C++23)
constexpr ranges::borrowed_subrange_t<R>
shift_right( R&& r, ranges::range_difference_t<R> n );
Shifts the elements in the range [first, last) or r by n positions. The behavior is undefined if [first, last) is not a valid range. fr fr, undefined is undefined.
- 1) Shifts the elements towards the beginning of the range:
- If
n == 0 || n >= last - first, there are no effects. - If
n < 0, the behavior is undefined. - Otherwise, for every integer
iin[0, last - first - n), moves the element originally at positionfirst + n + ito positionfirst + i. The moves are performed in increasing order ofistarting from0.
- If
- 3) Shifts the elements towards the end of the range:
- If
n == 0 || n >= last - first, there are no effects. - If
n < 0, the behavior is undefined. - Otherwise, for every integer
iin[0, last - first - n), moves the element originally at positionfirst + ito positionfirst + n + i. IfImodelsbidirectional_iterator, then the moves are performed in decreasing order ofistarting fromlast - first - n - 1.
- If
- 2,4) Same as (1) or (3) respectively, but uses
ras the range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
Elements that are in the original range but not the new range are left in a valid but unspecified state. not me leaving my old elements in a valid but unspecified state π
The function-like entities described on this page are niebloids, that is:
- Explicit template argument lists cannot be specified when calling any of them.
- None of them are visible to argument-dependent lookup.
- When any of them are found by normal unqualified lookup as the name to the left of the function-call operator, argument-dependent lookup is inhibited.
In practice, they may be implemented as function objects, or with special compiler extensions. iykyk.
π Parameters π
firstβ the beginning of the original rangelastβ the end of the original rangerβ the range of elements to shiftnβ the number of positions to shift
π Return value π
- 1,2)
{first, /*NEW_LAST*/}, whereNEW_LASTis the end of the resulting range and equivalent to:first + (last - first - n), ifnis less thanlast - first;firstotherwise.
- 3,4)
{/*NEW_FIRST*/, last}, whereNEW_FIRSTis the beginning of the resulting range and equivalent to:first + n, ifnis less thanlast - first;lastotherwise.
π Complexity π
- 1,2) At most
ranges::distance(first, last) - nassignments. - 3,4) At most
ranges::distance(first, last) - nassignment or swaps.
π§ Notes π§
ranges::shift_left / ranges::shift_right has better efficiency on common implementations if I models bidirectional_iterator or (better) random_access_iterator.
Implementations (e.g. MSVC STL) may enable vectorization when the iterator type models contiguous_iterator and swapping its value type calls neither non-trivial special member function nor ADL-found swap.
__cpp_lib_shiftβ value202202L, std(C++23), feature:std::ranges::shift_leftandstd::ranges::shift_right.
π Example π
// Run this code
#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
}
π Possible output π
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 . . Ξ΄ Ξ΅ ΞΆ Ξ· .
π See also π
ranges::moveβ moves a range of elements to a new location (C++20) (niebloid)ranges::move_backwardβ moves a range of elements to a new location in backwards order (C++20) (niebloid)ranges::rotateβ rotates the order of elements in a range (C++20) (niebloid)shift_left,shift_rightβ shifts elements in a range (function template) (C++20)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::ranges...shift_right(3) π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms