Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 11:25 PM
bluekite2000@gmail.com
Guest
 
Posts: n/a
Default how do u convert a vector<float> A to a vector<double> B

and why doesnt the standard vector have such conversion available?

  #2  
Old August 31st, 2005, 11:35 PM
Ali Çehreli
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

<bluekite2000@gmail.com> wrote in message
news:1125526444.578739.319490@o13g2000cwo.googlegr oups.com...[color=blue]
> and why doesnt the standard vector have such conversion available?[/color]

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

  #3  
Old August 31st, 2005, 11:45 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

> 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())

<bluekite2000@gmail.com> wrote in message
news:1125526444.578739.319490@o13g2000cwo.googlegr oups.com...[color=blue]
> and why doesnt the standard vector have such conversion available?[/color]

It does. See above.

-Mike



  #4  
Old August 31st, 2005, 11:45 PM
bluekite2000@gmail.com
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

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.

  #5  
Old September 1st, 2005, 12:25 AM
Ali Çehreli
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

<bluekite2000@gmail.com> wrote in message
news:1125527793.917748.118130@o13g2000cwo.googlegr oups.com...[color=blue]
> What if b is already constructed?[/color]

Then it's assignment:

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

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

  #6  
Old September 1st, 2005, 05:15 AM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

"Ali Çehreli" <acehreli@yahoo.com> wrote in message
news:df5dem$88c$1@domitilla.aioe.org...[color=blue]
> <bluekite2000@gmail.com> wrote in message
> news:1125527793.917748.118130@o13g2000cwo.googlegr oups.com...[color=green]
>> What if b is already constructed?[/color]
>
> Then it's assignment:
>
> b = vector<double>(a.begin(), a.end());[/color]

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


  #7  
Old September 1st, 2005, 01:15 PM
Clark S. Cox III
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

On 2005-08-31 18:14:04 -0400, bluekite2000@gmail.com said:
[color=blue]
> and why doesnt the standard vector have such conversion available?[/color]

#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
clarkcox3@gmail.com

  #8  
Old September 1st, 2005, 01:25 PM
Clark S. Cox III
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

On 2005-09-01 00:09:55 -0400, "Ivan Vecerina"
<INVALID_use_webform_instead@vecerina.com> said:
[color=blue]
> "Ali Çehreli" <acehreli@yahoo.com> wrote in message
> news:df5dem$88c$1@domitilla.aioe.org...[color=green]
>> <bluekite2000@gmail.com> wrote in message
>> news:1125527793.917748.118130@o13g2000cwo.googlegr oups.com...[color=darkred]
>>> What if b is already constructed?[/color]
>>
>> Then it's assignment:
>>
>> b = vector<double>(a.begin(), a.end());[/color]
>
> Actually, a probably better expression (for clarity,
> maintainability, and performance) is:
>
> b.assign( a.begin(), a.end() );[/color]

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


--
Clark S. Cox, III
clarkcox3@gmail.com

  #9  
Old September 1st, 2005, 02:05 PM
Marc Mutz
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

Clark S. Cox III wrote:
<snip>[color=blue][color=green]
>> Actually, a probably better expression (for clarity,
>> maintainability, and performance) is:
>>
>> b.assign( a.begin(), a.end() );[/color]
>
> There is no "assign" function on std::vector.
>
>[/color]

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

  #10  
Old September 1st, 2005, 02:05 PM
Marc Mutz
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

Clark S. Cox III wrote:
<snip>[color=blue]
> //Or:
> B.resize(A.size());
> std::copy(A.begin(), A.end(), B.begin());[/color]

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

Marc

  #11  
Old September 1st, 2005, 10:15 PM
Clark S. Cox III
Guest
 
Posts: n/a
Default Re: how do u convert a vector<float> A to a vector<double> B

On 2005-09-01 08:52:49 -0400, Marc Mutz
<marc@klaralvdalens-datakonsult.se> said:
[color=blue]
> Clark S. Cox III wrote:
> <snip>[color=green][color=darkred]
>>> Actually, a probably better expression (for clarity,
>>> maintainability, and performance) is:
>>>
>>> b.assign( a.begin(), a.end() );[/color]
>>
>> There is no "assign" function on std::vector.
>>
>>[/color]
>
> 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?[/color]

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
clarkcox3@gmail.com

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles