<分区>
我是 C++ 的新手,这个问题对很多人来说可能看起来微不足道,但请记住,我才刚刚开始学习 C++ 语言。
我已经分配了一个变量 x
等于20
并想将它与一个字符串连接起来。我的 C++ 代码如下。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int x = 20;
int y = 15;
if (x >= y) {
cout << x + " is greater than " + y;
}
}
我的预期结果是 20 is greater than 15
, 但出现的却是一些奇怪的 é@
.我很困惑,我无法在 GeeksForGeeks、w3schools 或其他 SO 上找到解决方案。
我知道使用 cout << x << " is greater than " << y;
工作得很好,但我不确定为什么串联在这里不起作用。还有,为什么会出现这些奇怪的字符呢?
提前致谢。
(此外,请不要在没有回答问题的情况下留下答案。我记得在启动 JS 时我问了一个问题,唯一的答案是“不要使用 document.write
”。虽然我明白了,但它会是实际回答问题并将其作为旁注要好得多。)