Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamic arrays (convert vector to array)

Reply
Thread Tools

dynamic arrays (convert vector to array)

 
 
NeeZee
Guest
Posts: n/a
 
      02-04-2006
hi,
im new to this group and to the c++-language.
the problem:
i need an array-like class/data structure whose dimension can be set
dynamically. i noticed that there is a vector-class in STL, but i need
to find a way to convert that structure back to an ordinary array
because there's a function from a matrix-library that needs an array as
parameter and that i want to call.
i guess its not as easy as in java for example, where u got a method
like vector.toarray(). maybe someone knows how to do it anyway.
thanks.

 
Reply With Quote
 
 
 
 
Art Stamness
Guest
Posts: n/a
 
      02-04-2006
Its as easy just not as pretty.

vector<int> foo ;

.....
void ProcessArray ( const int * , size_t numInts ) ;

ProcessArray ( &foo[0], foo.size() );

The address of foo[0] will return a pointer to a contiguous memory
block that contains the values of foo. Vectors are guaranteed to store
there memory elements in sequential order, so this operation is legal,
and commonly used.

The above examples is (poorly ) transcribed from Effective STL , Item
16, Scott Meyers.

-Art

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-04-2006
NeeZee wrote:
> hi,
> im new to this group and to the c++-language.
> the problem:
> i need an array-like class/data structure whose dimension can be set
> dynamically. i noticed that there is a vector-class in STL, but i need
> to find a way to convert that structure back to an ordinary array
> because there's a function from a matrix-library that needs an array as
> parameter and that i want to call.
> i guess its not as easy as in java for example, where u got a method
> like vector.toarray(). maybe someone knows how to do it anyway.
> thanks.
>

Please avoid txt speak such as 'u' in postings.

While not mandated by the standard, every vector implementation I have
seen uses contiguous memory (to do anything else would have a
detrimental effect on the [] operator performance), so you can pass the
address of vector[0] to functions that expect an array.

--
Ian Collins.
 
Reply With Quote
 
NeeZee
Guest
Posts: n/a
 
      02-04-2006
Thx.
tried it that way,
had to modify it a bit, cos im using double values:

vector<double> foo ;

void ProcessArray ( const double * , size_t numDoubles ) ;

ProcessArray ( &foo[0], foo.size() );

when i call the function: Coord_Mat_double
r(matSize,matSize,nonZeros,foo,row,col);
which is part of the sparse-matrix-package im using and expects a
double-array as 4th parameter the compiler still says i called it using
a double-vector as 4th parameter.
Any idea why? Isn't the size_t type suitable for double-values?

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      02-04-2006
Ian Collins wrote:

> NeeZee wrote:
>> hi,
>> im new to this group and to the c++-language.
>> the problem:
>> i need an array-like class/data structure whose dimension can be set
>> dynamically. i noticed that there is a vector-class in STL, but i need
>> to find a way to convert that structure back to an ordinary array
>> because there's a function from a matrix-library that needs an array as
>> parameter and that i want to call.
>> i guess its not as easy as in java for example, where u got a method
>> like vector.toarray(). maybe someone knows how to do it anyway.
>> thanks.
>>

> Please avoid txt speak such as 'u' in postings.
>
> While not mandated by the standard, every vector implementation I have
> seen uses contiguous memory (to do anything else would have a
> detrimental effect on the [] operator performance), so you can pass the
> address of vector[0] to functions that expect an array.


And it gets even better: as of 2003, contiguity for std::vector<T> is
mandated by the standard as long as T is not bool. [See 23.2.4./1]


Best

Kai-Uwe Bux
 
Reply With Quote
 
NeeZee
Guest
Posts: n/a
 
      02-04-2006
Thanx Ian.
It worked that way.

 
Reply With Quote
 
Art Stamness
Guest
Posts: n/a
 
      02-04-2006
please print out the actual error from the compiler, and the line of
code that it is saying is causing the error.

There should be no way that " &foo[0] " is interpretted any other way
than a pointer to const X where X it seems is double in your case.
There may be other problems.

-Art

 
Reply With Quote
 
Aaron Graham
Guest
Posts: n/a
 
      02-04-2006
[...]
> While not mandated by the standard, every vector implementation I have
> seen uses contiguous memory (to do anything else would have a
> detrimental effect on the [] operator performance), so you can pass the
> address of vector[0] to functions that expect an array.


See the following:

http://www.open-std.org/jtc1/sc22/wg...efects.html#69

It wasn't stated by the original draft of the standard, but it was
intended. The fact that it wasn't stated explicitly was noticed and a
defect report/technical corrigenda was issued. A standards-conforming
vector implementation must be contiguous.

Aaron

 
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
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments