添加两个STL向量 [英] Adding two STL vectors
问题描述
vector< doublev1;
vector< doublev2;
将v1添加到v2的最佳方法是什么? (v2 = v1 + v2)?只需
迭代,还是可以使用像变换这样的算法?我已经使用了
变换向向量添加单个值,但没有将两个向量添加到一起。
谢谢c ++ board。
vector<doublev1;
vector<doublev2;
What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have used
transform to add a single value to a vector, but not two vectors together.
Thank you c++ board.
推荐答案
Chris Roth写道:
Chris Roth wrote:
vector< doublev1;
vector< doublev2 ;
将v1添加到v2的最佳方法是什么? (v2 = v1 + v2)?只需
迭代,还是可以使用像变换这样的算法?我用
使用变换为向量添加单个值,但不是两个向量
。
vector<doublev1;
vector<doublev2;
What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two vectors
together.
v2.resize(v1.size());
std :: transform(v1.begin(),v1.end( ),v2.begin(),v2.begin(),
加上< double>());
应该这样做......
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
v2.resize(v1.size());
std::transform(v1.begin(), v1.end(), v2.begin(), v2.begin(),
plus<double>());
should do it...
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Chris Roth写道:
Chris Roth wrote:
vector< doublev1;
vector< doublev2;
将v1添加到v2的最佳方法是什么? (v2 = v1 + v2)?只需
迭代,还是可以使用像变换这样的算法?我使用了
变换将一个值添加到矢量中,但不是两个矢量一起添加。
vector<doublev1;
vector<doublev2;
What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have used
transform to add a single value to a vector, but not two vectors together.
单值:vector :: push_back应该没问题:)
要加入两个向量,你可以使用
vector :: insert(),就像这里一样
#include< iostream>
#include< vector>
#include< iterator>
使用命名空间std;
int main()
{
vector< intv1;
vector< intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
v1.insert(v1.end(),v2.begin( ),v2.end());
copy(v1.begin(),v1.end(),ostream_iterator< int>(cout,""));
返回0;
}
Single value: vector::push_back should be ok :)
To join two vectors you could use
vector::insert(), like here
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<intv1;
vector<intv2;
v1.push_back(5);
v2.push_back(6);
v2.push_back(7);
v1.insert(v1.end(), v2.begin(), v2.end());
copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
return 0;
}
Marcin Gil写道:
Marcin Gil wrote:
Chris Roth写道:
Chris Roth wrote:
> vector< doublev1;
vector< doublev2;
什么是最好的将v1添加到v2的方法? (v2 = v1 + v2)?只需要遍历,或者可以使用像变换这样的算法吗?我用
将一个值添加到矢量中,但不是两个矢量一起添加。
>vector<doublev1;
vector<doublev2;
What is the best way to add v1 to v2? (v2 = v1+v2 that is)? Simply
iterate through, or can an algorithm like transform be used? I have
used transform to add a single value to a vector, but not two vectors
together.
Ups ..看起来我没听懂你的意思:)
干杯,
-Marcin
Ups.. Looks like I didn''t catch what you mean :)
Cheers,
-Marcin
这篇关于添加两个STL向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!