Skip to main content
gamedev.net
🔒 Locked

Learning STL

Started by Aeolus Jan 3, 2001 at 7:16 PM 2 replies 350+ views
Original Post
Aeolus
Aeolus
I have several books on C++. None of which go into the STL that much I was wondering if anyone know a good book to learn STL from. "Nothing will ever be attempted if all possible objections must first be overcome." - Samuel Johnson
"Nothing will ever be attempted if all possible objections must first be overcome." - Samuel Johnson
Marsupial Rodentia
Marsupial Rodentia
I've looked for those books and havn't found any good ones. All the online reviews on Amazon and Borders don't say many good things about STL books.

Recently I've been doing some work with STL and in hindsite, STL is too easy to even need a book. You just have to know and understand the "properties" of each container (which is mentioned in the STL docs in MSDN and the STL docs on http://www.dinkumware.com).

In my opinion, the easiest way to use STL containers is to inherit from them. For example
    
#include <map>

#include <string>

using namespace std;


struct mystruct
{
int nMyData;
};
typedef mystruct* PMYSTRUCT;

typedef map< string, PMYSTRUCT > MAP;

struct CMap : public MAP
{
void Insert( char* szKey, int nData )
{
// Insert into the container as if it were a binary tree!

PMYSTRUCT pMine = new mystruct;
pMine->nMyData = nData;
insert( value_type( string(szKey), pMine ) );
}
~CMap() // Destructor

{
// Iterate through the container as if it were a list!

iterator i;
for( i = begin(); i != end(); ++i)
{
delete i->second; // Prevent memory leaks.

}
clear();
}
};


Note that STL map and STL set are both implemented as Red-Black trees, so they have the property of O(LogN) performance for insert() and find().

This means that when you insert an item or search for an item, and there are already a million items in the container, that (in the worst case) only 40 items will be visited for comparison.





Edited by - Marsupial Rodentia on January 4, 2001 1:18:47 AM
Aeolus
Aeolus
Marsupial Rodentia, thanks for the help. I am writing a text game for credit at my school. And am looking for what type of container I should use for the zones,items,intellegences etc. again thanks for the help.

"Nothing will ever be attempted if all possible objections must first be overcome."
- Samuel Johnson
"Nothing will ever be attempted if all possible objections must first be overcome." - Samuel Johnson
Marsupial Rodentia
Marsupial Rodentia
"what type of container I should use for the zones,items,intellegences..." sounds like you need a remedial Algorithms and Data Structures class.

Topic Locked

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

Sign in to reply to this topic.