将整数添加到字符
addition of integer to char
有人可以解释一下这里发生了什么,在第一个将 char 添加到 int 中,ASCII 值工作正常
但在第二个不起作用你能解释一下它是如何工作的吗?
#include<iostream>
using namespace std;
int main(){
string str="1234";
str[0]=str[0]+1; //working fine
cout<<str<<endl;
str[1]=str[1]+'c'; //printing some new character at 1 position
cout<<str<<endl;
}
我是 n
str[1] = str[1] + 'c'
'2' + 'c'
,这与 2 + 'c'
不同。
在ascii中,'2'
是50
,而'c'
是99
。
它的总和是 149
不在 ascii (0-127) 的范围内,因此显示的字符取决于您使用的扩展 ASCII(可能ò
)。
如果你想有2 + 'c'
,你必须在你的情况下做str[1] = str[1] + 'c' - '0'
。
相关文章:
- 只能向C++添加一定数量的字符
- 如何在C++中为字符串添加字符?
- 如何将字符添加到已经大小的数组中?
- 向字符串添加字符
- 如何编写模板函数来添加整数但连接字符串和字符类型?
- 在字符串中的字符前添加空格C++插入函数
- std::字符串添加到字符*
- Qt UTF-8 文件到 std::string 添加额外的字符
- 编译器将随机字符添加到我的 char 数组中
- 为什么编译器不抱怨将字符添加到 char*?
- 将作为数学运算符的字符添加到随机字符串中
- 将字符添加到 std::cin
- 如何将点字符添加到字符串中的字符
- strftime 将不需要的字符添加到我尝试显示的内容中
- 当我将字符添加到空字符串(如 " " + c)时,为什么要返回未知值?
- 如何将字符添加到 2D 数组
- 将空格字符添加到附加的每个项目,以必须擦除最后一个项目.如何改进
- "b"作为字符添加到字符串中
- 如何将字符添加到字符串中
- C++将x个字符添加到字符串数组中