注册 登录
编程论坛 C++教室

在C++编程里,什么时候用“#include<iostream> using namespace std;”?什么时候又用“#include <

bc574479449 发布于 2010-11-01 14:25, 6478 次点击
(一)、#include <iostream.h>
using namespace std;
class point
{
public:point(int x1=0,int y1=0):x(x1),y(y1){}
       friend ostream& operator<<(ostream&, const point&);
      
       friend istream& operator>>(istream&,point&);
       int x,y;     
};
ostream& operator<<(ostream& out, point& src)
{
    out<<"<"<<src.x<<","<<src.y<<">";
    return out;
}
istream& operator>>(istream& in,point& target)
{
    in>>target.x>>target.y;
    return in;
}
int main()
{
    point a(1,2);
    cin>>a;
    cout<<a;
    return 0;
}
(二)、#include <iostream.h>
class point
{
    int x,y;
public:
    point(int x1=0,int y1=0):x(x1),y(y1){}      
    friend ostream& operator<<(ostream&, const point&);     
    friend istream& operator>>(istream&, point&);
};
ostream& operator<<(ostream& out, const point& src)
{
    out << "<" << src.x << "," << src.y << ">";
    return out;
}

istream& operator>>(istream& in,point& target)
{
    in>> target.x >> target.y;
    return in;
}
int main()
{
    point a(1,2);
    cin>>a;
    cout<<a;
    return 0;
}
为什么换个头文件“#include <iostream.h>”成
(二)就能够运行!??
9 回复
#2
bc5744794492010-11-01 14:26
在C++编程里,什么时候用“#include<iostream> using namespace std;”?什么时候又用“#include <iostream.h>”?
#3
gaoce2272010-11-01 14:52
#include<iostream.h>//C的头文件
#include<iostream>//C++的头文件

using namespace std;//使用命名空间


#4
2010-11-01 20:46
  好像有的编译器对C++不怎么支持,你用#include<iostream>编译有错,但改为#include<iostream.h>就能运行吧,
反正以后你都试试就行了,不过最好用对C++编译兼容的比较好vs和vc++都不错。
#5
pangding2010-11-01 20:51
推荐在任何时候都用:
#include <iostream>
using namespace std;
后面那个 using 語句,如果不想引入 std 名空间的话,就不要用。之后用里面东西的时候加上名空间限定就行了,像 std::cin 这样。
#6
jookmmmm2010-11-02 23:09
编译器问题
#7
qshzh1022010-11-03 14:50
只需要将(一)中的头文件换成#include<iostream> using namespace std;即可
#8
weiqiang2010-11-03 15:27
C++中最好用#include <iostream>
using namespace std;
#9
ytfsksk2010-11-03 17:04
#include "iostream"
using namespace std;
class point
{int x,y;
public:
       point(int x1=0,int y1=0):x(x1),y(y1){}
       friend ostream& operator<<(ostream&, const point&);
       friend istream& operator>>(istream&,point&);
};
ostream& operator<<(ostream& out, const point& src)
{ cout<<'<'<<src.x<<','<<src.y<<'>';
return out;
}
istream& operator>>(istream& in,point& target)
{ cin>>target.x>>target.y;
return in;
}
int main()
{point a(1,2);
cout<<"请输入:"<<endl;
cin>>a;
cout<<a;
return 0;
}
这个不是标准库引用的问题,这两种没有很大的区别看个人习惯。使用命名空间,最好用最新编译器
#10
swp1601082010-11-05 09:27
vc6对std支持不太好
1