• <small id='sgt42'></small><noframes id='sgt42'>

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

        <bdo id='sgt42'></bdo><ul id='sgt42'></ul>
        <tfoot id='sgt42'></tfoot>

        STL 算法:为什么没有额外的容器接口(除了迭代器对)?

        STL algorithms: Why no additional interface for containers (additional to iterator pairs)?(STL 算法:为什么没有额外的容器接口(除了迭代器对)?)
      1. <small id='ZFOs0'></small><noframes id='ZFOs0'>

            <legend id='ZFOs0'><style id='ZFOs0'><dir id='ZFOs0'><q id='ZFOs0'></q></dir></style></legend>

              • <bdo id='ZFOs0'></bdo><ul id='ZFOs0'></ul>
                  <tbody id='ZFOs0'></tbody>
                • <tfoot id='ZFOs0'></tfoot>

                  <i id='ZFOs0'><tr id='ZFOs0'><dt id='ZFOs0'><q id='ZFOs0'><span id='ZFOs0'><b id='ZFOs0'><form id='ZFOs0'><ins id='ZFOs0'></ins><ul id='ZFOs0'></ul><sub id='ZFOs0'></sub></form><legend id='ZFOs0'></legend><bdo id='ZFOs0'><pre id='ZFOs0'><center id='ZFOs0'></center></pre></bdo></b><th id='ZFOs0'></th></span></q></dt></tr></i><div id='ZFOs0'><tfoot id='ZFOs0'></tfoot><dl id='ZFOs0'><fieldset id='ZFOs0'></fieldset></dl></div>
                  本文介绍了STL 算法:为什么没有额外的容器接口(除了迭代器对)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道为什么 STL 不重载它们的算法函数,这样我就可以通过简单地提供一个容器而不是采用更冗长的方式来传递开始 + 结束迭代器来调用它们.我当然理解为什么我们还想使用迭代器对来处理容器/数组的子序列,但是,几乎所有对这些方法的调用都使用整个容器:

                  I'm wondering why the STL doesn't overload their algorithm functions such that I can call them by simply providing a container and not taking the more verbose way to pass begin + end iterators. I of course understand why we also want to use an iterator pair for processing subsequences of a container / array, however, almost all calls to these methods are using a whole container:

                  std::for_each(myVector.begin(), myVector.end(), doSomething);
                  

                  我觉得只写更方便、可读和可维护

                  I'd find it more convenient, readable and maintainable to just write

                  std::for_each(myVector, doSomething);
                  

                  STL 不提供这些重载是否有原因?它们是否引入了歧义?我正在考虑这样的事情:

                  Is there a reason STL doesn't provide these overloads? Do they introduce ambiguity? I'm thinking about something like this:

                  template<typename _Container, typename _Funct>
                  inline _Funct for_each(_Container c, _Funct f) {
                      return for_each(begin(c), end(c), f);
                  }
                  

                  我错过了什么吗?

                  推荐答案

                  他们确实为许多算法引入了歧义.很多看起来像

                  They do introduce ambiguity for many algorithms. A lot of <algorithm> looks like

                  template<class iterator>
                  void do_something(iterator, iterator);
                  
                  template<class iterator, class funct>
                  void do_something(iterator, iterator, funct);
                  

                  如果你添加额外的重载

                  template<class container, class funct>
                  void do_something(container, funct);
                  

                  编译器在弄清楚 do_something(x, y) 的含义时会遇到一些麻烦.如果 xy 是相同的 type,它将匹配 iterator = type 和 <代码>容器 = 类型,功能 = 类型.*)

                  the compiler will have some trouble figuring out what do_something(x, y) means. If x and y are of the same type, it will match both iterator = type and container = type, funct = type.*)

                  C++11 试图用 "concepts" 解决这个问题,它可以识别容器和迭代器.然而,这些概念"被证明过于复杂,无法纳入标准,因此这些重载也没有.

                  C++11 tried to solve this with "concepts" that could recognize the difference between a container and an iterator. However, these "concepts" turned out to be too complicated to make it into the standard, so neither did these overloads.

                  *) 编译器不同意这里,Comeau 编译器声称它是不明确的,g++ 4.5 和 MSVC 10 调用第一个函数.

                  在评论中进行了非常长时间的讨论后,这里有一个无法按预期工作的示例 - 使用还可以兼作谓词的容器适配器.

                  After an extremely long discussion in the comments, here is one example where it doesn't work as expected - using a container adapter that can also double as a predicate.

                  #include <iostream>
                  #include <vector>
                  
                  template<class iterator>
                  void test(iterator, iterator)
                  {
                     std::cout << "test iterator
                  ";
                  }
                  
                  template<class iterator, class predicate>
                  void test(iterator, iterator, predicate)
                  {
                     std::cout << "test iterator, predicate
                  ";
                  }
                  
                  template<class container, class predicate>
                  void test(const container& cont, predicate compare)
                  {
                     std::cout << "test container, predicate
                  ";
                  
                     test(cont.begin(), cont.end(), compare);
                  }
                  
                  template<class container>
                  class adapter
                  {
                  public:
                     typedef typename container::iterator   iterator;
                  
                     adapter(container* cont) : cont(cont)
                     { }
                  
                     iterator begin() const
                     { return cont->begin(); }
                  
                     iterator end() const
                     { return cont->end(); }
                  
                     bool operator()(const iterator& one, const iterator& two)
                     { return *one < *two; }
                  
                  private:
                     container* cont;
                  };
                  
                  int main()
                  {
                     std::vector<int>   v;
                  
                     adapter<std::vector<int>>   a(&v);
                  
                     test(a, a);
                  
                  }
                  

                  输出:

                  测试迭代器

                  http://ideone.com/wps2tZ

                  这篇关于STL 算法:为什么没有额外的容器接口(除了迭代器对)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                  • <bdo id='kyh8I'></bdo><ul id='kyh8I'></ul>

                      <tfoot id='kyh8I'></tfoot>

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

                        <legend id='kyh8I'><style id='kyh8I'><dir id='kyh8I'><q id='kyh8I'></q></dir></style></legend>
                          • <small id='kyh8I'></small><noframes id='kyh8I'>