Namespaces
Variants

std::ranges:: uninitialized_copy_n, std::ranges:: uninitialized_copy_n_result

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
Défini dans l'en-tête <memory>
Signature d'appel
template < std:: input_iterator I,

no-throw-input-iterator O, no - throw - sentinel - for < O > S >
requires std:: constructible_from < std:: iter_value_t < O > ,
std:: iter_reference_t < I >>
uninitialized_copy_n_result < I, O >
uninitialized_copy_n ( I ifirst, std:: iter_difference_t < I > count,

O ofirst, S olast ) ;
(1) (depuis C++20)
(constexpr depuis C++26)
Types auxiliaires
template < class I, class O >
using uninitialized_copy_n_result = ranges:: in_out_result < I, O > ;
(2) (depuis C++20)

Soit N égal à ranges:: min ( count, ranges:: distance ( ofirst, olast ) ) .

Copie N éléments de la plage commençant à ifirst vers une zone mémoire non initialisée [ ofirst , olast ) comme par auto ret = ranges:: uninitialized_copy ( std:: counted_iterator ( std :: move ( ifirst ) , count ) ,
std:: default_sentinel , ofirst, olast ) ;
return { std :: move ( ret. in ) . base ( ) , ret. out } ;

Si une exception est levée pendant l'initialisation, les objets déjà construits sont détruits dans un ordre non spécifié.

Si [ ofirst , olast ) chevauche ifirst + [ 0 , count ) , le comportement est indéfini.

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

Table des matières

Paramètres

ifirst - le début de la plage d'éléments à copier
count - le nombre d'éléments à copier
ofirst, olast - la paire itérateur-sentinelle définissant la plage de destination des éléments

Valeur de retour

Comme décrit ci-dessus.

Complexité

𝓞(N) .

Exceptions

Toute exception levée lors de la construction des éléments dans la plage de destination.

Notes

Une implémentation peut améliorer l'efficacité de ranges::uninitialized_copy_n , en utilisant par exemple ranges::copy_n , si le type de valeur de la plage de sortie est TrivialType .

Macro de test de fonctionnalité Valeur Std Fonctionnalité
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr pour les algorithmes de mémoire spécialisés , ( 1 )

Implémentation possible

struct uninitialized_copy_n_fn
{
    template<std::input_iterator I, no-throw-input-iterator O, no-throw-sentinel-for<O> S>
        requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>>
    constexpr ranges::uninitialized_copy_n_result<I, O>
        operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const
    {
        auto iter = std::counted_iterator(std::move(ifirst), count);
        auto ret = ranges::uninitialized_copy(iter, std::default_sentinel, ofirst, olast);
        return {std::move(ret.in).base(), ret.out};
    }
};
inline constexpr uninitialized_copy_n_fn uninitialized_copy_n{};

Exemple

#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    const char* stars[]{"Procyon", "Spica", "Pollux", "Deneb", "Polaris"};
    constexpr int n{4};
    alignas(alignof(std::string)) char out[n * sizeof(std::string)];
    try
    {
        auto first{reinterpret_cast<std::string*>(out)};
        auto last{first + n};
        auto ret{std::ranges::uninitialized_copy_n(std::begin(stars), n, first, last)};
        std::cout << '{';
        for (auto it{first}; it != ret.out; ++it)
            std::cout << (it == first ? "" : ", ") << std::quoted(*it);
        std::cout << "};\n";
        std::ranges::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "uninitialized_copy_n exception\n";
    }
}

Sortie :

{"Procyon", "Spica", "Pollux", "Deneb"};

Voir aussi

copie une plage d'objets vers une zone mémoire non initialisée
(objet fonction algorithme)
copie un nombre d'objets vers une zone mémoire non initialisée
(modèle de fonction)