I thought I might start learning some more C++, since it's changed a lot since I last used it (pre-STL).
I want a dynamic storage structure to hold enemies and projectiles, and it looks like Lists or Vectors are what I'm after.
What are the essential differences between the two, and which would be more suitable for such a task?
List is a linked list. It's good for when you need to remove items from any place in the list (ie test each item in the list and remove those that fail for example). Vectors are good at random access (which lists are not) and removal at end. However, you could use a vector and copy-on-delete (that is, copy the last element over the one you want to remove and pop the last element).
Thanks, I think a list suits my needs better, since I won't ever be accessing members by index, and items will be removed from all over.
I had never heard of programming lists in my life until recently. Are they in the native C++ or Allegro or another header file? Maybe I'll google them to learn more about them though. It sounds like an array, but I guess not all of the same type like a vector.
Felix-The-Ghost:
The C++ Standard Template Library will come with any modern C++ compiler.
I'd suggest looking into a book on data structures. You'll learn all sorts of useful things. The book I used in my class is called Classic Data Structures in Java, though the author also has a book titled Classic Data Structures in C++, which may be more up your alley.
Felix: Please, please get a C++ reference. Or any kind of C++ book. There are even good free ones available. Seriously. I believe I'm speaking for everyone here.
...but I have one...and it mentions lists nowhere in it...
I'm just asking my questions out loud that others would probably google.
I'm not that bad I just didn't know which library used it, and Harry told me.
Oh, alright. See I haven't done much with container class templates.
forehead slap
And here I was working on my own double linked list class. I guess it was good experience.
And here I was working on my own double linked list class. I guess it was good experience.
It is. I would suggest that everyone write their own implementations before using the STL, just because it is excellent experience and you will get a much better idea of the concept. This is useful for knowing which structure will be most efficient for a given situation.
It is. I would suggest that everyone write their own implementations before using the STL, just because it is excellent experience and you will get a much better idea of the concept. This is useful for knowing which structure will be most efficient for a given situation.
I would suggest the exact opposite. The entire point of the STL (and indeed C++ as a whole) is to be able to use code without having to understand every detail of how it works. It's an utter waste of time spending days trying to implement your own containers when you could be doing something useful with proven, reliable containers that already exist.
If you get to the point where std::list (or whatever you're using) is inadequate for your needs, then start worrying about implementing custom containers.
In any case, using STL's list instead of my own linked list implementation has definitely reduced the ammount of code.
If you get to the point where std::list (or whatever you're using) is inadequate for your needs, then start worrying about implementing custom containers.
Yeah, I'll second that. If the only thing you care about is functionality, then look for things that are already made. STL is very big and does a lot of stuff already, so looking through a reference will help you. It only takes a few minutes of directed internet searches, and it will save you a lot of time. Also, you'll know exactly what can't be done with STL (e.g. it has no double-ended vectors), but you can't know that if you don't know what is already there.
Speedo's link should be sufficient for most uses, so no need to buy anything I'd think.
would suggest the exact opposite.
I think its extremely important for a programmer to know the basics. That includes the basic data containers, and algorithms. Its hard to know how to fix a problem if you don't know whats actually going on.
I will agree though that black box containers are very useful.
Look in my sig for a link to a nice STL reference (includes containers). You can download a copy for offline use from there as well.
I would suggest the exact opposite. The entire point of the STL (and indeed C++ as a whole) is to be able to use code without having to understand every detail of how it works. It's an utter waste of time spending days trying to implement your own containers when you could be doing something useful with proven, reliable containers that already exist.
If you get to the point where std::list (or whatever you're using) is inadequate for your needs, then start worrying about implementing custom containers.
Implementing a basic linked list only takes a few hours (if that), and is well worth it. Of course, you won't actually be USING it. But it's nice to have a general idea of what's going on behind the scenes...
That being said... STL is great. Use it.
is to be able to use code without having to understand every detail of how it works
Funny, that's what Java programmers say...
STL is a huge time saver, but there are a couple of 'gotcha's' like copy constructors. Chris Barry's tutorials were really good, whatever happened to 'em?
http://www.allegro.cc/files/attachment/596901
Awesome flow chart. I need to keep a copy of that.
...but I have one...and it mentions lists nowhere in it...
Probably because STL isn't seen as part of the C++ language, traditionally. The language works without STL, just like (or even more than) C works without libc - but since both languages are practically feature-less by themselves, the standard libraries are so important that they have become a de-facto part of the language itself.
Also, the STL is such a vast topic by itself that it's worth a book of its own - but adding the basics to a C++ book is still a good idea IMO.
That flow chart is pretty good, gnolam. There is one link I'm a bit hazy about though: why are deques better than vectors when "size will vary widely"?
I would suggest the exact opposite. The entire point of the STL (and indeed C++ as a whole) is to be able to use code without having to understand every detail of how it works. It's an utter waste of time spending days trying to implement your own containers when you could be doing something useful with proven, reliable containers that already exist.
I don't think that being able to program stuff without understanding it is point of STL or C++.
I think that the best way to learn which STL data structure is best for which situation is to learn how those data types are implemented. Creating the data structures oneself is a good way to learn how they work and thus understand how each of them will perform in any given situation.
I don't think that being able to program stuff without understanding it is point of STL or C++.
So, when someone asks for a 2d graphics library, do you recommend allegro to them and then say "but write your own library first, it's good experience and will help you understand the concepts behind it"? Of course not - the idea is pretty silly.
STL is not allegro. It's a template library filled with well written, common, algorithms and data structures. The basic principle behind them is very useful, and is needed when you want to write more sophisticated data structures anyway (trees and graphs for example).
STL is not allegro. It's a template library filled with well written, common, algorithms and data structures. The basic principle behind them is very useful, and is needed when you want to write more sophisticated data structures anyway (trees and graphs for example).
And that's something you learn when you need to.
I'd argue that if you need to learn it eventually anyway, you might as well do it as soon as possible and get more out of STL (a fair bit of questions regarding lists, iterators etc posted here could have been avoided if they knew how a linked list works). It's not weeks of studying for gods sake... It takes maybe 5 hours to build a decent linked list from scratch if you don't know anything about it - and that time includes googling for help.
It's also a generic knowledge applicable in most languages and programming situations... STL is only in C++ (though most modern languages does have an equivalent).
And that's something you learn when you need to.
Data structures and algorithms are something a programmer should learn in CLASS before they even start coding.
So, when someone asks for a 2d graphics library, do you recommend allegro to them and then say "but write your own library first, it's good experience and will help you understand the concepts behind it"? Of course not - the idea is pretty silly.
You're right. I would not give that advice. But as Jonatan pointed out, Allegro is not the standard template library. I said that I don't think the point of the STL (or C++) is to allow people to program stuff without understanding it; but rather to allow people to write object orientated code that is easy to read and has a reliable foundation. On the other hand, I do think that part of the purpose of Allegro is to allow programmers to do things without understanding the details.
Regardless of what you or I think STL and Allegro are for, I would still recommend that anyone who wants to learn about programming should at some point use dynamically allocated arrays (do-it-yourself 'vectors') and write their own linked list implementation. I know that I understand pointers and memory addresses and computers in general a lot better for having done those things myself, and the knowledge I've gained from it has enabled me to do more complicated things.
It's not weeks of studying for gods sake... It takes maybe 5 hours to build a decent linked list from scratch if you don't know anything about it - and that time includes googling for help.
That's pretty much the point. People always love to make this argument for linked lists - but you basically never see it for vectors, deques, maps, strings, or any of the other STL containers. Why? Because people for some reason have this thing about linked lists...
It doesn't really make sense. There's really no logic to shoving it down the throats of beginning programmers. Honestly, check out some modern intro-to-C++ books - many of them introduce you to vector and string before they even teach things like control structures.
Data structures and algorithms are something a programmer should learn in CLASS before they even start coding.
Do we really want to take a poll here to see how many people learned that way? That pretty much falls in the "maybe in a perfect world..." category.
Regardless of what you or I think STL and Allegro are for, I would still recommend that anyone who wants to learn about programming should at some point use dynamically allocated arrays (do-it-yourself 'vectors') and write their own linked list implementation. I know that I understand pointers and memory addresses and computers in general a lot better for having done those things myself, and the knowledge I've gained from it has enabled me to do more complicated things.
At some point, yes. Just like everyone who's serious about programming should probably learn some asm at some point.
The point of the argument is, encouraging people to use the STL without trying to force them to understand the details of how it works first allows them to do more, faster, easier. And with a language like C++, keeping people interested... allowing them to actually do things, is often a big component in keeping them engaged to actually learn the language.
The point of the argument is, encouraging people to use the STL without trying to force them to understand the details of how it works first allows them to do more, faster, easier.
Is this a good thing though? Encouraging laziness in students seems to be a modern trend.
Is this a good thing though? Encouraging laziness in students seems to be a modern trend.
Laziness according to whom? If I have a person who's been learning C++ for a week and is able to properly use vector, string, or list to create programs even though their eyes gloss over when you start talking about pointers or dynamic memory, how are they lazy?
...their eyes gloss over when you start talking about pointers or dynamic memory, how are they lazy?
If their eyes glaze over when you start talking about those things, they are not lazy; they are stupid, and should get a different major.
Do we really want to take a poll here to see how many people learned that way? That pretty much falls in the "maybe in a perfect world..." category.
Indeed, I'm 100% self taught (programming wise), and I whole heartedly believe in what I said. You NEED to learn and understand the basics or you're just fooling yourself.
If their eyes glaze over when you start talking about those things, they are not lazy; they are stupid, and should get a different major.
Well, I guess we can't all be geniuses like your holiness. Luckily the real world isn't so picky; if they booted everyone who didn't understand pointers after a week of instruction, there'd be maybe a dozen C/C++ programmers in the world.
Luckily the real world isn't so picky; if they booted everyone who didn't understand pointers after a week of instruction, there'd be maybe a dozen C/C++ programmers in the world.
We're not talking about a week after. There are retards that somehow get their degree in "whatever it is they took" and don't know what a pointer is, or how any of the data types they use actually work. That is flawed.
We're not talking about a week after. There are retards that somehow get their degree in "whatever it is they took" and don't know what a pointer is, or how any of the data types they use actually work. That is flawed.
Read my post. Read his reply. We aren't talking about retards, were talking about a particular person being extremely arrogant.
I don't think what he said was entirely uncalled for. They really should have learned what pointers and memory is before anything else, unless they are coding in Python or something.
I recommend learning to make your own linked list before using the STL to do it for you, mostly for the reasons given above. You learn more about what's really going on, which could help you debug things that are going wacky in your code. And it helps with other things that involve similar concepts.
I'm glad my programming teacher made us implement linked lists ourselves instead of just teaching us how to use java's linked list class. Sure, I'll probably use the pre-made class most of the time when I'm actually coding, but I'll know what it's doing when I use it.
That's pretty much the point. People always love to make this argument for linked lists - but you basically never see it for vectors, deques, maps, strings, or any of the other STL containers. Why? Because people for some reason have this thing about linked lists...
Because the linked list is the most degenerate case of the basic tecnhique used for most of those containers.
I don't think what he said was entirely uncalled for. They really should have learned what pointers and memory is before anything else, unless they are coding in Python or something.
Ok, seriously, you're grasping at straws now. Why don't you go ahead and cite a few intro to C++ books or show us the syllabus from a course that teaches pointers "before anything else"? I'm sure you know as well is I do that the vast, overwhelming majority of people who teach C++ disagree with you.
overwhelming majority of people who teach C++ disagree with you.
Sure they disagree with him. That's because he isn't talking about learning c++ but about learning to code. Two totally diffrent things.
And in my opinion a good coder should know how such a basic thing such as how lists are working.
This could be achieved by writing own basic implementations or by looking at the code of finished ones. Depending on skill and personal style each way should be a good way.
I'm sure you know as well is I do that the vast, overwhelming majority of people who teach C++ disagree with you.
Source? Are you the majority?
In fact, the major for Computer Science at my university has 6 hours dedicated to data structures. Just data structures.
Not understanding the basics of what is going on in a computer is great, if all you want to do professionally is make websites using ASP.NET. You won't be able to write the fancy backend code that does all the hard work, because you will have no idea how to solve the most rudimentary problems that comprise that one.
The best way to learn is by doing. If he must learn forever without doing anything he'd give up and become a plumber.
Maybe even change his name to Joe.