C++:将字符插入字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3223302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: insert char to a string
提问by James McNellis
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:
所以我试图将我从一个字符串中得到的字符插入到另一个字符串中。下面是我的操作: 1. 我想使用简单的:
someString.insert(somePosition, myChar);
2. I got an error, because insert requires(in my case) char* or string
3. I am converting char to char* via stringstream:
2. 我收到一个错误,因为插入需要(在我的情况下)char* 或 string
3. 我通过 stringstream 将 char 转换为 char*:
stringstream conversion;
char* myCharInsert;
conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters;
conversion >> myCharInsert;
someString.insert(somePosition, myCharInsert);
4. Everything seems to be compiling successfully, but program crashes the gets to
4.一切似乎都编译成功,但程序崩溃了
conversion >> myCharInsert;
line.
线。
5.I am trying to replace char* with string:
5.我想用字符串替换char*:
stringstream conversion;
char* myCharInsert;
conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters;
conversion >> myCharInsert;
someString.insert(somePosition, myCharInsert);
Everything seems to be OK, but when someAnotherString.at(someOtherPosition)
becomes space, program crashes.
一切似乎都很好,但是当someAnotherString.at(someOtherPosition)
变成空间时,程序崩溃了。
So how do I correctly do this?
那么我该如何正确地做到这一点呢?
回答by James McNellis
There are a number of overloads of std::string::insert
. The overload for inserting a single character actually has three parameters:
有许多 的重载std::string::insert
。插入单个字符的重载实际上有三个参数:
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.,
第二个参数,n
,是c
在位置处插入字符串pos
的次数(即重复该字符的次数。如果你只想插入一个字符实例,只需传递一个,例如,
someString.insert(somePosition, 1, myChar);
回答by James McNellis
Simplest is to provide yourself with a function that turns a character into a string. There are lots of ways of doing this, such as
最简单的方法是为自己提供一个将字符转换为字符串的函数。有很多方法可以做到这一点,例如
string ToStr( char c ) {
return string( 1, c );
}
Then you can simply say:
那么你可以简单地说:
someString.insert(somePosition, ToStr(myChar) );
and use the function in other cases where you want a string but have a char.
并在需要字符串但有字符的其他情况下使用该函数。
回答by Prasoon Saurav
- 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).
问题是您试图取消引用(访问)myCharInsert
(声明为 a char*
),它指向内存中的随机位置(可能不在用户的地址空间内),这样做是未定义的行为(在大多数实现中崩溃)。
EDIT
编辑
To insert a char
into a string use string& insert ( size_t pos1, size_t n, char c );
overload.
要将 achar
插入字符串使用string& insert ( size_t pos1, size_t n, char c );
重载。
Extra
额外的
To convert char
into a std::string
read thisanswer
转换char
为std::string
阅读此答案