关于命名空间的理解---iostream与iostream.h的区别

C++中为了避免名字定义冲突,特别引入了“名字空间的定义”,即namespace。当代码中用<iostream.h>时,输出可直接引用cout<<x;<iostream.h>继承C语言的标准库文件,未引入名字空间定义,所以可直接使用。

当代码中引入<iostream>时(C++标准),输出需要引用std::cout<<x;如果还是按原来的方法就会有错,或者直接添加using namespace std;

实例:

code1

#include "stdafx.h"
#include <iostream>
using namespace std;

int main(void)
{
	int cout=100;								//cout被覆盖

	cout<<cout;									//整数cout左移100位,不起任何作用
	system("pause");
	return 0;
}

code2

#include "stdafx.h"
#include <iostream>

int main(void)
{
	int cout=100;

	std::cout<<cout<<std::endl;		//名字空间中取出函数
	system("pause");
	return 0;
}

code3

#include <stdlib.h>
#include <iostream.h>

int main(void)
{
	int a=10;
	cout<<a<<endl;

	int cout=100;		//名字cout被覆盖
	cout<<20;

	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值