Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linked list - Java style Vector

Started by Qpeg Feb 17, 2003 at 10:15 AM 7 replies 300+ views
Original Post
Qpeg
Qpeg
As I have not programmed C++ for that long, I am looking for an eqivalent of Vectors in Java. Basicaly arrays are not sufficient for my needs, I need to be able to add as few or as many objects to a list (in runtime) (which I could loop through and eg. Render whenever I wish). I could mimic a Java Vector by making a liked list, but I am unshure what sort of class would be the base for each of the nodes. In Java I would use Object, from which all classes are derived, but I can''t find a similar baseclass in C++/D3D? Is there such a class, or would I have to base all my classes of a pretty generic self-constructed class? Any ref. to already existing code for scalable arrays would be much appreciated! Best regards, Perry
Real programmers don't comment their code, it was hard to write, it should be hard to understand
Qw3r7yU10p!
Qw3r7yU10p!
good documentation for the c++ standard template library

std::list - good for memory - slow to grow in size - poor for random access
std::vector - good for most things - can use more memory if you don''t use it properly (ie shrink to fit once you''ve stopped allocating) - default choice of container as you would have to have a good design reason to choose something different.
Qpeg
Qpeg
Excellent, this is just what I was looking for, thanks to both of you!
Real programmers don't comment their code, it was hard to write, it should be hard to understand
smart_idiot
smart_idiot
Vectors keep everything in one chunk of memory. On the bright side, finding an element is fast and just requires a tiny bit of math. On the downside, making it bigger requires it getting a new, bigger chunk of memory, and copying everything over to it, unless you use reserve first.

With a list on the other hand, each element has a pointer to the next. On the bright side, adding an element requires just a small amount of memory for the object you're adding and changing a few pointers in the neighboring objects. On the down side, finding any object other than the next/previous/first/last element requires searching through every node until you find it.

Long story short: If you don't need random access or the you will be adding and removing alot of elements, use a list. If you need random access to elements and you aren't adding and removing elements very often, use a vector.

[edited by - smart_idiot on February 17, 2003 3:17:44 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Fruny
Fruny
Due to ownership semantics, putting a std::auto_ptr<type> in a container will never work. Use a boost::shared_ptr<type> instead (boost link in sig).


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.