Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::input_iterator_tag,std::output_iterator_tag,std::forward_iterator_tag,std::bidirectional_iterator_tag,std::random_access_iterator_tag,std::contiguous_iterator_tag

      From cppreference.com
      <cpp‎ |iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      input_iterator_tagoutput_iterator_tagforward_iterator_tagbidirectional_iterator_tagrandom_access_iterator_tagcontiguous_iterator_tag
      (C++20)
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       
      Defined in header<iterator>
      struct input_iterator_tag{};
      (1)
      struct output_iterator_tag{};
      (2)
      struct forward_iterator_tag:public input_iterator_tag{};
      (3)
      struct bidirectional_iterator_tag:public forward_iterator_tag{};
      (4)
      struct random_access_iterator_tag:public bidirectional_iterator_tag{};
      (5)
      struct contiguous_iterator_tag:public random_access_iterator_tag{};
      (6)(since C++20)

      Defines the category of an iterator. Each tag is an empty type.

      Contents

      [edit]Iterator category

      For everyLegacyIterator typeIt, atypedefstd::iterator_traits<It>::iterator_category must be defined to be an alias to one of these tag types, to indicate the most specific category thatIt is in.

      1. input_iterator_tag corresponds toLegacyInputIterator.
      2. output_iterator_tag corresponds toLegacyOutputIterator.
      3. forward_iterator_tag corresponds toLegacyForwardIterator.
      4. bidirectional_iterator_tag corresponds toLegacyBidirectionalIterator.
      5. random_access_iterator_tag corresponds toLegacyRandomAccessIterator.

      Iterator category tags carry information that can be used to select the most efficient algorithms for the specific requirement set that is implied by the category.

      Iterator concept

      For everyinput_iterator typeIt, eitherIt::iterator_concept (ifstd::iterator_traits<It> is generated from primary template) orstd::iterator_traits<It>::iterator_concept (ifstd::iterator_traits<It> is specialized) may be declared as an alias to one of these tags, to indicate the strongest iterator concept thatIt intends to model.

      1. input_iterator_tag corresponds toinput_iterator.
      2. forward_iterator_tag corresponds toforward_iterator.
      3. bidirectional_iterator_tag corresponds tobidirectional_iterator.
      4. random_access_iterator_tag corresponds torandom_access_iterator.
      5. contiguous_iterator_tag corresponds tocontiguous_iterator.

      Ifiterator_concept is not provided,iterator_category is used as a fallback. Ifiterator_category is not provided either (i.e.It is not aLegacyIterator), andstd::iterator_traits<It> is not specialized,random_access_iterator_tag is assumed.

      In any case, each concept is not satisfied if the required operations are not supported, regardless of the tag.

      (since C++20)

      [edit]Notes

      There is no separate tag forLegacyContiguousIterator. That is, it is not possible to tell aLegacyContiguousIterator based on itsiterator_category.To define specialized algorithm for contiguous iterators, use thecontiguous_iterator concept.(since C++20)

      There are no correspondences betweenoutput_iterator_tag and theoutput_iterator concept. Settingiterator_concept tooutput_iterator_tag only indicates that the type does not modelinput_iterator.

      [edit]Example

      Common technique for algorithm selection based on iterator category tags is to use a dispatcher function (the alternative isstd::enable_if).The iterator tag classes are also used in the corresponding concepts definitions to denote the requirements, which can't be expressed in terms of usage patterns alone.(since C++20)

      Run this code
      #include <iostream>#include <iterator>#include <list>#include <vector> // Using concepts (tag checking is part of the concepts themselves) template<std::bidirectional_iterator BDIter>void alg(BDIter, BDIter){std::cout<<"1. alg()\t called for bidirectional iterator\n";} template<std::random_access_iterator RAIter>void alg(RAIter, RAIter){std::cout<<"2. alg()\t called for random-access iterator\n";} // Legacy, using tag dispatch namespace legacy{// Quite often implementation details are hidden in a dedicated namespacenamespace implementation_details{template<class BDIter>void alg(BDIter, BDIter, std::bidirectional_iterator_tag){std::cout<<"3. legacy::alg() called for bidirectional iterator\n";} template<class RAIter>void alg(RAIter, RAIter, std::random_access_iterator_tag){std::cout<<"4. legacy::alg() called for random-access iterator\n";}}// namespace implementation_details template<class Iter>void alg(Iter first, Iter last){        implementation_details::alg(first, last,typenamestd::iterator_traits<Iter>::iterator_category());}}// namespace legacy int main(){std::list<int> l;    alg(l.begin(), l.end());// 1.    legacy::alg(l.begin(), l.end());// 3. std::vector<int> v;    alg(v.begin(), v.end());// 2.    legacy::alg(v.begin(), v.end());// 4. //  std::istreambuf_iterator<char> i1(std::cin), i2;//  alg(i1, i2);         // compile error: no matching function for call//  legacy::alg(i1, i2); // compile error: no matching function for call}

      Output:

      1. alg()  called for bidirectional iterator3. legacy::alg() called for bidirectional iterator2. alg()  called for random-access iterator4. legacy::alg() called for random-access iterator

      [edit]See also

      (deprecated in C++17)
      base class to ease the definition of required types for simple iterators
      (class template)[edit]
      provides uniform interface to the properties of an iterator
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/iterator_tags&oldid=159922"

      [8]ページ先頭

      ©2009-2025 Movatter.jp