1. <legend id='Y015Y'><style id='Y015Y'><dir id='Y015Y'><q id='Y015Y'></q></dir></style></legend>

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

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

    1. 如何逐个添加两个 STL 向量的元素?

      时间:2023-09-15

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

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

        <tfoot id='ptWla'></tfoot>

              • <bdo id='ptWla'></bdo><ul id='ptWla'></ul>
                  <tbody id='ptWla'></tbody>
                <legend id='ptWla'><style id='ptWla'><dir id='ptWla'><q id='ptWla'></q></dir></style></legend>

                本文介绍了如何逐个添加两个 STL 向量的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                这个问题很愚蠢,但我需要以一种非常有效的方式来做——它将在我的代码中再次执行.我有一个返回向量的函数,我必须将返回的值逐个元素添加到另一个向量中.很简单:

                The question is quite dumb, but I need to do it in a very efficient way - it will be performed over an over again in my code. I have a function that returns a vector, and I have to add the returned values to another vector, element by element. Quite simple:

                vector<double> result;
                vector<double> result_temp
                for(int i=0; i< 10; i++) result_temp.push_back(i);
                
                result += result_temp //I would like to do something like that.
                for(int i =0; i< result_temp.size();i++)result[i] += result_temp[i]; //this give me segfault
                

                我想做的数学运算是

                u[i] = u[i] + v[i] 对于所有的 i

                u[i] = u[i] + v[i] for all i

                可以做什么?

                谢谢

                添加了一个简单的初始化,因为这不是重点.结果应该如何初始化?

                added a simple initialization, as that is not the point. How should result be initialized?

                推荐答案

                如果您尝试将一个 vector 附加到另一个,您可以使用以下类似的方法.这些来自我的一个实用程序库 - std::vector 的两个 operator+= 重载:一个将单个元素附加到 vector,另一个附加整个vector:

                If you are trying to append one vector to another, you can use something like the following. These are from one of my utilities libraries--two operator+= overloads for std::vector: one appends a single element to the vector, the other appends an entire vector:

                template <typename T>
                std::vector<T>& operator+=(std::vector<T>& a, const std::vector<T>& b)
                {
                    a.insert(a.end(), b.begin(), b.end());
                    return a;
                }
                
                template <typename T>
                std::vector<T>& operator+=(std::vector<T>& aVector, const T& aObject)
                {
                    aVector.push_back(aObject);
                    return aVector;
                }
                

                如果您正在尝试执行求和(即,创建一个新的 vector 包含其他两个 vector 的元素之和),您可以使用一些东西像下面这样:

                If you are trying to perform a summation (that is, create a new vector containing the sums of the elements of two other vectors), you can use something like the following:

                #include <algorithm>
                #include <functional>
                
                template <typename T>
                std::vector<T> operator+(const std::vector<T>& a, const std::vector<T>& b)
                {
                    assert(a.size() == b.size());
                
                    std::vector<T> result;
                    result.reserve(a.size());
                
                    std::transform(a.begin(), a.end(), b.begin(), 
                                   std::back_inserter(result), std::plus<T>());
                    return result;
                }
                

                您可以类似地实现 operator+= 重载.

                You could similarly implement an operator+= overload.

                这篇关于如何逐个添加两个 STL 向量的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在需要旧式无符号字符的地方使用新的 std 下一篇:std::引用向量

                相关文章

                最新文章

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

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

                1. <tfoot id='vbrN9'></tfoot>
                  1. <small id='vbrN9'></small><noframes id='vbrN9'>

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