1

I would like to append a char to a std::string in C++. I can think of at least three ways to do it:

std::string s = "hello";
char c = '!';

s += c;
s.append(c);
s.push_back(c);
  • which one is considered best practice ?
  • is there a performance difference (C++11) ?

Also, if I want to add two strings, same question with:

std::string s1 = "hello ", s2 = "world";

s1.append(s2);
s1 += s2;
1
  • 3
    There should be no performance difference. Your compiler should generate the same code for all of those examples. And while there are no immediate differences between these three methods, there are distinctions in what they can do. For example, string::append() has several more overloads. And string::push_back() exists so the container can be used in generic programming. Commented Mar 25, 2014 at 1:16

2 Answers 2

4

The question is a matter of the compiler and personal taste and/or company coding style requirements. The over loaded operator probably resolves to your 2nd choice.

I would go with first one myself

Sign up to request clarification or add additional context in comments.

Comments

2

which one is considered best practice?

If you've written a template accepting a variety of container types including string, the containers' APIs may only have a subset of those methods in common - in which case it's good to use that.

For example:

template <typename Container>
void f(Container& c, int n)
{
    if (n % 7) c.push_back(n);
}

This will work with anything that support push_back - namely std::vector, std::deque, std::list, std::string. This contrasts with += and append which - of the Standard Library containers - are only supported by std::string.

Otherwise, do whatever you think's clearest to read - likely +=.

is there a performance difference (C++11)?

No reason there would be in optimised code, though implementations aren't required by the Standard to ensure that.

(Talking about performance of unoptimised code is not often useful, but FWIW in unoptimised code it's possible one or more of these may be written in terms of another (i.e. it may call the other function), and if inlining isn't done then the less direct code may be ever-so-slightly slower: which that is may vary across implementations. It would be misguided to worry about this.)

Also, if I want to add two strings, same question [...]

For two std::strings it's a little more complicated, as something like this...

template <typename Container, tyepname Value>
void append_twice(Container& c, const Value& v)
{
    c.push_back(v);
    c.push_back(v);
}

...could still be used for vector, deque, list, and string, but it does something semantically different for string by implicitly extracting and appending char elements, so you might want to avoid supporting all the types if correct behaviour of later code in your algorithm depends on that difference between extracting the chars and extracting the same string elements from the container that were appended earlier.

Again there's no performance difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.