std::cout会影响编译的结果吗
Does std::cout affect the result of compilation?
我使用C++接收一个包含一些单词的字符串,这些单词由任意数量的空格分隔,并打印出每个单词的第一个字母。
这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hi my name is rex";
int i = 0;
int len = str.length();
while (i < len) {
// cout << " blah "; // <--- Note this line
cout << str[i];
while (str[i] != ' ') ++i;
while (str[i] == ' ') ++i;
}
}
如果我运行这段代码,我将得到一个运行时错误(请参阅此处)。
然而,如果我不评论"废话"行,我将获得"成功",并打印出"废话"(见此处)。
我知道我可能应该检查I<len在这两个嵌套的while循环中,但我想知道的是,为什么打印"blah"行会对编译结果产生如此大的影响。
有人能帮我解决这个问题吗?谢谢
cout
正在使用缓冲区。在该缓冲区被刷新之前,"输出"将保留在缓冲存储器中
但是当到达字符串的末尾时while循环while (str[i] != ' ') ++i;
继续进行。在线IDE给程序一些时间,然后放弃或出现分段故障
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++编译器能在编译时计算出文字的除法结果吗
- wcslen() 在使用编译指示包时返回不正确的结果
- 未经GCC优化而编译的简单C++程序不会产生预期的结果
- 重用编译器前端的结果,以加快多个配置/平台的C++代码编译
- 为什么 gcc 和 clang 为我的程序编译为不同的"const"结果?
- 用不同版本的Ubuntu上的GCC编译会产生不同的结果
- 在编译时拆分字符串在不同的编译器上会得到不同的结果
- 使用不同版本编译的相同代码提供不同的结果
- std::cout会影响编译的结果吗
- 传递右值引用结果编译错误unique_ptr
- 编译时检查大小是否为..(args) 匹配来自 constexpr 函数的结果
- GCC:为什么编译.HPP文件与.cpp文件的结果不同
- 代码检查 std::unordered_set::find 的结果无法编译
- 使用COUT来输出位运算符的结果时编译错误
- g++ 对象文件参数位置很重要,并给出无法正常编译的不同结果
- gluPerspective with SFML:相同的编译,不同的结果.图形未显示
- 如何在C++中打印编译时计算的结果
- 编译一个相当简单的c++11程序时,gcc和clang之间的结果不同
- Boost bjam:将结果与预编译库链接