invalid conversion from ‘const char*’ to ‘char’(从‘const char*’到‘char’的无效转换)
问题描述
我正在尝试使用以下代码行将字符串中的某个字符替换为空格:
I am trying to replace a certain character in a string with a space using the following code line:
str[i] = " ";
如何在不出现问题标题错误的情况下实现这一点?
How can realize this without getting the error in the title of the question?
推荐答案
使用单引号
str[ i ] = ' ';
在 C++ 中,标记 ""
是一个字符串文字,它表示一个包含两个字符的数组:字符集中一个空格的值(例如,ascii 中的值 32)和一个零.另一方面,标记 ' '
代表一个带有空格值(通常为 32)的单个字符.请注意,在 C 中,标记 ' '
表示一个带有空格值的整数.(在 C 中,sizeof ' ' == sizeof(int)
,而在 C++ 中,sizeof ' ' == sizeof(char) == 1
.)
In C++, the token " "
is a string literal which represents an array of two characters: the value of a space in the character set (eg, the value 32 in ascii) and a zero. On the other hand, the token ' '
represents a single character with the value of a space (usually 32). Note that in C, the token ' '
represents an integer with the value of a space. (In C, sizeof ' ' == sizeof(int)
, while in C++, sizeof ' ' == sizeof(char) == 1
.)
这篇关于从‘const char*’到‘char’的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从‘const char*’到‘char’的无效转换


基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01