C ++混合printf和cout [英] C++ mixing printf and cout

查看:75
本文介绍了C ++混合printf和cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
混合cout和printf以获得更快的输出

我正在使用Microsoft Visual Studio 6.0.

I'm using Microsoft Visual Studio 6.0.

以下程序,

#include "stdafx.h"
#include "iostream.h"

int main(int argc, char* argv[])
{
printf("a");
printf("b");
printf("c");
return 0;
}

产生"abc".

同时使用以下程序,

#include "stdafx.h"
#include "iostream.h"

int main(int argc, char* argv[])
{
printf("a");
cout<<"b";
printf("c");
return 0;
}

产生"acb".

出什么问题了?我不能在同一程序中混合使用cout和printf吗?

What's the problem? Can't I mix cout and printf in the same program?

推荐答案

标准规定:

当标准iostream对象 str 与标准同步时stdio流 f ,通过

When a standard iostream object str is synchronized with a standard stdio stream f, the effect of inserting a character c by

fputc(f, c);

str.rdbuf()->sputc(c);

用于任何字符序列;

默认情况下,除非您调用 sync_with_stdio(false),否则 cout stdout 同步.因此,您的第二个代码段等效于:

By default, unless you invoke sync_with_stdio(false), cout is synchronized with stdout. Therefore your second code snippet is equivalent to:

printf("a");
fputc(stdout, 'b')
printf("c");

即使在您的实现中,也必须产生"abc".

Which must produce "abc" even on your implementation.

底线:MSVC6不符合标准,这并不奇怪,因为它很旧.

Bottom line: MSVC6 is not standard conforming, which is not a surprise as it is very old.

这篇关于C ++混合printf和cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆