我正在探索C++中的ostream
类。我困惑于对字符串和整数数据类型使用cout
时奇怪的输出。
当传递一个整数或浮点值时,输出正好是我传递的值。例如,cout.operator<<(10);
打印10
。但是当传递一个字符串作为参数时,它会打印一些十六进制值:
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* str = "aia";
cout.operator<<(str);
return 0;
}
输出:0x4007e0
。
operator<<
?你想要做什么? - Nicol Bolascout << str;
即可看到您期望的输出。 - john