const char* concatenation

前端 未结 12 2207
抹茶落季
抹茶落季 2020-11-29 18:09

I need to concatenate two const chars like these:

const char *one = \"Hello \";
const char *two = \"World\";

How might I go about doing tha

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 18:30

    const char *one = "Hello ";
    const char *two = "World";
    
    string total( string(one) + two );
    
    // to use the concatenation as const char*, use:
    total.c_str()
    

    Updated: changed string total = string(one) + string(two); to string total( string(one) + two ); for performance reasons (avoids construction of string two and temporary string total)

    // string total(move(move(string(one)) + two));  // even faster?
    

提交回复
热议问题