CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

关于输出

楼主huanglun(思考,思考,再思考)2005-04-04 17:59:52 在 C/C++ / C语言 提问

c++中的cout和c中printf输出同样东西的时候  
  为什么结果有时候不同呢  
   
  他们哪里有区别呢,谢谢 问题点数:0、回复次数:10Top

1 楼kobefly(科比--网络学习中)回复于 2005-04-04 18:10:22 得分 0

cout比printf智能啊Top

2 楼solid123(游戏机)回复于 2005-04-04 18:27:44 得分 0

cout比printf少2个字母啊~~~Top

3 楼leon8341(Leon)回复于 2005-04-04 18:59:08 得分 0

cout简单啊,不用输入格式化字符Top

4 楼pcboyxhy(-273.15℃)回复于 2005-04-04 19:03:29 得分 0

cout速度慢Top

5 楼yangkun110(狼的传人)回复于 2005-04-04 19:03:57 得分 0

COUT   叼Top

6 楼yangkun110(狼的传人)回复于 2005-04-04 19:04:28 得分 0

给分就是了,   管他多哦Top

7 楼style1984()回复于 2005-04-04 19:13:43 得分 0

如果用cout在输出字符串时,你就可以看出它的优越老!!!Top

8 楼xiaozhishi(彭志)回复于 2005-04-04 19:21:59 得分 0

你知道吗  
  知道就告诉我哦  
  我不知道呢Top

9 楼xjp6688(大平/要做必须最好)回复于 2005-04-05 08:59:32 得分 0

Summary  
   
  Controls   output   to   a   stream   buffer   associated   with   the   object   stdout   declared   in   <cstdio>.  
   
  Synopsis  
   
  #include   <iostream>  
  extern   ostream   cout;  
  ostream   cout;  
   
  Description  
   
  The   object   cout   controls   output   to   a   stream   buffer   associated   with   the   object   stdout   declared   in   <cstdio>.   By   default   the   standard   C   and   C++   streams   are   synchronized,   but   performance   improvement   can   be   achieved   by   using   the   ios_base   member   function   synch_with_stdio   to   desynchronize   them.  
   
  After   the   object   cin   is   initialized,   cin.tie()   returns   &cout,   which   implies   that   cin   and   cout   are   synchronized.  
   
  Formatting  
   
  The   formatting   is   done   through   member   functions   or   manipulators.    
   
  Manipulators Member   functions  
  showpos setf(ios_base::showpos)    
  noshowpos unsetf(ios_base::showpos)    
  showbase setf(ios_base::showbase)  
  noshowbase unsetf(ios_base::showbase)  
  uppercase setf(ios_base::uppercase)  
  nouppercase unsetf(ios_base::uppercase)  
  showpoint setf(ios_base::showpoint)  
  noshowpoint unsetf(ios_base::showpoint)  
  boolalpha setf(ios_base::boolalpha)  
  noboolalpha unsetf(ios_base::boolalpha)    
  unitbuf setf(ios_base::unitbuf)  
  nounitbuf unsetf(ios_base::unitbuf)  
  internal setf(ios_base::internal,ios_base::adjustfield)  
  left setf(ios_base::left,ios_base::adjustfield)  
  right setf(ios_base::right,ios_base::adjustfield)  
  dec setf(ios_base::dec,ios_base::basefield)  
  hex setf(ios_base::hex,ios_base::basefield)    
  oct setf(ios_base::oct,ios_base::basefield)  
  fixed setf(ios_base::fixed,ios_base::floatfield)  
  scientific setf(ios_base::scientific,ios_base::floatfield)  
  resetiosflags   (ios_base::fmtflags   flag) setf(0,flag)  
  setiosflags(ios_base::fmtflags   flag) setf(flag)  
  setbase(int   base) see   above  
  setfill(char_type   c) fill(c)  
  setprecision(int   n) precision(n)    
  setw(int   n) width(n)  
  endl Puts   揬n?and   flush  
  ends Puts   揬0?and   flush  
  flush flush(   )  
  Description  
   
  showpos Generates   a   +   sign   in   non-negative   generated   numeric   output.  
  showbase Generates   a   prefix   indicating   the   numeric   base   of   generated   integer   output  
  uppercase Replaces   certain   lowercase   letters   with   their   uppercase   equivalents   in   generated   output  
  showpoint Generates   a   decimal-point   character   unconditionally   in   generated   floating-point   output  
  boolalpha Inserts   and   extracts   bool   type   in   alphabetic   format  
  unitbuf Flushes   output   after   each   output   operation  
  internal Adds   fill   characters   at   a   designated   internal   point   in   certain   generated   output.   If   no   such   point   is   designated,   it抯   identical   to   right.  
   
  left Adds   fill   characters   on   the   right   (final   positions)   of   certain   generated   output  
  right Adds   fill   characters   on   the   left   (initial   positions)   of   certain   generated   output  
  dec Converts   integer   input   or   generates   integer   output   in   decimal   base  
  hex Converts   integer   input   or   generates   integer   output   in   hexadecimal   base  
  oct Converts   integer   input   or   generates   integer   output   in   octal   base  
  fixed Generates   floating-point   output   in   fixed-point   notation  
  scientific Generates   floating-point   output   in   scientific   notation  
   
  resetiosflags  
  (ios_base::fmtflags   flag) Resets   the   fmtflags   field   flag  
  setiosflags  
  (ios_base::fmtflags   flag) Sets   up   the   flag   flag  
  setbase(int   base) Converts   integer   input   or   generates   integer   output   in   base   base.   The   parameter   base   can   be   8,   10   or   16.    
  setfill(char_type   c) Sets   the   character   used   to   pad   (fill)   an   output   conversion   to   the   specified   field   width  
  setprecision(int   n) Sets   the   precision   (number   of   digits   after   the   decimal   point)   to   generate   on   certain   output   conversions  
   
  setw(int   n) Sets   the   field   with   (number   of   characters)   to   generate   on   certain   output   conversions  
  endl Inserts   a   newline   character   into   the   output   sequence   and   flush   the   output   buffer.    
  ends Inserts   a   null   character   into   the   output   sequence.    
  flush Flush   the   output   buffer.    
   
  Default   Values  
   
  precision()                         6  
  width()                                 0  
  fill()                                   the   space   character  
  flags()                                 skipws   |   dec  
  getloc()                               locale::locale()  
   
  Example  
   
  //  
  //   cout   example   #1  
  //  
  #include<iostream>  
  #include<iomanip>  
  void   main   (   )  
  {  
      using   namespace   std;  
      int   i;  
      float   f;  
       
      //   read   an   integer   and   a   float   from   stdin  
      cin   >>   i   >>   f;  
      //   output   the   integer   and   goes   at   the   line  
      cout   <<   i   <<   endl;  
      //   output   the   float   and   goes   at   the   line  
      cout   <<   f   <<   endl;        
      //   output   i   in   hexa  
      cout   <<   hex   <<   i   <<   endl;  
      //   output   i   in   octal   and   then   in   decimal  
      cout   <<   oct   <<   i   <<   dec   <<   i   <<   endl;      
      //   output   i   preceded   by   its   sign  
   
      cout   <<   showpos   <<   i   <<   endl;  
      //   output   i   in   hexa  
      cout   <<   setbase(16)   <<   i   <<   endl;  
      //   output   i   in   dec   and   pad   to   the   left   with   character  
      //   @   until   a   width   of   20  
      //   if   you   input   45   it   outputs   45@@@@@@@@@@@@@@@@@@    
      cout   <<   setfill('@')   <<   setw(20)   <<   left   <<   dec   <<   i;  
      cout   <<   endl;  
      //   output   the   same   result   as   the   code   just   above  
      //   but   uses   member   functions   rather   than   manipulators  
      cout.fill('@');  
      cout.width(20);  
      cout.setf(ios_base::left,   ios_base::adjustfield);  
   
      cout.setf(ios_base::dec,   ios_base::basefield);  
      cout   <<   i   <<   endl;    
      //   outputs   f   in   scientific   notation   with  
      //   a   precision   of   10   digits  
      cout   <<   scientific   <<   setprecision(10)   <<   f   <<   endl;  
      //   change   the   precision   to   6   digits  
      //   equivalents   to   cout   <<   setprecision(6);  
      cout.precision(6);  
      //   output   f   and   goes   back   to   fixed   notation  
      cout   <<   f   <<   fixed   <<   endl;  
  }  
  //  
  //   cout   example   #2  
  //  
  #include   <iostream>  
  void   main   (   )  
  {  
      using   namespace   std;  
   
      char   p[50];  
      cin.getline(p,50);  
      cout   <<   p;        
  }  
  //  
  //   cout   example   #3  
  //  
  #include   <iostream>  
  #include   <fstream>  
  void   main   (   )  
  {  
      using   namespace   std;  
      //   open   the   file   "file_name.txt"  
      //   for   reading  
      ifstream   in("file_name.txt");  
       
      //   output   the   all   file   to   stdout  
      if   (   in   )    
          cout   <<   in.rdbuf();    
      else  
          {    
              cout   <<   "Error   while   opening   the   file";      
              cout   <<   endl;  
          }  
  }  
   
  Warnings  
   
  Keep   in   mind   that   the   manipulator   endl   flushes   the   stream   buffer.   Therefore   it   is   recommended   to   use   慭n?if   your   only   intent   is   to   go   at   the   line.   It   greatly   improves   performance   when   C   and   C++   streams   are   not   synchronized.  
   
  If   your   compiler   does   not   support   namespaces,   then   you   do   not   need   the   using   declaration   for   std.  
   
  See   Also  
   
  basic_ostream(3C++),   basic_iostream(3C++),   basic_filebuf(3C++),   cin(3C++),   cerr(3C++),   clog(3C++),   wcin(3C++),   wcout(3C++),   wcerr(3C++),   wclog(3C++)  
   
  Working   Paper   for   Draft   Proposed   International   Standard   for   Information   Systems--Programming   Language   C++,   Section   27.3.1    
   
  Standards   Conformance  
   
  ANSI   X3J16/ISO   WG21   Joint   C++   CommitteeTop

10 楼greatjzl()回复于 2005-04-05 09:36:13 得分 0

ftTop

相关问题

  • 循环输出??
  • 输出问题
  • cout输出!
  • 输入输出
  • applet 输出html
  • Excel输出
  • 输出问题?
  • 输出与GetProcAddress
  • Response.Write输出html
  • 转换输出

关键词

  • c++
  • ios
  • adjustfield
  • basefield
  • cin
  • cout
  • stdout
  • output
  • streams
  • using namespace std

得分解答快速导航

  • 帖主:huanglun

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo