473,387 Members | 3,684 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,387 software developers and data experts.

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
Jul 19 '05 #1
2 2587
On 29 Sep 2003 19:38:16 -0700, ma**********@yahoo.com (J. Campbell) wrote:
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?

Jul 19 '05 #2
"J. Campbell" <ma**********@yahoo.com> wrote
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.
Jul 19 '05 #3

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

Similar topics

10
by: FLChamp | last post by:
SOrry if this message is a little confused, it most probably reflects the state of the author! I have made a small program that plots the orbit of a planet in visual python using visual.vector...
10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
19
by: nick | last post by:
The attached program is fine, but I need to create vectors for each AcctExample object. I know that I can do the following: vector<AcctExample> example which makes a vector of AcctExample objects...
3
by: John Bend | last post by:
Hello. Can anyone please suggest some good tutorial links where Java Vectors and Arrays are explained and compared? I'm a proficient programmer but fairly new to Java. Whilst I understand...
7
by: Mack | last post by:
Hi Guys, In C++ , i was just wondering what is the advantage of Vectors over arrays? I saw one sample program doing pretty much same way traditional vectors do then what the use of VECTORS??or...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.