In learning c++, I first use Qt library instead of the standard C++, STL and all that (Ok, so I'm new with c++ and spoiled by Qt). On Qt, QString used implicit sharing, thus enabling me to just copy assign it to another variable like:
QString var1=QString("Hi there!");
QString var2=var1
And that would do nicely without much overhead. But now, i'm trying std::string so, should I do
std::string var1=std::string()
or
std::string* var1=new std::string()
And also, how about QVector and std::vector. And If I do have to use the pointer... any tips?
The c_str method of std::string returns a raw pointer to the memory buffer owned by the std::string . The pointer is only safe to use while the std::string is still in scope. When the std::string goes out of scope, its destructor is called and the memory is deallocated, so it is no longer safe to use the pointer.
Strings can also be declared using pointers. Let's see an example. In the above example, since p stores the address of name[0], therefore the value of *p equals the value of name[0] i.e., 'S'. So in while loop, the first character gets printed and p++ increases the value of p by 1 so that now p+1 points to name[1].
In general you should always use std::string, since it is less bug prone. Be aware, that memory overhead of std::string is significant. Recently I've performed some experiments about std::string overhead. In general it is about 48 bytes!
A pointer to string in C can be used to point to the base address of the string array, and its value can be dereferenced to get the value of the string. To get the value of the string array is iterated using a while loop until a null character is encountered.
Whether std::string
uses copy-on-write depends on the implementation (i.e. your standard library vendor decides that). However, most std::string
implementations will not use COW, largely due to the fact that most if not all read operations force a copy -- operator[]
returns a reference, c_str()
and data()
return a pointer. Compare this to QString::operator[]
, which returns a proxy object.
In spite of the above, don't use pointers to std::string
, unless you determine (by measuring) that string copies are the bottleneck in your application.
Also, beware that QString
stores UTF-16 strings, whereas std::string
stores a sequence of char
s -- QByteArray
would be the Qt equivalent.
std::string var1("Hi there!");
std::string var2=var1;
std::string class has an =
operator defined as:
string& operator= ( const string& str );
std::string* var1=new std::string()
Don't do that. Just pass it by reference wherever possible:
void f(const std::string& s); // no copying
If you really need to share the string, use:
std::shared_ptr<std::string> var1 = std::make_shared<std::string>();
If you are going to return std::string
from function then don't use pointer - return by value. In this case most likely Return Value Optimization will be applied and string data will not be copied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With