C++: insert char to a string

后端 未结 3 1078
庸人自扰
庸人自扰 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:08

    1. Everything seems to be compiling successfully, but program crashes the gets to
    conversion >> myCharInsert;
    

    The problem is that you are trying to dereference(access) myCharInsert(declared as a char* ) which is pointing to a random location in memory(which might not be inside the user's address space) and doing so is Undefined Behavior (crash on most implementations).

    EDIT

    To insert a char into a string use string& insert ( size_t pos1, size_t n, char c ); overload.

    Extra

    To convert char into a std::string read this answer

提交回复
热议问题