473,508 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do u convert a vector<float> A to a vector<double> B

and why doesnt the standard vector have such conversion available?

Aug 31 '05 #1
10 15604
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
and why doesnt the standard vector have such conversion available?


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

Aug 31 '05 #2
> 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?


It does. See above.

-Mike

Aug 31 '05 #3
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.

Aug 31 '05 #4
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
What if b is already constructed?
Then it's assignment:

b = vector<double>(a.begin(), a.end());
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.


There is nothing wrong with it. Just wrap it in a function and you are done:

typedef vector<float> Reals;
typedef vector<complex<float> > Complexes;

void set_reals(Complexes & complexes, Reals const & reals)
{
// some checks
if (complexes.size() != reals.size())
{
throw SomeError;
}

/* the logic here */
}

Very neat :)

More cool (and possibly more obscure) things can probably be done, but I
really don't think that it's worth it.

Ali

Aug 31 '05 #5
"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:df**********@domitilla.aioe.org...
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
What if b is already constructed?


Then it's assignment:

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


Actually, a probably better expression (for clarity,
maintainability, and performance) is:

b.assign( a.begin(), a.end() );
hth, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Sep 1 '05 #6
On 2005-08-31 18:14:04 -0400, bl**********@gmail.com said:
and why doesnt the standard vector have such conversion available?


#include <vector>
#include <algorithm> //for std::copy

....

std::vector<float> A = ...;

//Construct B with the contents of A
std::vector<double> B(A.begin(), A.end());

//Or if B is already constructed:
B.clear();
B.insert(B.begin(), A.begin(), A.end());

//Or:
B.resize(A.size());
std::copy(A.begin(), A.end(), B.begin());
--
Clark S. Cox, III
cl*******@gmail.com

Sep 1 '05 #7
On 2005-09-01 00:09:55 -0400, "Ivan Vecerina"
<IN*************************@vecerina.com> said:
"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:df**********@domitilla.aioe.org...
<bl**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
What if b is already constructed?


Then it's assignment:

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


Actually, a probably better expression (for clarity,
maintainability, and performance) is:

b.assign( a.begin(), a.end() );


There is no "assign" function on std::vector.
--
Clark S. Cox, III
cl*******@gmail.com

Sep 1 '05 #8
Clark S. Cox III wrote:
<snip>
Actually, a probably better expression (for clarity,
maintainability, and performance) is:

b.assign( a.begin(), a.end() );


There is no "assign" function on std::vector.


23.2.4.1 vectors constructors, copy, and assignment
<snip>
template <class InputIterator>
void assign(InputIterator first, InputIterator last);

Effects:
erase(begin(), end());
insert(begin(), first, last);

template <class Size, class U> void assign(Size n, const U& u = U());

Effects:
erase(begin(), end());
insert(begin(), n, t);
<snip>

Maybe your compiler lacks member template support?

Marc

Sep 1 '05 #9
Clark S. Cox III wrote:
<snip>
//Or:
B.resize(A.size());
std::copy(A.begin(), A.end(), B.begin());


b.clear();
b.reserve( a.size() );
std::copy( a.begin(), a.end(), std::back_inserter( b ) );

Marc

Sep 1 '05 #10
On 2005-09-01 08:52:49 -0400, Marc Mutz
<ma**@klaralvdalens-datakonsult.se> said:
Clark S. Cox III wrote:
<snip>
Actually, a probably better expression (for clarity,
maintainability, and performance) is:

b.assign( a.begin(), a.end() );


There is no "assign" function on std::vector.


23.2.4.1 vectors constructors, copy, and assignment
<snip>
template <class InputIterator>
void assign(InputIterator first, InputIterator last);

Effects:
erase(begin(), end());
insert(begin(), first, last);

template <class Size, class U> void assign(Size n, const U& u = U());

Effects:
erase(begin(), end());
insert(begin(), n, t);
<snip>

Maybe your compiler lacks member template support?


Well, I'll be. I can't believe I missed that.

I think I was looking at the first listing in 23.2.4 without
remembering the "Descriptions are provided here only for operations on
vector that are not described in one of these tables or for operations
where there is additional semantic information." part.
--
Clark S. Cox, III
cl*******@gmail.com

Sep 1 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11431
by: Pepijn Kenter | last post by:
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() {
18
2537
by: Active8 | last post by:
I put the bare essentials in a console app. http://home.earthlink.net/~mcolasono/tmp/degub.zip Opening output.fft and loading it into a vector<float> screws up, but input1.dat doesn't. It does...
4
2470
by: Amit | last post by:
Hi, I was wondering how to define the + and = operator for a vector of double or float and do I need to define it explicitly? does it not get defined automatically(like copy constructor) if one...
0
3008
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
9
8208
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
1
9390
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
32
3945
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
1
1793
by: mathieu | last post by:
Hi there, I am dealing with the following problem: I need to convert a std::vector<T(where T can be any interget type: char, short, ushort) by applying a linear transform (a,b), following; ...
5
13304
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.