473,748 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamic array or STL vector

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 generateVals()
{
.....
//generate some values
....
}

What I want to do is store these values as they are generated.

I thought of using something like int *data = int[num_of_vertices] but I
will not know the num_of_vertices until after the generateVals()
function has finished, and when the function has finished I wont be able
to get/store the values any more.

I thought of using a dynamic linked list (such as the STL vector) and
after the generateVals function has finished to copy the values back to
the dynamic array *data (I need this in order to pass the values into
another function that uses int * arrays as input).
Anyways, my question is. Is there a more elegant way to do this by using
a dynamic array? or a linked list is the only way?

Thank you
Vasileios

P.S.
Some people might say that this posting is a bit of topic and should be
sent to a programming newsgroup, but I am looking for an answer specific
to c++ , so if you please know something do tell me.

Jul 22 '05 #1
6 2831
Vasileios Zografos wrote in news:bq******** **@news7.svr.po l.co.uk:

[snip]
I thought of using something like int *data = int[num_of_vertices] but
I will not know the num_of_vertices until after the generateVals()
function has finished, and when the function has finished I wont be
able to get/store the values any more.

I thought of using a dynamic linked list (such as the STL vector) and
STL vector or I like to call it std::vector is not a linked list.
Its a kinda "dynamic array".

[snip]
Anyways, my question is. Is there a more elegant way to do this by
using a dynamic array? or a linked list is the only way?
[snip]
... so if you please know something do tell me.


std::vector (post a fixed standard and all known implementations )
stores it elements in a contiguous array.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
"Vasileios Zografos" <no***@nowhere. net> wrote...
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 generateVals()
{
....
//generate some values
...
}

What I want to do is store these values as they are generated.

I thought of using something like int *data = int[num_of_vertices] but I
will not know the num_of_vertices until after the generateVals()
function has finished, and when the function has finished I wont be able
to get/store the values any more.

I thought of using a dynamic linked list (such as the STL vector) and
after the generateVals function has finished to copy the values back to
the dynamic array *data (I need this in order to pass the values into
another function that uses int * arrays as input).
Anyways, my question is. Is there a more elegant way to do this by using
a dynamic array? or a linked list is the only way?


The usual approach in the case where you need some storage but
are not sure of its size ahead of time (like when reading a file
or other stream), a linked list (singly, often) is quite enough.
You can always convert (copy) it into some other kind of storage
with different traits (like into a vector or an array).

AFAICT, such way is "elegant" enough. When time comes to concern
yourself with performance, you may choose to use 'std::vector'
instead of 'std::list' even for the first, growing, storage. It
is relatively efficient in resizing when necessary, and later when
you need to pass the array to your next function, you just pass
the address of the first element. Just don't try to optimise it
before you are really sure you need it optimised. Make it work
first.

Victor

Jul 22 '05 #3
Vasileios Zografos wrote:


Anyways, my question is. Is there a more elegant way to do this by using
a dynamic array? or a linked list is the only way?


std::vector is normally implemented as a dynamic array. If
you are going to be using no more than a few 100 elements
then a dynamic array is probably ok. Once you get into a few
1000 elements or more than a dynamic array (whether it is
std::vector or something else) is a poor choice. We use what
we call paged arrays. You can think of it in terms of

std::vector< std::vector<T>* >

where the std::vectorT>* are fixed size pages containing N
elements. As the container fills up only the vector of
vector pointers has to resize.

But if you really need to deal with raw arrays of ints you
should be able to roll your own dynamic int array fairly easily.

Jul 22 '05 #4
In article <bq**********@n ews7.svr.pol.co .uk>,
Vasileios Zografos <no***@nowhere. net> wrote:

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 generateVals()
{
....
//generate some values
...
}

What I want to do is store these values as they are generated.

I thought of using something like int *data = int[num_of_vertices] but I
will not know the num_of_vertices until after the generateVals()
function has finished, and when the function has finished I wont be able
to get/store the values any more.

I thought of using a dynamic linked list (such as the STL vector) and
after the generateVals function has finished to copy the values back to
the dynamic array *data (I need this in order to pass the values into
another function that uses int * arrays as input).


Use a std::vector. It's not a linked list (there's a separate std::list
class for that), but rather a dynamic array wrapped up in a class. If you
start with a vector of zero size and append new items with push_back(),
the vector will expand as necessary in a reasonably efficient way. When
you're done, you can easily get a pointer to the beginning of the data and
pass it to the other function.

void generateVals()
{
vector<int> Values;
while (whatever)
{
int nextValue;
// ...generate nextValue here...
Values.push_bac k(Value);
}
FunctionThatUse sTheValues (&Values[0]);
}

If you want to call the other function outside of generateVals, the most
effective way to do it is probably to declare the vector in the calling
function and then pass it to generateVals() by reference:

void generateVals (vector<int>& Values);
{
while (whatever)
{
int nextValue;
// ...generate nextValue here...
Values.push_bac k(Value);
}
}

int main ()
{
// ...whatever other stuff the program does...
vector<int> Values;
generateVals (Values);
FunctionThatUse sTheValues (&Values[0]);
// ...carry on...
}

--
Jon Bell <jt*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5

"lilburne" <li******@godzi lla.net> skrev i en meddelelse
news:bq******** *****@ID-203936.news.uni-berlin.de...
Vasileios Zografos wrote:


Anyways, my question is. Is there a more elegant way to do this by using
a dynamic array? or a linked list is the only way?

std::vector is normally implemented as a dynamic array. If
you are going to be using no more than a few 100 elements
then a dynamic array is probably ok. Once you get into a few
1000 elements or more than a dynamic array (whether it is
std::vector or something else) is a poor choice.


Why??
We use what
we call paged arrays. You can think of it in terms of

std::vector< std::vector<T>* >

where the std::vectorT>* are fixed size pages containing N
elements. As the container fills up only the vector of
vector pointers has to resize.
Is this resize stuff necesarrily a problem?

But if you really need to deal with raw arrays of ints you
should be able to roll your own dynamic int array fairly easily.
You're sure? And why should they be better than std::vector?


Kind regards
Peter
Jul 22 '05 #6
> std::vector is normally implemented as a dynamic array. If
you are going to be using no more than a few 100 elements
then a dynamic array is probably ok. Once you get into a few
1000 elements or more than a dynamic array (whether it is
std::vector or something else) is a poor choice.
Why? The amortized time needed to reallocate even a very large vector is at
worst proportional to the time needed to fill the vector.
We use what
we call paged arrays. You can think of it in terms of

std::vector< std::vector<T>* >

where the std::vectorT>* are fixed size pages containing N
elements. As the container fills up only the vector of
vector pointers has to resize.


If you need such a data structure, why not use std::deque?
Jul 22 '05 #7

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

Similar topics

5
10649
by: Nils | last post by:
Hi, I want to create a dynamic array with pointer, without allocation of the memory. I tried it so: objekt **ob= new objekt; It is not working, because he will parameter for the Konstructor ob objekt. It works in the following way
4
7694
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 simple, but its just not popping into my mind at the moment. My little snippet of code is below. Basically, the studentID array is dynamic so it will fit any length of a Student's Name. What I'm trying to do is place this chunk of code into a...
5
3408
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to be done using dynamic arrays. I don't know the size of the text file ahead of time, though, so I created a class that includes a method to resize the array. Here is that class: class Data { public: Data(int initialsize);
23
7416
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
9
2110
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 size of array I need at run time without using too much memory or going over the allotted size if I choose to use this object for a different game. One idea I have is to create space * spaces = new space; then have all my accessors just convert...
19
6072
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. allocate an array of the same size as the vector and copy elements from the vector into the array */
11
7706
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
13
8947
by: kwikius | last post by:
Does anyone know what a C99 dynamic array is, and if it will be useable in C++? regards Andy Little
3
1527
by: saneman | last post by:
std::vector<intv creates a dynamic array where its possible to insert new elements without doing any preallocation. But how about a dynamic double array? I would like to have something like: int M; where a and b change be set during runtime and the size og M can vary, is that possible?
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.