C++: insert char to a string

后端 未结 3 1080
庸人自扰
庸人自扰 2020-12-05 18:27

so I am trying to insert the character, which i got from a string, to another string. Here I my actions: 1. I want to use simple:

someString.insert(somePosi         


        
3条回答
  •  甜味超标
    2020-12-05 19:04

    There are a number of overloads of std::string::insert. The overload for inserting a single character actually has three parameters:

    string& insert(size_type pos, size_type n, char c);
    

    The second parameter, n, is the number of times to insert c into the string at position pos (i.e., the number of times to repeat the character. If you only want to insert one instance of the character, simply pass it one, e.g.,

    someString.insert(somePosition, 1, myChar);
    

提交回复
热议问题