![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
arrays
Is there any way to resize an array (of the same name)? Or do I "have to use a linked list"?
In java I could just do this. Piece tetPiece[] = new Piece [1]; tetPiece = new Piece [x];
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Arrays in C/C++ are just contiguous collections of data. The are not individual objects in the sense that they are in Java. If you want to adjust your array size, allocate it dynamically, then you may do so. If it can't be upsized, a larger area will be allocated and the contents moved. A linked list is not necessaryily a good substitute for an array. Since you're using C++, investigate the STL (vectors, deques, etc.), as these are classes designed to do what you probably need to do. Resizing and all that good hooey is taken care of for you.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#4 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>In java I could just do this.
That doesn't resize the array, it just points your reference to a completely different array. Tell me, what happens to the data in your old array now that you no longer have a reference to it? :p So I guess you can't just do that, or you'll end up writing really bad code.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#5 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,107
Rep Power: 5
![]() |
Before you pick which you want to go with, you should carefully consider how your data will be accessed, as different data structures are optimized for different sets of operations. For example, adding to (the end of) a linked list is generally done in constant time, but adding or removing elements at arbitrary positions is done in linear time, and sorting is abysmally slow. If you're not familiar with the term 'big O notation', look it up; it's a way of measuring the approximate mathematical complexity of different algorithms with respect to given operations.
Quote:
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
i'd check out vectors.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|