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

C++ string and carray question

Started by Drethon May 25, 2006 at 2:59 PM 2 replies 850+ views
Original Post
Drethon
Drethon
At the lowest level of C++ string and carray functions is there any differences? What I mean is do both have to traverse the whole string to find specific characters, the only real difference being the string class has more easy to use wrappers around the functionality? I don't really see how it can be any other way as strings are not predictable enough to use common sorting methods.
My $0.02
rip-off
rip-off
std::strings have well defined creation and destruction times. With a c style string, you dont know if the value is stack or heap allocated, or is a static constant embedded into the exectuable.

You always know the length of a std::string, allowing it to be more efficient in some operations that require the length.

Anyway, "easy to use" always sounds good to me. [smile]
Drethon
Drethon
Makes sense, I guess what I'm mostly asking (now that I've looked at this topic after being busy with work for a while) is if I were to write my own string class and base it around char*s. Would it be just as effecient as C++ String class, assuming I don't do anything stupid...
My $0.02
Enigma
Enigma
Quote:
Original post by Drethon
Makes sense, I guess what I'm mostly asking (now that I've looked at this topic after being busy with work for a while) is if I were to write my own string class and base it around char*s. Would it be just as effecient as C++ String class, assuming I don't do anything stupid...

No. Don't get me wrong - it's entirely possible to write a string class as efficient as the SC++L string class - but you have to do a lot more than just "not do anything stupid". Your standard library implementation was written by experts. It was written by people who know your particular compiler like the back of their hand. It likely makes use of all sorts of clever optimizations.

Now, you could write your own string class. But what's the point? It will take longer than if you just use std::string. Unless you're very clever or designing for specialist use (e.g. fixed-length strings) it will be no more efficient than std::string. It will almost certainly be more buggy than std::string. Writing a string class might be a good learning exercise, but for general use - 99.99% of the time it's really not worth it. Also note that additional functionality can easily be added to std::string by providing non-member functions. See for example Boost.String_Algo.

Σnigma

Topic Locked

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

Sign in to reply to this topic.