How to append a char to a std::string?

后端 未结 13 2168
半阙折子戏
半阙折子戏 2020-11-29 15:58

The following fails with the error prog.cpp:5:13: error: invalid conversion from ‘char’ to ‘const char*’

int main()
{
  char d = \'d\';
  std::s         


        
13条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 16:17

    Also adding insert option, as not mentioned yet.

    std::string str("Hello World");
    char ch;
    
    str.push_back(ch);  //ch is the character to be added
    OR
    str.append(sizeof(ch),ch);
    OR
    str.insert(str.length(),sizeof(ch),ch) //not mentioned above
    

提交回复
热议问题