C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

simple thing: from vector<complex<double> > to complex<doub

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
archi
Guest





PostPosted: Thu Sep 25, 2003 1:53 pm    Post subject: simple thing: from vector<complex<double> > to complex<doub Reply with quote



I've the following problem:

I want to change from vector<complex to
complex<double> *.

I tried:

vector<complex in(N);
// vector is filled
complex<double> *inn;
// ... malloc ...

// here I want to have the data from "in" in "inn"
inn = reinterpret_cast(complex<double> *)(&in);

but it did not work.
How to correct this or how to find an efficient way to transfer from
vector<complex to complex<double> *.

Archi.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Early Ehlinger
Guest





PostPosted: Thu Sep 25, 2003 8:51 pm    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote



"archi" <archimg77 (AT) yahoo (DOT) com> wrote:
Quote:
// here I want to have the data from "in" in "inn"
inn = reinterpret_cast(complex<double> *)(&in);

reinterpret_cast is a very broad, jagged sword. Any time you're about to
use it, you need to ask yourself two questions:

1: "is this really my last resort?"
2: "do I mind if this code is tied to this specific platform?"

What you're telling the compiler to do here is take the address of an
OBJECT, "in", and convert it to the address of a totally unrelated object.
"in" is a CONTAINER, while complex<double> is a CONTAINEE (in this case).

Quote:
but it did not work.
How to correct this or how to find an efficient way to transfer from
vector<complex to complex<double> *.

complex<double>* inn = &*in.begin( );

This calls vector< complex< double > >::begin(), which returns an iterator.
The * is a call to the iterators' operator*(), which returns a reference to
the item that the iterator "points" to. In this case, that's the first item
in "in", because the iterator has not been modified. Next, the & takes the
address of the object that the reference is referring to.

std::vector guarantees that its items will be laid out contiguously.

Note, though, that there is a reason that taking the address of an item in a
vector is so unusually funky to do. Have you considered the possibility of
working with iterators instead?

--
Best Regards,
- Early Ehlinger - CEO - www.ResPower.com - 866-737-7697
- 500+ GHz Self-Service Super-Computer from $0.50/GHz*Hr
- (yes, five hundred gigahertz. point-five terahertz.)



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Sep 25, 2003 9:21 pm    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote



archi wrote:
Quote:
How to correct this or how to find an efficient way to transfer from
vector<complex to complex<double> *.

The elements of a vector are in contiguous memory as described in the
FAQ([url]http://www.parashift.com)[/url]. Just remember that the memory is still owned
by the vector.

cheers

Uli



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tonci Tomic
Guest





PostPosted: Thu Sep 25, 2003 9:22 pm    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote


"archi" <archimg77 (AT) yahoo (DOT) com> wrote

Quote:
I want to change from vector<complex to
complex<double> *.
vector<complex in(N);
// vector is filled
complex<double> *inn;

try:
inn = in.begin();

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Daniel
Guest





PostPosted: Thu Sep 25, 2003 11:37 pm    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

archi wrote:
Quote:
I've the following problem:

I want to change from vector<complex to
complex<double> *.

I tried:

vector<complex in(N);
// vector is filled
complex<double> *inn;
// ... malloc ...

// here I want to have the data from "in" in "inn"
inn = reinterpret_cast(complex<double> *)(&in);

but it did not work.
How to correct this or how to find an efficient way to transfer from
vector<complex to complex<double> *.

inn = &in.front();

-cd


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jon Heiner
Guest





PostPosted: Fri Sep 26, 2003 10:34 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

hi archi-

correct me if i'm wrong, but what you seem to be thinking is this:

int a[10];
int * pA = a;

and use 'pA' to access the data in 'a'. the bad news is that doing
this with any of the STL containers is non-sensical at best. the
good news is that there is definitely a better way to do things.

first, the STL containers ( like vector ) do not necessarily store
their data in contiguous memory, or even anywhere local to the
address of the container. the array example above takes advantage
of both of these features of regular C-arrays. taking the address
of a container tells you nothing about the data in the container
which is appropriately abstracted. ( see Allocators )

what you probably want is an iterator for the vector in order to
traverse the objects in the vector. look in any standard C++
textbook for "iterator". they work like:

vector<complex in(N);
for( vector<complex::iterator i = in.begin(); i != in.end(); ++i )
{
func( *i ); // where func takes a reference to a complex<double>
}

if i'm misunderstanding this, please give more information, but
this is the best i can interpret from your posting.

-jon

"archi" <archimg77 (AT) yahoo (DOT) com> wrote

Quote:
I've the following problem:

I want to change from vector<complex to
complex<double> *.

I tried:

vector<complex in(N);
// vector is filled
complex<double> *inn;
// ... malloc ...

// here I want to have the data from "in" in "inn"
inn = reinterpret_cast(complex<double> *)(&in);

but it did not work.
How to correct this or how to find an efficient way to transfer from
vector<complex to complex<double> *.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
P.J. Plauger
Guest





PostPosted: Fri Sep 26, 2003 10:39 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

"Tonci Tomic" <tonci.tomic (AT) mireo (DOT) hr> wrote


Quote:
"archi" <archimg77 (AT) yahoo (DOT) com> wrote in message
news:b1160da0.0309250236.2638abeb (AT) posting (DOT) google.com...
I want to change from vector<complex to
complex<double> *.
vector<complex in(N);
// vector is filled
complex<double> *inn;

try:
inn = in.begin();

You can try, but it might fail. There's no guarantee that in.begin()
returns a pointer, or an iterator convertible to a pointer. The
two portable constructs are:

inn = &*in.begin();
inn = &in[0];

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ian
Guest





PostPosted: Fri Sep 26, 2003 10:42 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

In article <bkuvrd$qk0$1 (AT) garrison (DOT) globalnet.hr>, Tonci Tomic
<tonci.tomic (AT) mireo (DOT) hr> writes
Quote:

"archi" <archimg77 (AT) yahoo (DOT) com> wrote in message
news:b1160da0.0309250236.2638abeb (AT) posting (DOT) google.com...
I want to change from vector<complex to
complex<double> *.
try:
inn = in.begin();


I don't think this is correct in general as it assumes that
vector<T>::iterator is the same as (or at least convertible to) T*.
Whilst this might be true for some library implementations, it should
not be relied upon.

I believe the correct approach would be:

inn = &in[0];

This is fine since the underlying data in std::vector is guaranteed by
the standard to be contiguous.

--
Ian


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Sat Sep 27, 2003 12:41 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

"Tonci Tomic" <tonci.tomic (AT) mireo (DOT) hr> wrote


Quote:
inn = in.begin();

in.begin returns a vector<complex::iterator, which may not be the
same as complex<double> *. It is so on my implementation (Borland 6), but
not on many others in common use.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Sat Sep 27, 2003 12:45 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

"Early Ehlinger" <early (AT) respower (DOT) com> wrote in message

Quote:
complex<double>* inn = &*in.begin( );

Generally, we should assume that in may contain zero elements. Thus

complex<double>* inn = in.size() ? &*in.begin( ) : NULL;

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
archi
Guest





PostPosted: Sat Sep 27, 2003 1:03 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

"Carl Daniel" <cpdaniel (AT) nospam (DOT) mvps.org> wrote

Quote:
archi wrote:
I've the following problem:

I want to change from vector<complex to
complex<double> *.

I tried:

vector<complex in(N);
// vector is filled
complex<double> *inn;
// ... malloc ...

// here I want to have the data from "in" in "inn"
inn = reinterpret_cast(complex<double> *)(&in);

but it did not work.
How to correct this or how to find an efficient way to transfer from
vector<complex to complex<double> *.



Quote:
inn = &in.front();

Thanks.

p.s.: inn=in.begin(); returns iterator on first element, so it doesn't work

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Sat Sep 27, 2003 1:22 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote



Tonci Tomic schrieb:

Quote:
"archi" <archimg77 (AT) yahoo (DOT) com> wrote in message
news:b1160da0.0309250236.2638abeb (AT) posting (DOT) google.com...
I want to change from vector<complex to
complex<double> *.
vector<complex in(N);
// vector is filled
complex<double> *inn;

try:
inn = in.begin();

Not guaranteed to work, because the iterators don't have to be plain
pointers.

Change it to

inn = &in[0].


regards,

Thomas

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Sat Sep 27, 2003 2:10 pm    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

On 26 Sep 2003 06:39:06 -0400, "P.J. Plauger" <pjp (AT) dinkumware (DOT) com>
wrote:

Quote:
The two portable constructs are:

inn = &*in.begin();
inn = &in[0];

Is there a problem with
inn = &in.front();
that I am missing?

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
P.J. Plauger
Guest





PostPosted: Sun Sep 28, 2003 1:32 am    Post subject: Re: simple thing: from vector<complex<double> > to complex< Reply with quote

"John Potter" <jpotter (AT) falcon (DOT) lhup.edu> wrote


Quote:
On 26 Sep 2003 06:39:06 -0400, "P.J. Plauger" wrote:

The two portable constructs are:

inn = &*in.begin();
inn = &in[0];

Is there a problem with
inn = &in.front();
that I am missing?

Only that front() has every right to fail disastrously if the vector
is empty. Some might argue that the two forms above can also fail
for an empty vector, but they at least require an argument; and they
happen to work right in all cases I've encountered.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.