Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > conversion double* to vector<double> and vice versa

Reply
Thread Tools

conversion double* to vector<double> and vice versa

 
 
J.M.
Guest
Posts: n/a
 
      03-08-2007
I am trying to use a software package in my program, the problem being
different methods are used to store vectors. I have "my" vectors stored as
vector<double> from the STL and need to pass a double* and the dimension of
the vector to the software package. On return, I get a double* plus
dimension, but need vector<double>. Is there anyway that this can be
achieved *without* copying the data, as this is a very expensive option?

Thanks in advance for any help.

Jan
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-08-2007
"J.M." <(E-Mail Removed)> wrote in message
news:esoj3j$v9i$(E-Mail Removed)-karlsruhe.de...
>I am trying to use a software package in my program, the problem being
> different methods are used to store vectors. I have "my" vectors stored as
> vector<double> from the STL and need to pass a double* and the dimension
> of
> the vector to the software package. On return, I get a double* plus
> dimension, but need vector<double>. Is there anyway that this can be
> achieved *without* copying the data, as this is a very expensive option?
>
> Thanks in advance for any help.
>
> Jan


Going into the functions, yes. Vectors are guaranteed to store their data
contiguously and so can be used where arrays are expected. That is, you can
pass &MyVector[0] and MyVector.size(). &MyVector.front() is also used
sometimes.

Coming out of the functions, I don't see how. You are given a pointer to an
array of doubles. The only way to get this into a vector, as far as I know,
is to copy the data. There may be some constructor for vector that takes a
pointer to data and a length, I just don't know (but tend to doubt it?).


 
Reply With Quote
 
 
 
 
J.M.
Guest
Posts: n/a
 
      03-08-2007
Jim Langston schrieb:

> "J.M." <(E-Mail Removed)> wrote in message
> news:esoj3j$v9i$(E-Mail Removed)-karlsruhe.de...
>>I am trying to use a software package in my program, the problem being
>> different methods are used to store vectors. I have "my" vectors stored
>> as vector<double> from the STL and need to pass a double* and the
>> dimension of
>> the vector to the software package. On return, I get a double* plus
>> dimension, but need vector<double>. Is there anyway that this can be
>> achieved *without* copying the data, as this is a very expensive option?
>>
>> Thanks in advance for any help.
>>
>> Jan

>
> Going into the functions, yes. Vectors are guaranteed to store their data
> contiguously and so can be used where arrays are expected. That is, you
> can
> pass &MyVector[0] and MyVector.size(). &MyVector.front() is also used
> sometimes.
>
> Coming out of the functions, I don't see how. You are given a pointer to
> an
> array of doubles. The only way to get this into a vector, as far as I
> know,
> is to copy the data. There may be some constructor for vector that takes
> a pointer to data and a length, I just don't know (but tend to doubt it?).


Thanks, but that is precisely what I needed . Actually, the software
package takes as argument double* and returns the result in the same array
by overwriting, so in this case, I do not actually need to convert back to
vector<double>... I just hope I am not doing something dangerous... Thanks.

Jan

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-08-2007
"J.M." <(E-Mail Removed)> wrote in message
news:esomh2$2ig$(E-Mail Removed)-karlsruhe.de...
> Jim Langston schrieb:
>
>> "J.M." <(E-Mail Removed)> wrote in message
>> news:esoj3j$v9i$(E-Mail Removed)-karlsruhe.de...
>>>I am trying to use a software package in my program, the problem being
>>> different methods are used to store vectors. I have "my" vectors stored
>>> as vector<double> from the STL and need to pass a double* and the
>>> dimension of
>>> the vector to the software package. On return, I get a double* plus
>>> dimension, but need vector<double>. Is there anyway that this can be
>>> achieved *without* copying the data, as this is a very expensive option?
>>>
>>> Thanks in advance for any help.
>>>
>>> Jan

>>
>> Going into the functions, yes. Vectors are guaranteed to store their
>> data
>> contiguously and so can be used where arrays are expected. That is, you
>> can
>> pass &MyVector[0] and MyVector.size(). &MyVector.front() is also used
>> sometimes.
>>
>> Coming out of the functions, I don't see how. You are given a pointer to
>> an
>> array of doubles. The only way to get this into a vector, as far as I
>> know,
>> is to copy the data. There may be some constructor for vector that takes
>> a pointer to data and a length, I just don't know (but tend to doubt
>> it?).

>
> Thanks, but that is precisely what I needed . Actually, the software
> package takes as argument double* and returns the result in the same array
> by overwriting, so in this case, I do not actually need to convert back to
> vector<double>... I just hope I am not doing something dangerous...
> Thanks.
>
> Jan


Nothing dangerous, just make sure that the software package doesn't try to
resize the array itself. And make sure it's sized proper when you go in.
But as you say it accepts a size parameter so I suspect it respects the
limits.


 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      03-08-2007
Jim Langston wrote:
> "J.M." <(E-Mail Removed)> wrote in message
> news:esoj3j$v9i$(E-Mail Removed)-karlsruhe.de...
>> I am trying to use a software package in my program, the problem being
>> different methods are used to store vectors. I have "my" vectors stored as
>> vector<double> from the STL and need to pass a double* and the dimension
>> of
>> the vector to the software package. On return, I get a double* plus
>> dimension, but need vector<double>. Is there anyway that this can be
>> achieved *without* copying the data, as this is a very expensive option?
>>
>> Thanks in advance for any help.
>>
>> Jan

>
> Going into the functions, yes. Vectors are guaranteed to store their data
> contiguously and so can be used where arrays are expected. That is, you can
> pass &MyVector[0] and MyVector.size(). &MyVector.front() is also used
> sometimes.
>
> Coming out of the functions, I don't see how. You are given a pointer to an
> array of doubles. The only way to get this into a vector, as far as I know,
> is to copy the data. There may be some constructor for vector that takes a
> pointer to data and a length, I just don't know (but tend to doubt it?).


As a side note, on return from the function, would it be OK to
resize the vector appropriately, and then writing to &vec[0]
(passing this pointer to the function). The memory in the vector
is guaranteed to be continuous and if you resize it in advance,
then would this work? I think so.

- J.
 
Reply With Quote
 
J.M.
Guest
Posts: n/a
 
      03-08-2007
Jim Langston schrieb:

> "J.M." <(E-Mail Removed)> wrote in message
> news:esomh2$2ig$(E-Mail Removed)-karlsruhe.de...
>> Jim Langston schrieb:
>>
>>> "J.M." <(E-Mail Removed)> wrote in message
>>> news:esoj3j$v9i$(E-Mail Removed)-karlsruhe.de...
>>>>I am trying to use a software package in my program, the problem being
>>>> different methods are used to store vectors. I have "my" vectors stored
>>>> as vector<double> from the STL and need to pass a double* and the
>>>> dimension of
>>>> the vector to the software package. On return, I get a double* plus
>>>> dimension, but need vector<double>. Is there anyway that this can be
>>>> achieved *without* copying the data, as this is a very expensive
>>>> option?
>>>>
>>>> Thanks in advance for any help.
>>>>
>>>> Jan
>>>
>>> Going into the functions, yes. Vectors are guaranteed to store their
>>> data
>>> contiguously and so can be used where arrays are expected. That is, you
>>> can
>>> pass &MyVector[0] and MyVector.size(). &MyVector.front() is also used
>>> sometimes.
>>>
>>> Coming out of the functions, I don't see how. You are given a pointer
>>> to an
>>> array of doubles. The only way to get this into a vector, as far as I
>>> know,
>>> is to copy the data. There may be some constructor for vector that
>>> takes a pointer to data and a length, I just don't know (but tend to
>>> doubt it?).

>>
>> Thanks, but that is precisely what I needed . Actually, the software
>> package takes as argument double* and returns the result in the same
>> array by overwriting, so in this case, I do not actually need to convert
>> back to vector<double>... I just hope I am not doing something
>> dangerous... Thanks.
>>
>> Jan

>
> Nothing dangerous, just make sure that the software package doesn't try to
> resize the array itself.


Yup, I figured that this would be a bad idea... But I know for a fact that
that it does not (or can be told not to do so). It seems to work fine now,
although not much testing has been done. Thanks for your help.

Jan

> And make sure it's sized proper when you go in.
> But as you say it accepts a size parameter so I suspect it respects the
> limits.


 
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
converting int and short to byte array and vice versa carmen Java 4 01-12-2010 05:00 PM
Safe conversion from floating real to integer (and vice versa) rz0 C Programming 4 02-01-2006 05:27 PM
Howto display an ASP.net datagrid rows as columns and vice versa? Jimmy ASP .Net 1 06-14-2005 03:30 PM
How to convert '1' into '-1' and vice versa? - Java 8 06-11-2005 09:28 AM
Java Object to XML and vice versa Sam Hwang Java 5 04-25-2005 12:22 PM



Advertisments