[ Web Proxy ]
URL:
Viewing: https://cppreference.com/cpp/algorithm/ranges/shift [Back]  [Original]

std::ranges::shift_left, std::ranges::shift_right - cppreference.com
cppreference.com
Namespaces
Variants

std::ranges::shift_left, std::ranges::shift_right

From cppreference.com
 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Non-modifying sequence operations    
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
(C++11)    

Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations


 
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Fold operations (Helper templates)
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
       
       
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
       
       
Permutation operations
Specialized <memory> algorithms
Return types
 
Defined in header <algorithm>
Call signature
template< std::permutable I, std::sentinel_for<I> S >
constexpr ranges::subrange<I>
    shift_left( I first, S last, std::iter_difference_t<I> count );
(1) (since C++23)
template< ranges::forward_range R >
    requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
    shift_left( R&& r, ranges::range_difference_t<R> count );
(2) (since C++23)
template< std::permutable I, std::sentinel_for<I> S >
constexpr ranges::subrange<I>
    shift_right( I first, S last, std::iter_difference_t<I> count );
(3) (since C++23)
template< ranges::forward_range R >
    requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
    shift_right( R&& r, ranges::range_difference_t<R> count );
(4) (since C++23)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S >
    requires std::permutable<I>
ranges::subrange<I>
    shift_left( Ep&& policy, I first, S last,
                std::iter_difference_t<I> count );
(5) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R >
    requires std::permutable<ranges::iterator_t<R>>
ranges::borrowed_subrange_t<R>
    shift_left( Ep&& policy, R&& r, ranges::range_difference_t<R> count );
(6) (since C++26)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S >
    requires std::permutable<I>
ranges::subrange<I>
    shift_right( Ep&& policy, I first, S last,
                 std::iter_difference_t<I> count );
(7) (since C++26)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ R >
    requires std::permutable<ranges::iterator_t<R>>
ranges::borrowed_subrange_t<R>
    shift_right( Ep&& policy, R&& r, ranges::range_difference_t<R> count );
(8) (since C++26)

For the definition of /*execution-policy*/, see this page; for the definition of /*sized-random-access-range*/, see this page.

Shifts the elements in the target range [firstlast) or r by count positions. If count is not less than ranges::distance(first, last) or ranges::distance(r), does nothing.

1,2) shift_left shifts the elements towards the beginning of the target range.
For every integer i starting from 0 to ranges::distance(first, last) - count - 1, moves the element originally at position ranges::next(first, count + i) to position ranges::next(first, i).
3,4) shift_right shifts the elements towards the end of the target range.
For every integer i starting from ranges::distance(first, last) - count - 1 to 0, moves the element originally at position ranges::next(first, i) to position ranges::next(first, count + i).
If I or ranges::iterator_t<R> does not model bidirectional_iterator, the shifting process is performed by swapping elements instead, the swap order is unspecified.
5-8) Same as (1-4), but the move order is determined by policy.

If count >= 0 is not true, the behavior is undefined.

The function-like entities described on this page are algorithm function objects (informally known as niebloids), that is:

Parameters

first, last - the iterator-sentinel pair defining the target range
r - the target range
count - the shift offset
policy - the execution policy to use

Return value

A subrange containing all elements that are not shifted away from the target range, or an empty range if count is not less than ranges::distance(first, last) or ranges::distance(r).

Complexity

Given \(\scriptsize N\)N as ranges::distance(first, last) or ranges::distance(r):

1,2,5,6) At most \(\scriptsize \max(N-\mathtt{count},0)\)max(N-count,0) assignments.
3,4,7,8) At most \(\scriptsize \max(N-\mathtt{count},0)\)max(N-count,0) assignment or swaps.

Exceptions

5-8) During the execution process:
  • If the temporary memory resources required for parallelization are not available, std::bad_alloc is thrown.
  • If an uncaught exception is thrown while accessing objects via an algorithm argument, the behavior is determined by the execution policy (for standard policies, std::terminate is invoked).

Notes

ranges::shift_left / ranges::shift_right has better efficiency on common implementations if I or ranges::iterator_t<R> 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.

Feature-test macro Value Std Feature
__cpp_lib_shift 202202L (C++23) std::ranges::shift_left and std::ranges::shift_right

Example

#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(const S& rhs) = default;
    S(S&& rhs) { *this = std::move(rhs); }
    S& operator=(const S& 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, const std::vector<T>& 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> \t\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

shifts elements in a range
(function template) [edit]
moves a range of elements to a new location
(algorithm function object)[edit]
moves a range of elements to a new location in backwards order
(algorithm function object)[edit]
rotates the order of elements in a range
(algorithm function object)[edit]

Web Proxy Viewer  |  New URL  |  Original Page