[ Web Proxy ]
URL:
Viewing: https://fr.cppreference.com/cpp/iterator/iterator_traits [Back]  [Original]

std::iterator_traits cppreference.com
cppreference.com
Espaces de noms
Variantes

std::iterator_traits

De cppreference.com
 
 
Bibliothque Iterator
Primitives Iterator
Original:
Iterator primitives
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
iterator_traits
input_iterator_tag
output_iterator_tag
forward_iterator_tag
bidirectional_iterator_tag
random_access_iterator_tag
iterator
Adaptateurs Iterator
Original:
Iterator adaptors
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
reverse_iterator
Itrateurs de flux
Original:
Stream iterators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
istream_iterator
ostream_iterator
istreambuf_iterator
ostreambuf_iterator
Oprations Iterator
Original:
Iterator operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
advance
distance
prev (C++11)
next (C++11)
Gamme d'accs
Original:
Range access
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
begin (C++11)
end (C++11)
 
<tbody> </tbody>
Dclar dans l'en-tte <iterator>
template< class Iterator> struct iterator_traits;
template< class T > struct iterator_traits<T*>;
template< class T > struct iterator_traits<const T*>;

std::iterator_traits est la classe "gnrique" qui fournit une interface uniforme pour accder aux proprits du type itrateur. Cela permet d'implmenter des algorithmes seulement en termes d'itrateurs.

Types des membres

Type de membre Dfinition
difference_type Iterator::difference_type
value_type Iterator::value_type
pointer Iterator::pointer
reference Iterator::reference
iterator_category Iterator::iterator_category

Spcialisations

std::iterator_traits tant "gnrique", il peut tre spcialis pour des types donns par l'utilisateur qui peuvent tre utiliss comme des itrateurs. La bibliothque standard fournit deux spcialisations partielles pour des pointeurs de type T*, ce qui permet d'utiliser tous les algorithmes bass sur des itrateurs avec de simples pointeurs.

Spcialisation T* : types des membres

Type de membre Dfinition
difference_type std::ptrdiff_t
value_type T
pointer T*
reference T&
iterator_category std::random_access_iterator_tag

Spcialisation const T* : types des membres

Type de membre Dfinition
difference_type std::ptrdiff_t
value_type T
pointer const T*
reference const T&
iterator_category std::random_access_iterator_tag

Exemple

implmentation universelle de reverse () pour les itrateurs bidirectionnels

#include <iostream>
#include <iterator>
#include <vector>
#include <list>

template<class BDIter>
void my_reverse(BDIter first, BDIter last)
{
    typename std::iterator_traits<BDIter>::difference_type n = std::distance(first, last);
    --n;
    while(n > 0) {
        typename std::iterator_traits<BDIter>::value_type tmp = *first;
        *first++ = *--last;
        *last = tmp;
        n -= 2;
    }
}

int main()
{
    std::vector<int> v{1,2,3,4,5};
    my_reverse(v.begin(), v.end());
    for(int n : v)
        std::cout << n << ' ';
    std::cout << '\n';

    std::list<int> l{1,2,3,4,5};
    my_reverse(l.begin(), l.end());
    for(auto n : l)
        std::cout << n << ' ';
    std::cout << '\n';

//    std::istreambuf_iterator<char> i1(std::cin), i2;
//    my_reverse(i1, i2); // erreur de compilation

}

Rsultat :

5 4 3 2 1
5 4 3 2 1

Voir aussi

l'itrateur de base
Original:
the basic iterator
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(classe gnrique) [edit]
types de classes vides utiliss pour indiquer les catgories d'itrateurs
Original:
empty class types used to indicate iterator categories
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(classe) [edit]

Web Proxy Viewer  |  New URL  |  Original Page