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

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

    • std::ostringstream
    #include 
    
    std::ostringstream s;
    s << "John " << age;
    std::string query(s.str());
    
    • std::to_string (C++11)
    std::string query("John " + std::to_string(age));
    
    • boost::lexical_cast
    #include 
    
    std::string query("John " + boost::lexical_cast(age));
    

提交回复
热议问题