分享方式:


random_access_iterator_tag 結構

類別,提供代表隨機存取反覆運算器的函式傳回型 iterator_category 別。

語法

struct random_access_iterator_tag    : public bidirectional_iterator_tag {};

備註

分類標籤類別會用來當作演算法選擇的編譯標籤。 範本函式必須尋找其迭代器引數的最明確分類,如此它才能在編譯階段使用最有效率的演算法。 針對每個 Iterator 類型的迭代器,必須將 iterator_traits<Iterator>::iterator_category 定義為描述迭代器行為的最明確分類標籤。

當描述可作為隨機存取反覆運算器的物件時,此類型與 反覆運算器 < Iter > ::iterator_category 相同。 Iter

範例

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

範例輸出

以下輸出適用於 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

需求

Header: < iterator>

命名空間:std

另請參閱

bidirectional_iterator_tag 結構
C++ 標準程式庫中的執行緒安全
C++ 標準程式庫參考