By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

Convert vector<float> to vector<double>

P: n/a
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<double> >::vector(std::vector<float, std::allocator<float>
&)'

/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.

Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Pepijn Kenter wrote:
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
Jul 22 '05 #2

P: n/a
Victor Bazarov wrote:
Pepijn Kenter wrote:
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.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.