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

using c++ standard library

Started by Ridcully Mar 4, 2000 at 7:27 AM 4 replies 700+ views
Original Post
Ridcully
Ridcully
i am currently reading a book about c++ and it always says use the STL rather than writing your own code. (okay, it''s from the creator of the language.. ) a good example is a string. i am used to use a char*, allocate space for it and mess around with it on a quite low level. the opposite would be a c++ "string" type, which is more comfortable in use but as i said i am used to use char* and it wouldn''t take me much more time to write a string manipulating routine using standard c. so what are the advantages of the STL then, if it''s not faster development? is it faster? other examples would be vector, map... thanks ridcully
jwalker
jwalker
Well, the STL provides data type abstraction. A function can be codded to return many data types. Example if the calling function is (int), then the STL function can return (int)..
This provides high abstraction, like used in map and vector.
Internal low level functions can be codded to be automated...such as Array and Vector can grow when new elements are added

osmanb
osmanb
In addition, there are already a LARGE number of routines in the library you can use with string that you''d have to duplicate with some other (home-spun) function. You COULD write your own for those, and it might not take that long, but it''s still going to take longer, especially when you make mistakes. This is nothing against you, EVERYONE makes mistakes and ends up with buggy code periodically. At least if you use the standard library, you can be confident that that code will work as it''s supposed to. It also makes you code more maintainable for others. If everyone just uses the components from the standard library, then when you look at someone else''s code, you understand what''s going on. Otherwise, every time you want to maintain something written by someone else, you have to re-learn all of their low-level stuff.

-Brian
crouilla
crouilla
If you plan to use a lot of string manipulation, your best bet is probably to use the prefab C++ libraries. However, if you only plan to use a couple of functions, and you know exactly what those are going to be, it might be in your best interest to manipulate the strings manually, and forego the overhead of using the C++ classes (why bother having a thousand different classes available for abstraction if you only plan to use 2 functions?)

Of course, you have to be careful and debug your code carefully -- step on a memory location, and your program could be GPF''ing all over the place.

-Chris


---<<>>---
Chris Rouillard
Software Engineer
crouilla@hotmail.com
---<<>>--- Chris Rouillard Software Engineercrouilla@hotmail.com
Stoffel
Stoffel
Not to sound like the grumpy-old-man, but I still think std::string fails to be as easy to use as a char[] with sprintf. Granted, strstream and sstream are supposed to let you do this, but their implementation is LAME! And for some reason sstream isn''t supported on our Solaris library.

Anywho, I totally dig the rest of STL.
osmanb
osmanb
You may be right to some degree, but strstream is still safer. It frees you from having to worry about overwriting your array of characters, which happens all too often in C code that uses the sprintf() idiom. All of that said, I agree that the interface to the string stream libraries is a little more obtuse than I''d like. Oh well.

-Brian
PS: Not to be grumpy myself, but you probably don''t have the library because Sun''s C++ compiler bites. They have the absolute worst standard support I''ve seen, especially for template features...

Topic Locked

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

Sign in to reply to this topic.