0

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?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • 1
    My answer is no and no. I generally use `+=` too. – john Dec 09 '22 at 15:18
  • 1
    If you have C++20, I'd say use std::format. Or else I would use std::ostringstream for building up strings. There are so many ways, if it is performance you worry about, measure before you choose what's best. If not pick what's most readable. – Pepijn Kramer Dec 09 '22 at 15:19
  • 1
    imho `std::string` is one of the most quirky types. I suppose it only has `push_back` because it can and for similarity with other containers – 463035818_is_not_an_ai Dec 09 '22 at 15:20
  • 2
    Most likely one will be implemented in the terms of the other anyway, resulting in the former being inlined, so no performance difference. You *do* get one, though, if using `+=` inapropriately, using string literals for a single character, i.e. `std::string() += "s"` (correct would be `std::string() += 's'`), though the difference should be marginal and only notable in high performance code. – Aconcagua Dec 09 '22 at 15:20
  • I expect it's main purpose is to have a consistent interface between containers in the standard library. – drescherjm Dec 09 '22 at 15:20
  • 1
    If you decide to replace `std::string` with say `std::vector` you will have to change less code than if you use `+=`. – ks1322 Dec 09 '22 at 15:21
  • What can be a much larger performance issue is, in contrast, not `reserve`ing sufficient memory in advance! – Aconcagua Dec 09 '22 at 15:21
  • 2
    imho your change in title made the question worse. If you want to know which is faster you dont need to ask you can and should measure – 463035818_is_not_an_ai Dec 09 '22 at 15:23
  • ***Should I prefer push_back when I need to append one character only?*** Is opinion based. Being faster or not is something you will have to measure for your `c++` implementation. I expect that if there is a difference in performance it will be so small that it will not be easily measurable unless you are doing 10s of thousands of inserts. – drescherjm Dec 09 '22 at 15:36
  • std::string is one of the containers. Containers are required to support the specific interface for the algorithm library. Hence std::string::push_back exists but does not exclude other convenient API. Using one or another is up to you. – 273K Dec 09 '22 at 15:36

1 Answers1

3

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.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 2
    MSVC does the same: `_CONSTEXPR20 basic_string& operator+=(_Elem _Ch) { push_back(_Ch); return *this; }` – ChrisMM Dec 09 '22 at 15:26