A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../keywords/../algorithm/shift.html below:

std::shift_left, std::shift_right - cppreference.com

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 [firstlast) by n positions.

1)

Shifts the elements towards the beginning of the range.

The moves are performed in increasing order of i starting from ​0​.

3)

Shifts the elements towards the end of the range.

If

ForwardIt

meets the

LegacyBidirectionalIterator

requirements, then the moves are performed in decreasing order of

i

starting from

last - first - n - 1

.

2,4) Same as (1) and (3), respectively, but executed according to policy and the moves may be performed in any order.

Elements that are in the original range but not the new range are left in a valid but unspecified state.

If any of the following conditions is satisfied, the behavior is undefined:

[edit] Parameters first, last - the pair of iterators defining the range of elements to shift n - the number of positions to shift policy - the execution policy to use Type requirements -ForwardIt must meet the requirements of LegacyForwardIterator. [edit] Return value 1,2)

The end of the resulting range.

3,4)

The beginning of the resulting range.

[edit] Complexity [edit] Exceptions

The overloads with a template parameter named ExecutionPolicy report errors as follows:

[edit] Notes [edit] 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(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   . . δ ε ζ η .
[edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4