Advertisement

Learning STL...

Started by May 12, 2000 05:24 PM
16 comments, last by null_pointer 24 years, 6 months ago
Stoffel: Thanks! Just what I was looking for! Nothing is what it seems. Hasn''t the creator of STL ever heard of using comments in class headers?

SiCrane: My biggest trouble with using other people''s libraries is that they aren''t programmed "correctly"

Kraiklyn: I am aware of the concept of "book." I just can''t afford a "book" right now. I do understand (and have implemented) template classes and functions. STL has its own distinct "style," which I do not agree with 100%, but that puts it in a category with 99.99999% of all source code in existance.




- null_pointer
Sabre Multimedia
One nit-picky correction ... Bjarne Stroustroup DID NOT have anything at all to do with the creation of the STL ... not even a hint, or an idea, or any implementation. I cannot remeber the name of the guy who did, but I believe either his first or last name starts with N. (not much help eh...), sorry.

As for why they are not commented, well that''s because they are written by P.J.Plauger who runs Dinkumware Ltd... and is trying to make a living as a Library writer... Some other version availible from H.P. and S.G.I. as well as the GNU versions are all more well documented ... and MOST IMPORTANT, they use meaningfull names. PJP realizes that if his code is meaningless to people who do not already known how it works, he will minimize competition in the future. Don''t get me wrong, I like PJP, and find his C++ User''s Journal articles to be extremely informative, but I''m just pointing out that the Library is hard to follow for BUSINESS reasons, not because it has to be so archaicly laid out and named.

As for learning. I found Herbert Schildt''s book "C++: The Complete Reference" 3rd Edition by Osborne Press, to be a great start. And SiCrane has the order about right EXCEPT that the algorythms are COMPLETELY unneeded by a learning programmer ... i mean, we have all written "find" and "copy" for our own ADTs before. So I think learning iterators then: Vector, Deque, List, THEN algorythms, and then the Map and Set containers, would be easiest AND most usefull, because you''ll find you need to understand a deque inside and out long before you need to partition one

helpfull hint one ... when you are first learning iterators, just think of them as pointers into contiguous block of objects ... as long as you only use the specified operations to move around (usually ++, but for random access you can use arbitrary increments/decrements) then it is just like using a pointer into an array.

helpfull hint two ... the STL consistently uses iterators to specify a RANGE ... and the first iterator always points to the first data member in the range, but the second iterator always points to an iterator one PAST the last element in the range. Example: begin() returns the first element in a container ... so you start iterating AT begin() ... but end() returns an invalid iterator ... so you DO NOT operate on the data pointed to by end(). a loop looks like this:

for(MyDeque::iterator pos = myData.begin; pos != myData.end(); ++pos)
{
// do something ... such as
cout << *pos;
}

NOTE - the for loop stops when pos == end() ... so *pos does NOT get called when pos == end() (which would be an error ... just like if pos was a NULL pointer).
Advertisement
Xai: Thanks, but I still don''t understand why anyone would deliberately make their code hard to read, unless he''s paranoid or slightly demented. I''m trying to be a library writer, and I see absolutely no need for that. The proper naming is still there -- he just made other people do the conversions for him. And how can someone POSSIBLY sell a library if it''s hard to read and requires massive time to learn and convert? I thought libraries were SUPPOSED to encapsulate code to make it easier to read, and that''s a big selling point. Most forms of "copy protection" just discourage good users and bad users. Hey! Maybe if more people made their libraries hard to read, we''d all be better off? *disgusted*

Oh well, I guess I''ll use it though since other people seem to like it...


- null_pointer
Sabre Multimedia
Usually, a library''s implementation source code is not read by the end user: you just use the docs to see what functions to call in the interface. Therefore you can sell it safe in the knowledge that your proprietary knowledge is hidden from someone who might rip it off, fix 2 bugs, and sell it as a ''better'' version. With the STL being done in templates, the source code is there to see, so it makes sense that if he wants to protect his work, he will just make it obscure to read. Your end code using his version of the STL will be no different from your code using anyone else''s STL. The interface is identical. The only difference is that his proprietary implementation is obfuscated. It -should- make no difference to the user, given good enough STL documentation. As mentioned elsewhere, the SGI STL is a lot clearer, with better variable names, nicer layout, and better documentation too. Just go by the docs, leave the headers alone until you need them
Kylotan: I see your point with templates. BTW, I just checked out the vector header, and I''m feeling a little woozy right now...so I don''t have the strength to post anything more...they ought to put a warning on the STL source not to operate heavy equipment after looking at it... *keels over*


- null_pointer
Sabre Multimedia
Just a quickie here.

The advantage function objects have over function pointers is that they are capable of carrying state ! Just think about what cool things become possible.
Advertisement
And you can derive subclasses from a function object. For example, you could derive QuickSort, MergeSort, HeapSort from a Sort object and pass them to some function without the client code needing to know what kind of sort it is performing. This also has more type-safety than a function pointer, as you can only pass objects of type Sort).
Thanks again guys, I''m a quick learner!

Anyway, function objects are like verbals in English, huh?


- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement