Compartilhar via


random_access_iterator_tag Struct

Uma classe que fornece um tipo de retorno da função de iterator_category que representa um iterador de acesso aleatório.

struct random_access_iterator_tag
   : public bidirectional_iterator_tag {};

Comentários

As classes da marca de categoria são usadas como criar marcas para a seleção do algoritmo.A função do modelo precisa encontrar a categoria a mais específica do argumento de iterador para que ele possa usar o algoritmo mais eficiente em tempo de compilação.Para cada iterador de tipo Iterator, iterator_traits<Iterator>::iterator_category deve ser definido para ser a marca a mais específica da categoria que descreve o comportamento de iterador.

O tipo é o mesmo que iterator<Iter>::iterator_category quando Iter descreve um objeto que pode servir como um iterador de acesso aleatório.

Exemplo

// 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;
}

A saída de exemplo

A saída a seguir são para x.

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

Requisitos

Cabeçalho: <iterator>

namespace: STD

Consulte também

Referência

bidirectional_iterator_tag Struct

Segurança do thread na biblioteca C++ padrão

Standard Template Library