Namespaces
Variants

std::ranges:: move_backward, std::ranges:: move_backward_result

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
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
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
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
Fold operations
Operations on uninitialized storage
Return types
Definiert im Header <algorithm>
Aufrufsignatur
template < std:: bidirectional_iterator I1, std:: sentinel_for < I1 > S1,

std:: bidirectional_iterator I2 >
requires std:: indirectly_movable < I1, I2 >
constexpr move_backward_result < I1, I2 >

move_backward ( I1 first, S1 last, I2 d_last ) ;
(1) (seit C++20)
template < ranges:: bidirectional_range R, std:: bidirectional_iterator I >

requires std:: indirectly_movable < ranges:: iterator_t < R > , I >
constexpr move_backward_result < ranges:: borrowed_iterator_t < R > , I >

move_backward ( R && r, I d_last ) ;
(2) (seit C++20)
Hilfstypen
template < class I, class O >
using move_backward_result = ranges:: in_out_result < I, O > ;
(3) (seit C++20)
1) Verschiebt die Elemente im durch [ first , last ) definierten Bereich in einen anderen Bereich [ d_last - N , d_last ) , wobei N = ranges:: distance ( first, last ) . Die Elemente werden in umgekehrter Reihenfolge verschoben (das letzte Element wird zuerst verschoben), aber ihre relative Reihenfolge bleibt erhalten. Das Verhalten ist undefiniert, wenn d_last innerhalb von ( first, last ] liegt. In einem solchen Fall kann stattdessen ranges::move verwendet werden.
2) Gleich wie (1) , verwendet jedoch r als Quellbereich, als ob ranges:: begin ( r ) als first und ranges:: end ( r ) als last verwendet würde.

Die Elemente im moved-from Bereich enthalten weiterhin gültige Werte des entsprechenden Typs, aber nicht notwendigerweise dieselben Werte wie vor der Verschiebung, als ob * ( d_last - n ) = ranges:: iter_move ( last - n ) für jede ganze Zahl n verwendet würde, wobei 0 ≤ n < N .

Die auf dieser Seite beschriebenen funktionsähnlichen Entitäten sind Algorithm Function Objects (informell bekannt als Niebloids ), das heißt:

Inhaltsverzeichnis

Parameter

first, last - das Iterator-Sentinel-Paar, das den Bereich der zu verschiebenden Elemente definiert
r - der Bereich der zu verschiebenden Elemente
d_last - das Ende des Zielbereichs

Rückgabewert

{ last, d_last - N } .

Komplexität

1) Genau N Move-Zuweisungen.
2) Genau ranges:: distance ( r ) Move-Zuweisungen.

Hinweise

Beim Verschieben überlappender Bereiche ist ranges::move geeignet, wenn nach links verschoben wird (Anfang des Zielbereichs liegt außerhalb des Quellbereichs), während ranges::move_backward geeignet ist, wenn nach rechts verschoben wird (Ende des Zielbereichs liegt außerhalb des Quellbereichs).

Mögliche Implementierung

struct move_backward_fn
{
    template<std::bidirectional_iterator I1, std::sentinel_for<I1> S1,
             std::bidirectional_iterator I2>
    requires std::indirectly_movable<I1, I2>
    constexpr ranges::move_backward_result<I1, I2>
        operator()(I1 first, S1 last, I2 d_last) const
    {
        auto i {last};
        for (; i != first; *--d_last = ranges::iter_move(--i))
        {}
        return {std::move(last), std::move(d_last)};
    }
    template<ranges::bidirectional_range R, std::bidirectional_iterator I>
    requires std::indirectly_movable<ranges::iterator_t<R>, I>
    constexpr ranges::move_backward_result<ranges::borrowed_iterator_t<R>, I>
        operator()(R&& r, I d_last) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::move(d_last));
    }
};
inline constexpr move_backward_fn move_backward {};

Beispiel

#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
using Vec = std::vector<std::string>;
void print(std::string_view rem, Vec const& vec)
{
    std::cout << rem << "[" << vec.size() << "]: ";
    for (const std::string& s : vec)
        std::cout << (s.size() ? s : std::string{"·"}) << ' ';
    std::cout << '\n';
}
int main()
{
    Vec a{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"};
    Vec b(a.size());
    print("Before move:\n" "a", a);
    print("b", b);
    std::ranges::move_backward(a, b.end());
    print("\n" "Move a >> b:\n" "a", a);
    print("b", b);
    std::ranges::move_backward(b.begin(), b.end(), a.end());
    print("\n" "Move b >> a:\n" "a", a);
    print("b", b);
    std::ranges::move_backward(a.begin(), a.begin()+3, a.end());
    print("\n" "Overlapping move a[0, 3) >> a[5, 8):\n" "a", a);
}

Mögliche Ausgabe:

Before move:
a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
b[8]: · · · · · · · ·
Move a >> b:
a[8]: · · · · · · · ·
b[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
Move b >> a:
a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █
b[8]: · · · · · · · ·
Overlapping move a[0, 3) >> a[5, 8):
a[8]: · · · ▄ ▅ ▁ ▂ ▃

Siehe auch

verschiebt einen Bereich von Elementen an einen neuen Speicherort
(Algorithmus-Funktionsobjekt)
kopiert einen Bereich von Elementen an einen neuen Speicherort
(Algorithmus-Funktionsobjekt)
kopiert einen Bereich von Elementen in umgekehrter Reihenfolge
(Algorithmus-Funktionsobjekt)
(C++11)
verschiebt einen Bereich von Elementen an einen neuen Speicherort
(Funktions-Template)
(C++11)
konvertiert das Argument zu einem xvalue
(Funktions-Template)