Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which string class to use in high performance and to be simple in c++

I'm building a simple framework in c++ , I heard a lot that std::string class is not good in critical performance situations, I'm aware of the problem of copy on return that was fixed in the c++11 by the move constructor for the rvalue reference.

  • did the C++11 fixed that issues?? if so then why there is a proposal for string_ref http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3334.html

  • Is it recommended to use std::string other than any other string?

  • Also the simplicity using std::string by users who all are used to deal with C# is so hard, I decided to provide my string with methods like those in System::String in.NET (I mean the same names , the same scenarios as i know that std and STL provide all functionality I'll need ) if string is good performance how bad the idea of wrapping it inside a custom class that serve the needed functionality? like the following :

.

//this is a simple scratch for what I meant by wrapping std::string inside a custom string
class CustomString
{
public:
    CustomString()// : str(nullptr) //shared_ptr will initialize to zero
    {
    }

    CustomString(const char* str)
    {
        str = make_shared<std::string>(str);
    }

    uint32_t IndexOf(char c)
    {
        //call the appropriate methods in the str->find(...
    }

    uint32_t IndexOf(const char* ofStr)
    {
        //call the appropriate methods in the str->find(...
    }

    uint32_t IndexOf(const CustomString& ofStr)
    {
        //call the appropriate methods in the str->find(...
    }

    CustomString SubString(uint32_t start = 0, uint32_t length = -1)
    {
        //call the appropriate methods in the str->substr(...
    }

    CustomString LastIndexOf(const CustonString& str)
    {
        //call the appropriate methods in the str->rfind(...
    }
    //............
    //.......
    //..complete all other needed functionality
    //.does the added in-between method call will have a noticeable  effect on the performance 
private:
    shared_ptr<std::string> str;

};
like image 493
ahmedsafan86 Avatar asked Jan 19 '26 13:01

ahmedsafan86


1 Answers

To answer one of your questions:

Is it recommended to use std::string other than any other string?

Yes, it is. If someone else wants to use your framework, then if you use std::string they don't have to worry about learning to use a new string class. And most of the time, the performance of std::string is quite satisfactory.

EDIT:

A time when you would not want to use std::string is when you want to pass a string for some other code to look at, but not touch. Note that strings in c++, unlike strings in .NET, are mutable. For that reason, passing std::strings around by value is much more expensive in c++ (because of the copy), so you want to use const std::string&.

Another time you don't want to use std::string is when you want to write a function that takes a string literal (ie, you need a hardcoded string that you can look at but not edit). For this, you want to use a const char *. This is about as efficient as you can get. Also, another c++ programming who looks at your code and sees const char * will immediately think "string literal," which makes the code more self-explanatory.

like image 128
Dan Avatar answered Jan 21 '26 04:01

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!