简体   繁体   English

std :: cout的奇怪问题

[英]Strange problem with std::cout

I have 2 loops: 我有2个循环:

//Loop 1
for ( vector<string>::iterator iter = vecv.begin() ; iter != vecv.end()  ; iter++)
    {
        cout << "-----IN LOOP----" <<  *iter;
    }

//Loop 2
for ( vector<string>::iterator iter = vecv.begin() ; iter != vecv.end()  ; iter++)
    {
        cout << "-----IN LOOP----" <<  *iter << endl ;
    }

Now vecv is a string vector and contains 2 strings: 现在vecv是一个字符串向量,包含2个字符串:

65 and A000 respectively. 65A000

Now the loop 1 Does not print anything, in fact the loop seems to not run at all. 现在循环1不打印任何内容,实际上循环似乎根本没有运行。

However on adding the endl as you see in Loop 2, it gives this output: 但是,如您在循环2中看到的那样添加endl ,将提供以下输出:

-----IN LOOP----65
-----IN LOOP----$A000

What is happening exactly? 到底是怎么回事?

Mind you i am facing this problem only in Visual Studio 2010 and not Dev-C++!! 请注意,我仅在Visual Studio 2010中而不是在Dev-C ++中面临此问题!

std::endl also flushes the buffer. std::endl也刷新缓冲区。 If you want to flush without linebreak, use std::flush . 如果要刷新而不换行,请使用std::flush

How do you know it doesn't print anything without endl ? 您怎么知道没有endl不会打印任何内容? By observing running program? 通过观察正在运行的程序? If so, the problem is that cout provides buffered output: it actually prints somehting on the screen (or in the file) only when its buffer is full. 如果是这样,则问题是cout提供了缓冲的输出:实际上,只有在其缓冲区已满时, cout才会在屏幕(或文件)中打印内容。

endl on other hands flushes the buffer. 另一方面, endl冲洗缓冲区。

If you want unbuffered output you can use cerr , but it would lead to some differences (like, now if you want to redirect output to file you should do 2> instead of > etc.) 如果想要无缓冲的输出,可以使用cerr ,但这会导致一些差异(例如,现在,如果您要将输出重定向到文件,则应该执行2>而不是>等)。

If you want buffered output but don't want to have newlines, print flush into cout . 如果你想缓冲输出,但不希望有换行符,打印flushcout

I think the cout.clear() just destroy the cout buffer. 我认为cout.clear()只会破坏cout缓冲区。 Since it has not been flushed therefore it's lost. 由于尚未刷新,因此丢失了。

EDIT 1: Code providing : 编辑1:提供代码:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {

  vector<string> vecv;

  vecv.push_back("abc");
  vecv.push_back("def");

  //Loop 1
  for ( vector<string>::iterator iter = vecv.begin() ; iter != vecv.end()  ; iter++)
  {
    cout << "-----IN LOOP 1----" <<  *iter;
  }

  //Loop 2
  for ( vector<string>::iterator iter = vecv.begin() ; iter != vecv.end()  ; iter++)
  {
    cout << "-----IN LOOP 2----" <<  *iter << endl ;
  }
}

outputs : 输出:

./a.out
-----IN LOOP 1----abc-----IN LOOP 1----def-----IN LOOP 2----abc
-----IN LOOP 2----def

You are not showing the full program, but I suspect that it ends with a system("pause"); 您没有显示完整的程序,但是我怀疑它以system("pause");结尾system("pause"); , as is often seen in tutorials for MSVC. ,就像在MSVC教程中经常看到的那样。 MSVC runtime does not flush the output buffer when calling system() (it isn't required to), thus all output to std::cout remains buffered if no std::flush , std::endl , or input from std::cin were performed. 调用system()时,MSVC运行时不会刷新输出缓冲区(不需要这样做),因此,如果没有std::flushstd::endl或来自std::cin输入,则所有输出到std::cout保持缓冲。 std::cin进行了。

Try running your program in a console window (Start->Run->cmd.exe). 尝试在控制台窗口(开始->运行-> cmd.exe)中运行程序。 The output should appear when the program terminates, even without endl . 程序终止时,即使没有endl ,输出也应该出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM