random_access_iterator_tag — Struktura
Klasa, która udostępnia typ zwracany dla iterator_category
funkcji, która reprezentuje iterator dostępu losowego.
Składnia
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
Uwagi
Klasy tagów kategorii są używane jako tagi kompilacji do wyboru algorytmu. Funkcja szablonu musi znaleźć najbardziej specyficzną kategorię argumentu iteratora, aby można było użyć najbardziej wydajnego algorytmu w czasie kompilacji. Dla każdego iteratora typu Iterator
, :Iterator
<>iterator_traits
:iterator_category musi być zdefiniowany jako najbardziej konkretny tag kategorii opisujący zachowanie iteratora.
Typ jest taki sam jak iterator Iterator>::iterator_category, gdy Iter
opisuje obiekt, który może służyć jako iterator<dostępu losowego.
Przykład
// 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;
}
Przykładowe dane wyjściowe
Następujące dane wyjściowe są przeznaczone dla 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
Wymagania
Nagłówek:<iterator>
Przestrzeń nazw: std
Zobacz też
bidirectional_iterator_tag, struktura
Bezpieczeństwo wątku w standardowej bibliotece C++
Dokumentacja standardowej biblioteki C++
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla