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

        <small id='6zSQi'></small><noframes id='6zSQi'>

        <tfoot id='6zSQi'></tfoot>

        关于使用用户定义方法扩展 C++ STL 容器的更好方法的建议

        时间:2024-05-11

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

              <small id='48SUd'></small><noframes id='48SUd'>

                <tbody id='48SUd'></tbody>

                  <bdo id='48SUd'></bdo><ul id='48SUd'></ul>

                • <tfoot id='48SUd'></tfoot>
                • 本文介绍了关于使用用户定义方法扩展 C++ STL 容器的更好方法的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我从 C++ STL 容器继承并添加了我自己的方法.其基本原理是,对于客户端来说,它看起来像一个常规列表,但具有可以轻松调用的特定于应用程序的方法.

                  I inherited from C++ STL container and add my own methods to it. The rationale was such that to the clients, it will look act a regular list, yet has application-specific methods they can readily be called.

                  这很好用,但我已经阅读了很多关于不继承 STL 的帖子.有人可以就我如何以更好的方式编写下面的代码提供具体建议吗?

                  This works fine, but I have read numerous posts about not inheriting from STL. Can someone provide a concrete advice of how I might write the code below in a better way?

                  class Item
                  {
                    int a;
                    int b;
                    int c;
                  
                    int SpecialB()
                    {
                      return a * b + c;
                    }
                  };
                  
                  class ItemList : public std::vector<Item>
                  {
                    int MaxA()
                    {
                      if( this->empty() )
                        throw;
                  
                      int maxA = (*this)[0].a;
                  
                      for( int idx = 1; idx < this->size(); idx++ )
                      {
                        if( (*this)[idx].a > maxA )
                        {
                          maxA = (*this)[idx].a;
                        }
                      }
                      return maxA;
                    }
                  
                    int SpecialB()
                    {
                      if( this->empty() )
                        throw;
                  
                      int specialB = (*this)[0].SpecialB();
                  
                      for( int idx = 1; idx < this->size(); idx++ )
                      {
                        if( (*this)[idx].SpecialB() < specialB )
                        {
                          specialB -= (*this)[idx].c;
                        }
                      }
                      return specialB;
                    }
                  
                    int AvgC()
                    {
                      if( this->empty() )
                        throw;
                  
                      int cSum = 0;
                      for( int idx = 0; idx < this->size(); idx++ )
                      {
                        cSum += (*this)[idx].c;
                      }
                  
                      return cSum / this->size(); // average
                    }
                  };
                  

                  编辑:感谢您提供一系列深思熟虑的答案.我将改为创建辅助函数,从现在开始不再继承 STL 容器.

                  EDIT: Thanks for a bunch of thoughtful answers. I will create helper functions instead and from now on will never inherit from STL containers.

                  推荐答案

                  为什么需要以这种方式扩展向量?

                  why you need extend vector in this way?

                  对您的函子使用标准 .
                  例如

                  use standard <algorithm> with your functors.
                  e.g.

                  std::min_element, std::max_element

                  int max_a = std::max_element
                          ( 
                              v.begin(), 
                              v.end(), 
                              boost::bind( 
                                  std::less< int >(),
                                  bind( &Item::a, _1 ), 
                                  bind( &Item::a, _2 ) 
                              )
                          )->a;
                  

                  std::accumulate - 对于计算平均值

                  std::accumulate - for calculate avarage

                  const double avg_c = std::accumulate( v.begin(), v.end(), double( 0 ), boost::bind( Item::c, _1 ) ) / v.size(); // ofcourse check size before divide  
                  

                  您的 ItemList::SpecialB() 可以重写为:

                  your ItemList::SpecialB() could be rewrited as:

                  int accumulate_func( int start_from, int result, const Item& item )
                  {
                     if ( item.SpecialB() < start_from )
                     {
                         result -= item.SpecialB();
                     }
                     return result;
                  }
                  
                  if ( v.empty() )
                  {
                      throw sometghing( "empty vector" );
                  }
                  const int result = std::accumulate( v.begin(), v.end(), v.front(), boost::bind( &accumulate_func, v.front(), _1, _2 ) );
                  

                  顺便说一句:如果您不需要访问成员,则不需要继承.

                  BTW: if you don't need access to members, you don't need inheritance.

                  这篇关于关于使用用户定义方法扩展 C++ STL 容器的更好方法的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:std::成员函数指针的映射? 下一篇:C++,我可以在编译时静态初始化 std::map 吗?

                  相关文章

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

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

                      <legend id='kaFyX'><style id='kaFyX'><dir id='kaFyX'><q id='kaFyX'></q></dir></style></legend>
                      <tfoot id='kaFyX'></tfoot>