Convert vector<float> to vector<double>

P

Pepijn Kenter

Dear experts.

I have a vector<float> and want to convert that to a vector<double>. I
optimistically tried:

#include <vector>
#include <iostream>
using namespace std;

int main() {

vector<float> vFloats;
vFloats.push_back(1.0);
vFloats.push_back(2.0);
vector<double> vDoubles = static_cast<vector<double> >( vFloats);

}

But it gave the following errors:

cast.cpp: In function `int main()':
cast.cpp:10: error: no matching function for call to `std::vector<double,
std::allocator said:
/usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0
bits/stl_vector.h:220: note: candidates are: std::vector<_Tp,
_Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = double, _Alloc
= std::allocator<double>]
/usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0
bits/stl_vector.h:206: note: std::vector<_Tp,
_Alloc>::vector(size_t) [with _Tp = double, _Alloc =
std::allocator<double>]
/usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0
bits/stl_vector.h:193: note: std::vector<_Tp,
_Alloc>::vector(size_t, const _Tp&, const typename std::_Vector_base<_Tp,
_Alloc>::allocator_type&) [with _Tp = double, _Alloc =
std::allocator<double>]
/usr/local/lib/gcc/i686-pc-linux-gnu/3.4.0/../../../../include/c++/3.4.0
bits/stl_vector.h:182: note: std::vector<_Tp,
_Alloc>::vector(const typename std::_Vector_base<_Tp,
_Alloc>::allocator_type&) [with _Tp = double, _Alloc =
std::allocator<double>]

I can only derive from them that there is no casting method available. It
mentions some candidates but I wouldn't know how to use them.

Is there some simple way to cast the vector or do I just have to write my
own conversion routine?

Regards, Pepijn Kenter.
 
V

Victor Bazarov

Pepijn said:
Dear experts.

I have a vector<float> and want to convert that to a vector<double>. I
optimistically tried:

#include <vector>
#include <iostream>
using namespace std;

int main() {

vector<float> vFloats;
vFloats.push_back(1.0);
vFloats.push_back(2.0);
vector<double> vDoubles = static_cast<vector<double> >( vFloats);

}

But it gave the following errors:

[...]

I can only derive from them that there is no casting method available. It
mentions some candidates but I wouldn't know how to use them.

Is there some simple way to cast the vector or do I just have to write my
own conversion routine?

Just declare it and initialise:

vector<double> vDoubles(vFloats.begin(), vFloats.end());

Victor
 
P

Pepijn Kenter

Victor said:
Pepijn said:
Dear experts.

I have a vector<float> and want to convert that to a vector<double>. I
optimistically tried:

#include <vector>
#include <iostream>
using namespace std;

int main() {

vector<float> vFloats;
vFloats.push_back(1.0);
vFloats.push_back(2.0);
vector<double> vDoubles = static_cast<vector<double> >( vFloats);

}

But it gave the following errors:

[...]

I can only derive from them that there is no casting method available. It
mentions some candidates but I wouldn't know how to use them.

Is there some simple way to cast the vector or do I just have to write my
own conversion routine?

Just declare it and initialise:

vector<double> vDoubles(vFloats.begin(), vFloats.end());

Victor

That is what I was looking for. Thanks.

Pepijn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top