在线文档教程
C++
迭代器 | Iterator

std::bidirectional_iterator_tag

STD::输入[医]迭代器[医]Tag,STD::Output[医]迭代器[医]Tag,STD::转发[医]迭代器[医]标签,STD::双向[医]迭代器[医]标签,STD::随机[医]存取[医]迭代器[医]标签

Defined in header
struct input_iterator_tag { };
struct output_iterator_tag { };
struct forward_iterator_tag : public input_iterator_tag { };
struct bidirectional_iterator_tag : public forward_iterator_tag { };
struct random_access_iterator_tag : public bidirectional_iterator_tag { };

定义迭代器的类别。每个标记都是空类型,对应于五个迭代器类别中的一个:

  • input_iterator_tag对应于InputIterator...

  • output_iterator_tag对应于OutputIterator...

  • forward_iterator_tag对应于ForwardIterator...

  • bidirectional_iterator_tag对应于BidirectionalIterator...

  • random_access_iterator_tag对应于RandomAccessIterator...

Iterator类别标记携带的信息可用于为类别所隐含的特定需求集选择最有效的算法。

对于每个迭代器类型,std::iterator_traits<Iterator>::iterator_category是可用的,这是这五种标记类型之一的别名。

Notes There is no separate tag for ContiguousIterator.(since C++17)

基于迭代器类别标记的算法选择的常用技术是使用Dispatcher函数%28。std::enable_if29%。

二次

#include <iostream> #include <vector> #include <list> #include <iterator> template< class BDIter > void alg(BDIter, BDIter, std::bidirectional_iterator_tag) { std::cout << "alg() called for bidirectional iterator\n"; } template <class RAIter> void alg(RAIter, RAIter, std::random_access_iterator_tag) { std::cout << "alg() called for random-access iterator\n"; } template< class Iter > void alg(Iter first, Iter last) { alg(first, last, typename std::iterator_traits<Iter>::iterator_category() } int main() { std::vector<int> v; alg(v.begin(), v.end() std::list<int> l; alg(l.begin(), l.end() // std::istreambuf_iterator<char> i1(std::cin), i2; // alg(i1, i2 // compile error: no matching function for call }

二次

产出:

二次

alg() called for random-access iterator alg() called for bidirectional iterator

二次

另见

iterator (deprecated in C++17)base class to ease the definition of required types for simple iterators (class template)
iterator_traitsprovides uniform interface to the properties of an iterator (class template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/iterator/iterator[医]标签