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

  1. <small id='N79z8'></small><noframes id='N79z8'>

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

    1. vector &lt;template&gt;, c++, class, add to vector

      时间:2023-10-07

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

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

              • <legend id='JYJdx'><style id='JYJdx'><dir id='JYJdx'><q id='JYJdx'></q></dir></style></legend>
                本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何存储向量&lt;bool&gt;或一个位集到文件中 下一篇:在 C++ 中,按第一个元素然后按第二个元素对成对

                相关文章

                最新文章

                <small id='3ACQY'></small><noframes id='3ACQY'>

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