Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 months ago.
Improve this question
I usually use the += operator to add content to an std::string which is seemingly the same as std::string::append. There is also the push_back method for a string which allows to add one character at a time. For now, I always use += since it is readable compared to other methods, even if I only want to add one character.
Should I prefer push_back when I need to append one character only? Are there any performance issues significant enough to use push_back when adding one character?
Since your question is limited by this
when I need to append one character only?
I think it's fair we keep basic_string& operator+=(const _CharT*) out of the question and concentrate on basic_string& operator+=(_CharT) vs void push_back(_CharT).
In this case, as far as GCC is concerned, += simply calls push_back and returns *this by reference,
_GLIBCXX20_CONSTEXPR
basic_string&
operator+=(_CharT __c)
{
this->push_back(__c);
return *this;
}
so the performance should be the same.
Most likely, other implementation of the STL will use a similar approach.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
Of the two versions of function calls below, is the one with std::move still preferred?
void myFunc(const std::string& myStr){
//
}
std::string MyStr = "my string";
//For these 2 versions, should I still prefer std::move here to save a value copy, even when the function itself takes in a reference?
myFunc(std::move(MyStr));
myFunc(MyStr);
When passing a value by reference std::move doesn't make any sense, because no instantiation is happening here, and there would be no side effects (provided you don't want to alter overload function candidate)
Thus for this particular case there is no any difference and you don't need std::move
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which is the correct way to get an std::string value from a function. The assumption here is that I am writing the function and the caller.
std::string foo()
{
std::string str = "ABC";
return str;
}
OR
void foo(std::string &str)
{
str = "ABC";
}
I understand that in the first method compiler optimizations will come into the picture and return by value will not be a big overhead so this method should be just fine.
The second method guarantees that there is no copy involved so it's going to be always efficient.
Which method would be your choice?
As said in the comments, using a return by value seems to be preferred nowadays. I find my code to be more readable that way, in that you can see that there is actually a change.
With the out parameter foo(str) it's not obvious that str is being changed.
I believe all mainstream compilers are clever enough to optimize away any unnecessary copying.
So, in essence, it's more about readability and clarity than anything else.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
The recommendation for appending one vector to another (according to Concatenating two std::vectors) is to use:
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
Why does vector not have a method along the lines of:
vector1.append(vector2);
The only reason that comes to mind is that it may be unclear exactly what append does.
That's a pretty good reason. In fact, as an apt example, I think you've got the meaning of "append" wrong. I'd expect it to be equivalent to push_back (which exists), whereas you're looking for something more like "concatenate".
Why isn't there a concatenate function? Well, there could have been. But the standard doesn't like to give you things you don't really need, and (as you've shown) this operation is pretty easy to implement yourself.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have code whereby I can query a structure for a std::set of objects of type A that all match some criteria. I will very often want my query criteria to be such that the code returns a set containing only one object. And in these cases, I will want my code to fail if the query hasn't produced only one result. So I would like to make a function
A& deref_or_throw(std::set<A> s)
{ if (s.size() != 1) throw ...; return *s.begin(); }
that throws if the set contains more than one (or no) element, and dereferences the first element otherwise.
For brevity, I thought to overload the indirection operator, which is not defined for std::set:
A& operator*(std::set<A>& s) {return deref_or_throw(s);}
Is this a bad idea? It does fit with the concept of the indirection operator that it performs a dereferencing. But I could not find a strict definition of what the indirection operator should do according to the standards, so as to make sure whether I'm perverting its standard use (too far).
Don't overload operator* in this way. One person's brevity is another person's obfuscation.
In this case there is no precedent for operator* operating on any standard container so in the future if anyone looks at the code they will have no idea what it does without the finding the implementation of your operator*. Instead, take the extra 10 seconds now to copy-paste your function call name and save your future maintainers who knows how much time hunting down the operator overload a year from now.
I would suggest maybe something like *ensure_single_element(your_set).begin() or something where it's quite clear what's going on.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
For years I have used stringname[0] to obtain the first char of a string without even thinking about it. However, I recently came to wonder if brute access to the array is really a good practice. This may seem a trivial question, but it is not because it looks deeply linked to memory/access management of STL containers.
I can think of stringname.at(0) (not very convincing) but there are probably better alternatives with an iterator. Most importantly, the ideal method would not cause an error if the string is empty.
Any widely accepted good practice for this ?
If s is an empty string, s[0] returns '\0' whereas s.at(0) throws std::out_of_range.
That difference in behavior is far more significant than any difference in performance.