Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how do u convert a vector<float> A to a vector<double> B

Reply
Thread Tools

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

 
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      08-31-2005
and why doesnt the standard vector have such conversion available?

 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      08-31-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-31-2005
> 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())

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> and why doesnt the standard vector have such conversion available?


It does. See above.

-Mike



 
Reply With Quote
 
bluekite2000@gmail.com
Guest
Posts: n/a
 
      08-31-2005
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.

 
Reply With Quote
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      08-31-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      09-01-2005
"Ali Çehreli" <(E-Mail Removed)> wrote in message
news:df5dem$88c$(E-Mail Removed)...
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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


 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-01-2005
On 2005-08-31 18:14:04 -0400, http://www.velocityreviews.com/forums/(E-Mail Removed) 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
(E-Mail Removed)

 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      09-01-2005
On 2005-09-01 00:09:55 -0400, "Ivan Vecerina"
<(E-Mail Removed)> said:

> "Ali Çehreli" <(E-Mail Removed)> wrote in message
> news:df5dem$88c$(E-Mail Removed)...
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) 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
(E-Mail Removed)

 
Reply With Quote
 
Marc Mutz
Guest
Posts: n/a
 
      09-01-2005
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

 
Reply With Quote
 
Marc Mutz
Guest
Posts: n/a
 
      09-01-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
IsNumeric: Convert.ToInt32 vs. Convert.ToInt64 sck10 ASP .Net 4 09-03-2006 09:40 PM
To convert to J2SE 6 or not to convert, that is the question... Jaap Java 4 07-10-2006 09:03 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Do I need to Convert with Convert.ToInt32(session("myNumber")) ? Andreas Klemt ASP .Net 1 07-23-2003 02:59 PM



Advertisments