<small id='a4PIq'></small><noframes id='a4PIq'>

      <bdo id='a4PIq'></bdo><ul id='a4PIq'></ul>

  1. <legend id='a4PIq'><style id='a4PIq'><dir id='a4PIq'><q id='a4PIq'></q></dir></style></legend>
    1. <tfoot id='a4PIq'></tfoot>
      <i id='a4PIq'><tr id='a4PIq'><dt id='a4PIq'><q id='a4PIq'><span id='a4PIq'><b id='a4PIq'><form id='a4PIq'><ins id='a4PIq'></ins><ul id='a4PIq'></ul><sub id='a4PIq'></sub></form><legend id='a4PIq'></legend><bdo id='a4PIq'><pre id='a4PIq'><center id='a4PIq'></center></pre></bdo></b><th id='a4PIq'></th></span></q></dt></tr></i><div id='a4PIq'><tfoot id='a4PIq'></tfoot><dl id='a4PIq'><fieldset id='a4PIq'></fieldset></dl></div>

      如何检查传递的 Iterator 是否为随机访问迭代器?

      时间:2024-05-11
      • <bdo id='x9bKg'></bdo><ul id='x9bKg'></ul>

                  <tbody id='x9bKg'></tbody>
                <tfoot id='x9bKg'></tfoot>

                <small id='x9bKg'></small><noframes id='x9bKg'>

                <i id='x9bKg'><tr id='x9bKg'><dt id='x9bKg'><q id='x9bKg'><span id='x9bKg'><b id='x9bKg'><form id='x9bKg'><ins id='x9bKg'></ins><ul id='x9bKg'></ul><sub id='x9bKg'></sub></form><legend id='x9bKg'></legend><bdo id='x9bKg'><pre id='x9bKg'><center id='x9bKg'></center></pre></bdo></b><th id='x9bKg'></th></span></q></dt></tr></i><div id='x9bKg'><tfoot id='x9bKg'></tfoot><dl id='x9bKg'><fieldset id='x9bKg'></fieldset></dl></div>

                <legend id='x9bKg'><style id='x9bKg'><dir id='x9bKg'><q id='x9bKg'></q></dir></style></legend>
              • 本文介绍了如何检查传递的 Iterator 是否为随机访问迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下代码,它执行一些迭代器算法:

                I have the following code, which does some iterator arithmetic:

                template<class Iterator>
                void Foo(Iterator first, Iterator last) {
                  typedef typename Iterator::value_type Value;
                  std::vector<Value> vec;
                  vec.resize(last - first);
                  // ...
                }
                

                (last - first) 表达式 (AFAIK) 仅适用于随机访问迭代器(例如来自 vectordeque 的迭代器).如何检查传递的迭代器满足此要求的代码?

                The (last - first) expression works (AFAIK) only for random access iterators (like the ones from vector and deque). How can I check in the code that the passed iterator meets this requirement?

                推荐答案

                如果 Iterator 是随机访问迭代器,则

                If Iterator is a random access iterator, then

                std::iterator_traits<Iterator>::iterator_category
                

                将是 std::random_access_iterator_tag.最简洁的实现方法可能是创建第二个函数模板并让 Foo 调用它:

                will be std::random_access_iterator_tag. The cleanest way to implement this is probably to create a second function template and have Foo call it:

                template <typename Iterator>
                void FooImpl(Iterator first, Iterator last, std::random_access_iterator_tag) { 
                    // ...
                }
                
                template <typename Iterator>
                void Foo(Iterator first, Iterator last) {
                    typedef typename std::iterator_traits<Iterator>::iterator_category category;
                    return FooImpl(first, last, category());
                }
                

                这样做的好处是,您可以根据需要为不同类别的迭代器重载 FooImpl.

                This has the advantage that you can overload FooImpl for different categories of iterators if you'd like.

                Scott Meyers 在其中一本Effective C++ 书中讨论了这种技术(我不记得是哪一本).

                Scott Meyers discusses this technique in one of the Effective C++ books (I don't remember which one).

                这篇关于如何检查传递的 Iterator 是否为随机访问迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:C++11 std::to_string(double) - 没有尾随零 下一篇:在迭代时从地图(或任何其他 STL 容器)中擦除/删除内容

                相关文章

              • <legend id='91kDX'><style id='91kDX'><dir id='91kDX'><q id='91kDX'></q></dir></style></legend>

                1. <i id='91kDX'><tr id='91kDX'><dt id='91kDX'><q id='91kDX'><span id='91kDX'><b id='91kDX'><form id='91kDX'><ins id='91kDX'></ins><ul id='91kDX'></ul><sub id='91kDX'></sub></form><legend id='91kDX'></legend><bdo id='91kDX'><pre id='91kDX'><center id='91kDX'></center></pre></bdo></b><th id='91kDX'></th></span></q></dt></tr></i><div id='91kDX'><tfoot id='91kDX'></tfoot><dl id='91kDX'><fieldset id='91kDX'></fieldset></dl></div>
                    <bdo id='91kDX'></bdo><ul id='91kDX'></ul>
                  <tfoot id='91kDX'></tfoot>

                    <small id='91kDX'></small><noframes id='91kDX'>