How to concatenate a std::string and an int?

前端 未结 23 2821
南方客
南方客 2020-11-22 02:40

I thought this would be really simple but it\'s presenting some difficulties. If I have

std::string name = \"John\";
int age = 21;

How do I

23条回答
  •  半阙折子戏
    2020-11-22 03:29

    In C++20 you'll be able to do:

    auto result = std::format("{}{}", name, age);
    

    In the meantime you can use the {fmt} library, std::format is based on:

    auto result = fmt::format("{}{}", name, age);
    

    Disclaimer: I'm the author of the {fmt} library and C++20 std::format.

提交回复
热议问题