473,698 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2606
On 29 Sep 2003 19:38:16 -0700, ma**********@ya hoo.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**********@y ahoo.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
2490
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 for all values. If i run the program it is clear that the orbit is non-keplerian as the planets gradually moves away from the sun. I am pretty sure the orbit should be keplerian so i guess there must be some source of error in the program. I am...
10
15828
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, so I'd like to use an STL container class template such as valarray or vector to represent 1D arrays, and valarrays or vectors of valarrays or vectors to represent 2D arrays. As I said the sizes of the arrays in my intended application are...
12
9189
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 statically-declared arrays? I'll also be looking at the other STL containers later... Also, I'd appreciate any comments anyone has on the suitability of the little program below for comparing vectors and arrays. One obvious shortcoming is that it uses...
5
2313
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 seem to be (in my eyes at least) are glorified Arrays. - Now I know there's a bit more difference, but what exactly are the advantages of Arrays over Vectors (if any)? Oh, and please keep it in mind I am a real beginner in C++ but can
3
3240
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 there anyway where I can accomodate both the vector types into a single set. Something like a set<vector<void*>, my_compare func >. Right now, I am having them as two different set dayatypes.
19
1941
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 called example. Now, how do I activate the methods for each object? Also, why are we replacing templates with vectors? (That was what the assignment required. I don't get why we do this). ACCTEXAMPLE.CPP #include <conio.h>
3
7065
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 arrays I don't fully understand Vectors and when to use them. Many thanks.
7
10504
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 what is their advantage?? Regards, Mack
1
2060
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 function that handles vectors of vectors of <typename T(which could be anything from int to vectors of something else). As well, I have specialized/overloaded functions to handle the single-level vectors of data (e.g. vector<string>). So the templated...
0
8603
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,...
1
8893
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
7723
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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.