我越来越绝望了。在使用 Code::Blocks 的 Windows 7 上,我已经安装了大约六个 MinGW/TDM-GCC 变体,但我无法使用 to_string 将我的 int 转换为字符串,例如:
std::cout << std::to_string(1) << ' - one' << std::endl;
输出 1544173669
我已经看到关于 to_string 在较早版本的 MinGW(v4.7 - 4.9 之前的任何地方)中不起作用的各种错误报告,但我已经尝试了最新版本无济于事。我关注了these instructions安装最新的 TDM-GCC,适本地更改工具链和调试器设置。
我所要求的只是关于为什么这不起作用的某种解释和解决方案。我可以根据需要提供任何进一步的信息。
最佳答案
它实际上为您正确地打印了它,当然还有您的多字符常量(这是实现定义的)...
std::cout << std::to_string(1) << ' - one' << std::endl;
这部分:
std::to_string(1)
打印1
这部分:
' - one'
544173669
为您打印。这给了你你得到的输出:
1544173669
。您的编译器应该(假设
-pedantic
)对表达式' - one'
发出警告。参见 Multi-character constant warnings和 What do single quotes do in C++ when used on multiple characters?
你用双引号表示一个字符串,你想写的可能是:
std::cout << std::to_string(1) << " - one" << std::endl;
关于C++ - to_string 在 Windows 中的 Code::Blocks 中无法跨多个编译器(MinGW、MinGW64、TDM GCC)工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37312988/