There are several ways to concatenate strings in C++. In this recipe, we will look at the three most common: the string class operator+(), the string class append() function, and the ostringstream class operator<<(). New in C++20, we also have the format() function. Each of these has its advantages, disadvantages, and use cases.

How to do it

In this recipe, we will examine ways to concatenate strings. We will then perform some benchmarks and consider the different use cases.

  • We'll start with a couple of std::string objects:

Get hands-on with 1200+ tech skills courses.