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

<small id='99aFh'></small><noframes id='99aFh'>

        <bdo id='99aFh'></bdo><ul id='99aFh'></ul>
      <legend id='99aFh'><style id='99aFh'><dir id='99aFh'><q id='99aFh'></q></dir></style></legend><tfoot id='99aFh'></tfoot>
      1. vector &lt;template&gt;, c++, class, add to vector

        vector lt;templategt;, c++, class, adding to vector(vector lt;templategt;, c++, class, add to vector)

      2. <small id='0EoHK'></small><noframes id='0EoHK'>

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

              <tbody id='0EoHK'></tbody>
            <legend id='0EoHK'><style id='0EoHK'><dir id='0EoHK'><q id='0EoHK'></q></dir></style></legend>
                <bdo id='0EoHK'></bdo><ul id='0EoHK'></ul>

                  本文介绍了vector &lt;template&gt;, c++, class, add to vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个类,该类将从一组向量中绘制元素(并将这些向量作为类中的容器保存),但我觉得在管理具有许多函数(如 vectorOneAdd、vectorTwoAdd)的向量时为了向向量添加元素是没有意义的.一定有更好的方法,这就是我在这里问的原因,我听说您可以使用模板来做到这一点,但我不太确定如何.需要帮助.不想有很多无意义的代码.

                  I am trying to create a class thats going to draw elements from a set of vectors (and also hold these vectors as containers inside the class), but i feel that when managing the vector having lots of functions like vectorOneAdd, vectorTwoAdd used in order to add elements to the vector is pointless. There must be a better way, thats why i am asking here, I heard you can use templates to do it, but i am not quite certain how. Assistance needed. Don't want to have lots of pointless code in.

                  我在下面的意思的例子:

                  Example of what I mean below:

                  class Cookie
                  {
                  std::vector<Chocolate> chocolateContainer;
                  std::vector<Sugar> sugarContainer;
                  
                  void chocolateVectorAdd(Chocolate element);    // first function adding to one vector
                  void sugarVectorAdd(Sugar element);   // second function adding to another vector
                  }
                  

                  请使用示例代码,谢谢:)

                  Please use example code, thanks :)

                  推荐答案

                  使用像vectorOneAdd、vectorTwoAdd这样的函数来向向量添加元素是没有意义的.一定有更好的办法

                  having lots of functions like vectorOneAdd, vectorTwoAdd used in order to add elements to the vector is pointless. There must be a better way

                  有:

                  class Cookie {
                      std::vector<Chocolate> chocolateContainer;
                      std::vector<Sugar> sugarContainer;
                  
                  private:
                      template<typename T>
                      std::vector<T>& get_vector(const T&); // not implemented but particularized
                  
                      // write one of these for each vector:
                      template<>
                      std::vector<Chocolate>& get_vector(const Chocolate&) { return chocolateVector; }
                      template<>
                      std::vector<Sugar>& get_vector(const Sugar&) { return sugarVector; }
                  
                  public:
                      template<typename T>
                      void add(T element) {
                          auto& v = get_vector(element);
                          v.push_back(std::move(element));
                      }
                  };
                  

                  这篇关于vector &lt;template&gt;, c++, class, add to vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)
                    <tbody id='MWC2s'></tbody>

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

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

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