473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

dynamic arrays (convert vector to array)

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.

Feb 4 '06 #1
7 41086
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

Feb 4 '06 #2
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.
Feb 4 '06 #3
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?

Feb 4 '06 #4
Thanx Ian.
It worked that way.

Feb 4 '06 #5
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

Feb 4 '06 #6
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
Feb 4 '06 #7
[...]
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

Feb 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rave | last post by:
Is there a better way to do this in C++ ? linkedlist * sort_array; for(int i=0; i<MAX; i++) { sort_array = new (linkedlist); sort_array = new (linkedlist); }
3
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
6
by: Vasileios Zografos | last post by:
Hello, I have a function that generates some values (e.g. vertices in 2d space) the number of which I dont know. So, it could generate 20 vertices, 100 vertices, or even 1 vertex. void...
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
5
ILtech
by: ILtech | last post by:
"3. (5 points) Write a method for a class that uses an array For the Vector class given below, write a method that computes the dot product of the Vector "this" object with the Vector object...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.