Namespaces
Variants

std::ranges:: swap_ranges, std::ranges:: swap_ranges_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
Défini dans l'en-tête <algorithm>
Signature d'appel
template < std:: input_iterator I1, std:: sentinel_for < I1 > S1,

std:: input_iterator I2, std:: sentinel_for < I2 > S2 >
requires std:: indirectly_swappable < I1, I2 >
constexpr swap_ranges_result < I1, I2 >

swap_ranges ( I1 first1, S1 last1, I2 first2, S2 last2 ) ;
(1) (depuis C++20)
template < ranges:: input_range R1, ranges:: input_range R2 >

requires std:: indirectly_swappable < ranges:: iterator_t < R1 > , ranges:: iterator_t < R2 >>
constexpr swap_ranges_result < ranges:: borrowed_iterator_t < R1 > ,
ranges:: borrowed_iterator_t < R2 >>

swap_ranges ( R1 && r1, R2 && r2 ) ;
(2) (depuis C++20)
Types auxiliaires
template < class I1, class I2 >
using swap_ranges_result = ranges:: in_in_result < I1, I2 > ;
(3) (depuis C++20)
1) Échange les éléments entre la première plage [ first1 , first1 + M ) et la deuxième plage [ first2 , first2 + M ) via ranges:: iter_swap ( first1 + i, first2 + i ) , où M = ranges:: min ( ranges:: distance ( first1, last1 ) , ranges:: distance ( first2, last2 ) ) .
Les plages [ first1 , last1 ) et [ first2 , last2 ) ne doivent pas se chevaucher.
2) Identique à (1) , mais utilise r1 comme premier intervalle et r2 comme second intervalle, comme si on utilisait ranges:: begin ( r1 ) comme first1 , ranges:: end ( r1 ) comme last1 , ranges:: begin ( r2 ) comme first2 , et ranges:: end ( r2 ) comme last2 .

Les entités de type fonction décrites sur cette page sont des objets fonction d'algorithme (informellement appelés niebloids ), c'est-à-dire :

Table des matières

Paramètres

first1, last1 - la paire itérateur-sentinelle définissant la première plage d'éléments à échanger
first2, last2 - la paire itérateur-sentinelle définissant la seconde plage d'éléments à échanger
r1 - la première plage d'éléments à échanger
r2 - la seconde plage d'éléments à échanger.

Valeur de retour

{ first1 + M, first2 + M } .

Complexité

Exactement M échanges.

Notes

Les implémentations (par exemple MSVC STL ) peuvent activer la vectorisation lorsque le type d'itérateur modélise contiguous_iterator et que l'échange de son type de valeur n'appelle ni fonction membre spéciale non triviale ni ADL -trouvée swap .

Implémentation possible

struct swap_ranges_fn
{
    template<std::input_iterator I1, std::sentinel_for<I1> S1,
             std::input_iterator I2, std::sentinel_for<I2> S2>
    requires std::indirectly_swappable<I1, I2>
    constexpr ranges::swap_ranges_result<I1, I2>
        operator()(I1 first1, S1 last1, I2 first2, S2 last2) const
    {
        for (; !(first1 == last1 or first2 == last2); ++first1, ++first2)
            ranges::iter_swap(first1, first2);
        return {std::move(first1), std::move(first2)};
    }
    template<ranges::input_range R1, ranges::input_range R2>
    requires std::indirectly_swappable<ranges::iterator_t<R1>, ranges::iterator_t<R2>>
    constexpr ranges::swap_ranges_result<ranges::borrowed_iterator_t<R1>,
                                         ranges::borrowed_iterator_t<R2>>
        operator()(R1&& r1, R2&& r2) const
    {
        return (*this)(ranges::begin(r1), ranges::end(r1),
                       ranges::begin(r2), ranges::end(r2));
    }
};
inline constexpr swap_ranges_fn swap_ranges {};

Exemple

#include <algorithm>
#include <iostream>
#include <list>
#include <string_view>
#include <vector>
auto print(std::string_view name, auto const& seq, std::string_view term = "\n")
{
    std::cout << name << " : ";
    for (const auto& elem : seq)
        std::cout << elem << ' ';
    std::cout << term;
}
int main()
{
    std::vector<char> p {'A', 'B', 'C', 'D', 'E'};
    std::list<char> q {'1', '2', '3', '4', '5', '6'};
    print("p", p);
    print("q", q, "\n\n");
    // échanger p[0, 2) et q[1, 3) :
    std::ranges::swap_ranges(p.begin(),
                             p.begin() + 4,
                             std::ranges::next(q.begin(), 1),
                             std::ranges::next(q.begin(), 3));
    print("p", p);
    print("q", q, "\n\n");
    // échanger p[0, 5) et q[0, 5) :
    std::ranges::swap_ranges(p, q);
    print("p", p);
    print("q", q);
}

Sortie :

p : A B C D E
q : 1 2 3 4 5 6
p : 2 3 C D E
q : 1 A B 4 5 6
p : 1 A B 4 5
q : 2 3 C D E 6

Voir aussi

(C++20)
échange les valeurs référencées par deux objets déréférençables
(objet de point de personnalisation)
échange les valeurs de deux objets
(objet de point de personnalisation)
échange deux plages d'éléments
(modèle de fonction)
échange les éléments pointés par deux itérateurs
(modèle de fonction)
échange les valeurs de deux objets
(modèle de fonction)