Search code examples
c++algorithmstring-concatenationstdstringstring-literals

How to add several string together such as "123"+"456"?


How to achieve such operation, the Visual Studio always tells me that it was wrong.
The wrong code is C2110 and E2140.
Can anyone help?

std::string a = "2323" + "22323" + "232332";

Solution

  • The expression "2323" is not a std::string, it is a const char[5].

    Since C++14, you can have a literal of type std::string:

    using namespace std::string_literals;
    
    std::string a = "2323"s + "22323"s + "232332"s;