 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
J. Campbell Guest
|
Posted: Tue Sep 30, 2003 2:38 am Post subject: swaping vectors & arrays |
|
|
I have a class that contains an array of integers that hold the state
of my 'system'. The system must be updated periodically. I need to
update the whole system at once, at which point the system is in a new
state. The update process depends upon the entire system state prior
to updating...that is, a single array cannot be serially updated, with
the results stored back into itself.
Just to give an indication of what I'm doing, the system size is circa
6000 bytes, and in the course of running the progam, the system may be
updated from 50,000 to many millions of times. I don't know the
system size until runtime, but once the system size is set, it never
needs to be changed.
What I've been doing is creating 2 pointers in the class definition:
int* array;
int* dup_array;
then, in the class constructor, I create 2 arrays using:
array = new int [a_size];
dup_array = new int [a_size];
Then, when I update the system, I update it into dup_array. After
updating the system, I swap the pointers rather than the contents of
the arrays. So that after the swap, the current system state is
located in array, and the preceeding system state is located in
dup_array.
The question...for an application of this type, is there any advantage
to using c++ vectors rather than the c-style array? It seems that for
this application it would actually complicate things, since I would
need 2 vectors and 2 array pointers rather than simply 2 array
pointers.
I do realize that with vectors I wouldn't need to free the memory in
the class destructor, which is an obvious advantage.
Thanks
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Tue Sep 30, 2003 3:07 am Post subject: Re: swaping vectors & arrays |
|
|
On 29 Sep 2003 19:38:16 -0700, [email]mango_maniac (AT) yahoo (DOT) com[/email] (J. Campbell) wrote:
| Quote: | I have a class that contains an array of integers that hold the state
of my 'system'. The system must be updated periodically. I need to
update the whole system at once, at which point the system is in a new
state. The update process depends upon the entire system state prior
to updating...that is, a single array cannot be serially updated, with
the results stored back into itself.
Just to give an indication of what I'm doing, the system size is circa
6000 bytes, and in the course of running the progam, the system may be
updated from 50,000 to many millions of times. I don't know the
system size until runtime, but once the system size is set, it never
needs to be changed.
What I've been doing is creating 2 pointers in the class definition:
int* array;
int* dup_array;
then, in the class constructor, I create 2 arrays using:
array = new int [a_size];
dup_array = new int [a_size];
Then, when I update the system, I update it into dup_array. After
updating the system, I swap the pointers rather than the contents of
the arrays. So that after the swap, the current system state is
located in array, and the preceeding system state is located in
dup_array.
The question...for an application of this type, is there any advantage
to using c++ vectors rather than the c-style array? It seems that for
this application it would actually complicate things, since I would
need 2 vectors and 2 array pointers rather than simply 2 array
pointers.
I do realize that with vectors I wouldn't need to free the memory in
the class destructor, which is an obvious advantage.
|
Uh, have you checked out std::swap? Not to mention the particulars of
the std::vector class?
|
|
| Back to top |
|
 |
Buster Guest
|
Posted: Tue Sep 30, 2003 9:20 am Post subject: Re: swaping vectors & arrays |
|
|
"J. Campbell" <mango_maniac (AT) yahoo (DOT) com> wrote
| Quote: | The question...for an application of this type, is there any advantage
to using c++ vectors rather than the c-style array? It seems that for
this application it would actually complicate things, since I would
need 2 vectors and 2 array pointers rather than simply 2 array
pointers.
|
Why would you need 2 pointers as well as the 2 vectors?
Have you considered the swap () member function of vector <>?
Regards,
Buster.
|
|
| Back to top |
|
 |
|
|
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
|
|