I saw a code like following. My understanding is that it has the following perf issues:
Should use string.append rather than + operator. str1 + "abc" will cause a new string object with its underlying heap data copied from str1 and moved from "abc" (because it matches string operator + (string& lhs, string&& rhs)(. Then with additional "+" in the expression, it will create new string object total 3 times with multiple overlapping copy operations which is waste full. string.append should do in-place extension. it will neither cause creating 3 string objects, nor will it cause wasteful copy. Can someone confirm my understanding?
class Foo {
public:
std::string GetCombined() {
return str1 + "abc" + str2 + "xyz";
}
private:
std::string str1, str2;
}
Aucun commentaire:
Enregistrer un commentaire