Partager via


random_access_iterator_tag, struct

Une classe qui fournit un type de retour pour une fonction iterator_category représentant un random-access iterator.

struct random_access_iterator_tag 
   : public bidirectional_iterator_tag {};

Notes

Les classes d'indicateur de catégorie sont utilisées comme indicateurs de compilation pour la sélection d'algorithme. La fonction de modèle doit découvrir quelle est la catégorie la plus spécifique de cet argument d'itérateur de façon à pouvoir utiliser l'algorithme le plus efficace lors de la compilation. Pour chaque itération de type Iterator, iterator_traits<Iterator>::iterator_category doit être défini comme étant l'indicateur de catégorie le plus spécifique qui décrit le comportement de l'itérateur.

Le type est identique à iterator<Iter>::iterator_category lorsque Iter décrit un objet pouvant servir d'itérateur à accès aléatoire.

Exemple

// iterator_rait.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>
#include <list>

using namespace std;

int main( )
{
   vector<int> vi;
   vector<char> vc;
   list<char> lc;
   iterator_traits<vector<int>:: iterator>::iterator_category cati;
   iterator_traits<vector<char>:: iterator>::iterator_category catc;
   iterator_traits<list<char>:: iterator>::iterator_category catlc;

   // These are both random-access iterators
   cout << "The type of iterator for vector<int> is "
       << "identified by the tag:\n " 
       << typeid ( cati ).name( ) << endl;
   cout << "The type of iterator for vector<char> is "
       << "identified by the tag:\n " 
       << typeid ( catc ).name( ) << endl;
   if ( typeid ( cati ) == typeid( catc ) )
      cout << "The iterators are the same." << endl << endl;
   else
      cout << "The iterators are not the same." << endl << endl;

   // But the list iterator is bidirectinal, not random access
   cout << "The type of iterator for list<char> is "
       << "identified by the tag:\n " 
       << typeid (catlc).name( ) << endl;

   // cout << ( typeid ( vi.begin( ) ) == typeid( vc.begin( ) ) ) << endl;
   if ( typeid ( vi.begin( ) ) == typeid( vc.begin( ) ) )
      cout << "The iterators are the same." << endl;
   else
      cout << "The iterators are not the same." << endl;
   // A random-access iterator is a bidirectional iterator.
   cout << ( void* ) dynamic_cast< iterator_traits<list<char>:: iterator>
          ::iterator_category* > ( &catc ) << endl;
}

Résultat de l'exemple

La sortie suivante est pour x86.

The type of iterator for vector<int> is identified by the tag:
 struct std::random_access_iterator_tag
The type of iterator for vector<char> is identified by the tag:
 struct std::random_access_iterator_tag
The iterators are the same.

The type of iterator for list<char> is identified by the tag:
 struct std::bidirectional_iterator_tag
The iterators are not the same.
0012FF3B

Configuration requise

En-tête : <iterator>

Espace de noms : std

Voir aussi

Référence

bidirectional_iterator_tag, struct

Sécurité des threads dans la bibliothèque standard C++

Bibliothèque STL (Standard Template Library)