Jump to content
  • Advertisement
Sign in to follow this  
gretty

When to use String Class Functions & when to use CString functions

This topic is 2789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello

I have a query about appending & comparing strings in c++. When I am, for example, trying to find whether a character exists in a string I could use .find() or I could convert the string to a cstring & use memchr().

I know that maybe the obvious answer is use string functions when you have a string & use cstring functions when you have a char array OR that string functions actually USE cstring functions so using either has the same result. But maybe converting a string to a cstring then using memchr() may actually turn out faster?

So when programming in c++ is it?...
- Better coding practice/standard to use string class functions
AND/OR
- Unsafe to use cstring functions because you are working directly with the memory & could make an error
AND/OR
- Faster to append & compare strings by going straight to cstring functions?

Share this post


Link to post
Share on other sites
Advertisement
Use the std::string unless you find a bottleneck.

If there is a bottleneck here, I suspect you might get more benefit by maintaining a searching data structure than just choosing between two reasonably similar algorithms.

In other words: Profile first, then macro-optimise, then micro-optimise (if necessary). Stop when profiling indicates performance is satisfactory or bottleneck is elsewhere.

Share this post


Link to post
Share on other sites
OR that string functions actually USE cstring functions so using either has the same result.

This. The expensive part of using std::string is the dynamic allocation. String comparison is more or less than same whether you're using c or c++. The real overhead of String is the dynamic allocation. It'll take over 100 times as long to allocate a String as it will to compare, say "Hello" and "World".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!