C++,在特定索引处更改字符串的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18305844/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:53:13  来源:igfitidea点击:

C++, best way to change a string at a particular index

c++string

提问by David

I want to change a C++ string at a particular index like this:

我想在特定索引处更改 C++ 字符串,如下所示:

string s = "abc";
s[1] = 'a';

Is the following code valid? Is this an acceptable way to do this?

以下代码有效吗?这是一种可以接受的方法吗?

I didn't find any reference which says it is valid:

我没有找到任何说明它有效的参考:

http://www.cplusplus.com/reference/string/string/

http://www.cplusplus.com/reference/string/string/

Which says that through "overloaded [] operator in string" we can perform the write operation.

也就是说,通过“字符串中的重载 [] 运算符”,我们可以执行写操作。

采纳答案by Eric Leschinski

Assigning a character to an std::stringat an index will produce the correct result, for example:

将一个字符分配给std::string一个索引将产生正确的结果,例如:

#include <iostream>
int main() {
    std::string s = "abc";
    s[1] = 'a';
    std::cout << s;
}

which prints aac. The drawback is you risk accidentally writing to un-assigned memory if string s is blankstring or you write too far. C++ will gladly write off the end of the string, and that causes undefined behavior.

打印aac. 缺点是如果字符串 s 是空白字符串或者您写得太远,您可能会意外写入未分配的内存。C++ 很乐意注销字符串的结尾,这会导致未定义的行为。

A safer way to do this would be to use string::replace: http://cplusplus.com/reference/string/string/replace

一个更安全的方法是使用string::replacehttp: //cplusplus.com/reference/string/string/replace

For example

例如

#include <iostream> 
int main() { 
    std::string s = "What kind of king do you think you'll be?"; 
    std::string s2 = "A good king?"; 
    //       pos len str_repl 
    s.replace(40, 1, s2); 
    std::cout << s;   
    //prints: What kind of king do you think you'll beA good king?
}

The replace function takes the string s, and at position 40, replaced one character, a questionmark, with the string s2. If the string is blank or you assign something out of bounds, then there's no undefined behavior.

替换函数接受字符串 s,并在位置 40 处用字符串 s2 替换一个字符,即问号。如果字符串为空或者您分配的内容超出范围,则没有未定义的行为。

回答by Prashant Shubham

Yes the code you have written is valid. You can also try:

是的,您编写的代码是有效的。您也可以尝试:

string num;
cin>>num;
num.at(1)='a';
cout<<num;

**Input**:asdf
**Output**:aadf

the std::replace can also be used to replace the charecter. Here is the reference link http://www.cplusplus.com/reference/string/string/replace/

std::replace 也可用于替换字符。这是参考链接http://www.cplusplus.com/reference/string/string/replace/

Hope this helps.

希望这可以帮助。

回答by Neil Kirk

Yes. The website you link has a page about it. You can also use at function, which performs bounds checking.

是的。您链接的网站有一个关于它的页面。您还可以使用执行边界检查的 at 函数。

http://www.cplusplus.com/reference/string/string/operator%5B%5D/

http://www.cplusplus.com/reference/string/string/operator%5B%5D/

回答by Nasz Njoka Sr.

You could use substring to achieve this

您可以使用 substring 来实现此目的

  string s = "abc";
  string new_s = s.substr(0,1) + "a" + s.substr(2);
  cout << new_s;
  //you can now use new_s as the variable to use with "aac"