std::shift_...shift_right(3)
π std::shift_...shift_right(3) π
Bestie, welcome to the C++ Standard Libary manual page for std::shift_left and std::shift_right. Yes, that's the manual name, we're not fixing it, it's iconic. Buckle up: these algorithms slide a range left or right, and the details are lowkey doing the most.
β¨ NAME β¨
std::shift_left,std::shift_right β std::shift_left,std::shift_right
That's the NAME section, and yes it's giving mirror selfie. Two algorithms, one shift-y family. Periodt.
πΈ Synopsis πΈ
Defined in header <algorithm>.
template< class ForwardIt >
constexpr ForwardIt shift_left( ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );
(1) β (since C++20)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt shift_left( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );
(2) β (since C++20)
template< class ForwardIt >
constexpr ForwardIt shift_right( ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );
(3) β (since C++20)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt shift_right( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last,
typename std::iterator_traits<ForwardIt>::difference_type n );
(4) β (since C++20)
Shifts the elements in the range [first, last) by n positions.
(1)β shifts the elements toward the beginning of the range.- If
n == 0 || n >= last - first, there are no effects. - 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 toward the end of the range.- If
n == 0 || n >= last - first, there are no effects. - Otherwise, for every integer
iin[0, last - first - n), moves the element originally at positionfirst + ito positionfirst + n + i. IfForwardItmeets theLegacyBidirectionalIteratorrequirements, then the moves are performed in decreasing order ofistarting fromlast - first - n - 1.
- If
(2),(4)β same as(1)and(3), respectively, but executed according topolicyand the moves may be performed in any order.
These overloads participate in overload resolution only if std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true.
Elements that are in the original range but not the new range are left in a valid but unspecified state. So like, they're alive, but they're not telling you what they've been through.
π¨ If any of the following conditions is satisfied, the behavior is undefined: -n >= 0is not true. - The type of*firstis notMoveAssignable. - Forshift_right,ForwardItis neitherLegacyBidirectionalIteratornorValueSwappable.
That's the danger zone, bestie. Do not.
π¦ Parameters π¦
firstβ the beginning of the original range.lastβ the end of the original range.nβ the number of positions to shift.policyβ the execution policy to use. See execution policy for details.
π Type requirements π
ForwardItmust meet the requirements ofLegacyForwardIterator.
π Return value π
(1),(2)β the end of the resulting range.- If
nis less thanstd::distance(first, last), returns an iterator equal tostd::next(first, (std::distance(first, last) - n)). - Otherwise, returns
first.
- If
(3),(4)β the beginning of the resulting range.- If
nis less thanstd::distance(first, last), returns an iterator equal tostd::next(first, n). - Otherwise, returns
last.
- If
π Complexity π
(1),(2)β At moststd::distance(first, last) - nassignments.(3),(4)β At moststd::distance(first, last) - nassignment or swaps. Yes, that's the wording. We're keeping it.
π«Ά Exceptions π«Ά
The overloads with a template parameter named ExecutionPolicy report errors as follows:
- If execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. For any otherExecutionPolicy, the behavior is implementation-defined. - If the algorithm fails to allocate memory,
std::bad_allocis thrown.
π Notes π
- Feature-test macro:
__cpp_lib_shiftβ Value:201806Lβ Std:(C++20)β Feature:std::shift_leftandstd::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::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
}
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 β
moveβ moves a range of elements to a new location.(C++11)(function template)move_backwardβ moves a range of elements to a new location in backwards order.(C++11)(function template)rotateβ rotates the order of elements in a range. (function template)ranges::shift_left,ranges::shift_rightβ shifts elements in a range.(C++23)(niebloid)
Source/version sign-off: http://cppreference.com, dated 2024.06.10, page std::shift_...shift_right(3). That's the whole manual page, bestie β go shift responsibly. πβ¨
π¦ 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