你如何转换矢量< float> A到矢量< double>乙

你如何转换矢量< float> A到矢量< double>乙

问题描述:

为什么标准矢量没有这样的转换?

and why doesnt the standard vector have such conversion available?

< bl ******** **@gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
以及为什么标准向量没有这样的转换可用?
and why doesnt the standard vector have such conversion available?




你可以使用带有两个迭代器的向量构造函数:


#include< vector>

#include< assert.h>


使用命名空间std;


int main()

{

vector< float> a;

a.push_back(1.2f);


vector< double> b(a.begin(),a.end());


断言(!b.empty());

cout<< b.front()<< ''\\ n'';

}


阿里



You can use the vector constructor that takes two iterators:

#include <vector>
#include <assert.h>

using namespace std;

int main()
{
vector<float> a;
a.push_back(1.2f);

vector<double> b(a.begin(), a.end());

assert(!b.empty());
cout << b.front() << ''\n'';
}

Ali


&gt ; Re:你如何转换矢量< float> A到矢量< double> B


std :: vector< float> A;

/ *等* /

std :: vector< double> B(A.begin(),A.end())


< bl ********** @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
> Re: how do u convert a vector<float> A to a vector<double> B

std::vector<float> A;
/* etc */
std::vector<double> B(A.begin(), A.end())

<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
以及为什么标准矢量没有这样的转换?
and why doesnt the standard vector have such conversion available?




确实如此。见上文。


-Mike



It does. See above.

-Mike


如果已经构建了b怎么办?

另一个问题

假设你有

vector< float> a(3);

// init a

vector< std :: complex< float> &GT; c(3);

// init c

现在我需要类似

real(c)= a;


目前我有

for(int i = 0; i< c.size(); i ++)

c [i] .real() = a [i];


我真的不喜欢。

What if b is already constructed?
Another question
Say you have
vector<float> a(3);
//init a
vector<std::complex<float> > c(3);
//init c
Now I want something like
real(c)=a;

Currently I have
for(int i=0;i<c.size();i++)
c[i].real()=a[i];

Which I dont really like.