如何添加元素的两个STL向量的元素? [英] How to add element by element of two STL vectors?

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

问题描述

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

 矢量< double>结果; 
矢量< double> result_temp
for(int i = 0; i <10; i ++)result_temp.push_back(i);

result + = result_temp //我想这样做。
for(int i = 0; i

我想做的数学运算是



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



p>

感谢



编辑:添加了一个简单的初始化,因为这不是重点。如何初始化结果?

解决方案

如果您要尝试附加一个向量到另一个,你可以使用像下面的东西。这些是我的一个实用程序库 - 两个 operator + = 重载 std :: vector :一个元素添加到向量,另一个附加整个向量

  template< typename T> 
std :: vector< T>运算符+ =(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;
}

如果您尝试执行求和(即创建一个新包含两个其他向量的元素的总和的向量),您可以使用类似如下:

  #include< algorithm> 
#include< functional>

template< typename T>
std :: vector< T>运算符+(const std :: vector< T& a,const std :: vector< T& b)
{
assert(a.size()== b.size ;

std :: vector< T>结果;
result.reserve(a.size());

std :: transform(a.begin(),a.end(),b.begin(),
std :: back_inserter(result),std :: plus< T& ());
return result;
}

您可以类似地实现 operator + = overload。


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

The mathematical operation that I'm trying to do is

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

What can be done?

Thanks

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

解决方案

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;
}

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;
}

You could similarly implement an operator+= overload.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆