C++:将字符插入字符串

发布于 2024-09-08 16:06:33 字数 963 浏览 9 评论 0 原文

所以我试图将从一个字符串中获得的字符插入到另一个字符串中。 这是我的行动: 1. 我想使用简单的:

someString.insert(somePosition, myChar);

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. 一切似乎都编译成功,但程序崩溃了获取行

conversion >> myCharInsert;

5.我试图用字符串替换char*:

stringstream conversion;
char* myCharInsert;
conversion << myChar //That is actually someAnotherString.at(someOtherPosition) if that matters;
conversion >> myCharInsert;
someString.insert(somePosition, myCharInsert);

一切似乎都很好,但是当someAnotherString.at(someOtherPosition)变成空格时,程序崩溃。

那么我该如何正确地做到这一点呢?

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(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:

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

conversion >> myCharInsert;

line.

5.I am trying to replace char* with string:

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.

So how do I correctly do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴骨ら 2024-09-15 16:06:35

std::string::insert。插入单个字符的重载实际上有三个参数:

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

第二个参数n,是将c插入字符串中位置pos<的次数。 /code> (即重复该字符的次数。如果您只想插入该字符的一个实例,只需将其传递一次,例如,

someString.insert(somePosition, 1, myChar);

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);
淡墨 2024-09-15 16:06:35

最简单的是为自己提供一个将字符转换为字符串的函数。有很多方法可以做到这一点,例如

string ToStr( char c ) {
   return string( 1, c );
}

然后您可以简单地说:

someString.insert(somePosition, ToStr(myChar) );

并在您想要字符串但有字符的其他情况下使用该函数。

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.

小…红帽 2024-09-15 16:06:35
  • 一切似乎都编译成功,但程序崩溃了
  • conversion >> myCharInsert;
    

    问题是您正在尝试取消引用(访问) myCharInsert (声明为 char* ),它指向到内存中的随机位置(可能不在用户的地址空间内),这样做是未定义的行为(在大多数实现上会崩溃)。

    编辑

    要将char插入字符串中,请使用string& insert ( size_t pos1, size_t n, char c ); 重载

    额外

    要将char转换为std::string,请阅读这个答案

    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

    梅倚清风 2024-09-15 16:06:35

    您可以尝试:

    std::string someString{"abc"};
    someString.push_back('d');
    

    You can try:

    std::string someString{"abc"};
    someString.push_back('d');
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文