zoukankan      html  css  js  c++  java
  • HelloWorld! C++纠错版

    例题:
    1
    #include<iostream> 2 3 int main() 4 { 5 cout << "HelloWorel!" ; 6 return 0; 7 }

    1 #include <iostream>
    2    using namespace std;
    3    int main()  //c++ programs start by executing the function main
    4    {
    5          cout << "HelloWorld!
    "<< endl; //output"HelloWorld!"
    6          return 0;
    7  }

    编译通过。

    疑问:

    书上的例题用VC++编译

    我在Dev-c++编译一样报错

    using namespace std;干嘛用的?为毛跟书上的列题不一样?

    查查资料后了解得知:
     #include<iostream.h> 非标准输入输出流,这个写法是以前C语言的写法,上个世纪八九十年代的书中一般采用这种写法,现在已经不适用了。
    iostream.h时代没有名词空间,即所有库函数包括头文件iostream.h都声明在全局域。为了体现结构层次,避免名字定义冲突,

    c++标准委员会特别引入了“名字空间的定义”,即namespace。引入了名词空间这一概念,并把所有库函数声明由全局域改到了名词空间std。
    所以,新的标准是:

    1 #include <iostream>  //标准输入输出流
    2 using namespace std;

    (因为iostream声明在std中,故而要加上这句,除非你不用库函数,否则错误);

    很多编译器都同时支持这两种头文件形式,更好的当然是标准头文件。至于为什么不废除非标准头文件,大概是为了兼容以前的代码吧。

    还有一点在标准c++中,所有库函数都没有.h后缀了,如果是c语言的库函数,则去掉后缀,并在开头加上一个c。

    如下:

    #include cstdio  //stdio.h
    #include cstring //string.h  

    使用<iostream>时,引入std::有以下方法:

    1 using namespace std;
    3 cout<<"Hello c++!"<<endl;
    4 
    5 using std::cout;
    6 cout<<"Hello c++!";
    7 
    8 std::cout<<"Hello c++!";
  • 相关阅读:
    IOC理论推导
    spring leile
    缓存
    动态SQL
    canvas小球运动
    jdk1.7后字符串的总结
    用ssm框架简单创建一个增删改查案列
    京东物流居家品类各区域联系人
    京东网址收藏
    京东自营申请新品打标方法
  • 原文地址:https://www.cnblogs.com/Charons/p/11167624.html
Copyright © 2011-2022 走看看