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

前端 未结 23 2619
南方客
南方客 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:42

    #include 
    #include 
    #include 
    using namespace std;
    string itos(int i) // convert int to string
    {
        stringstream s;
        s << i;
        return s.str();
    }
    

    Shamelessly stolen from http://www.research.att.com/~bs/bs_faq2.html.

提交回复
热议问题