c++ using namespace std可以不使用头文件使用吗_C到C++I

从C转向C++时,需要注意C++中使用`cout`和`cin`等标准库函数需要包含头文件 `<iostream>` 并使用 `using namespace std;`。`std` 是C++的标准命名空间,`::` 用于作用域限定符。文章还介绍了命名空间的作用、创建、使用方式以及命名空间的各种操作,包括声明、定义、合并、别名等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

5da60a244f0c4160b1b8405d976bd7d8.png

C到C++

f09f8a57dcb1875a07dd7005d18b4888.png

头文件

C风格

 #include<stdio.h>
 #include<math.h>

C++风格

 #include<iostream>
 #include<cstdio>    //C++风格 
 #include<cmath>     //math.h cmath

输入输出

 #include<stdio.h>
 #include<iostream>
 using namespace std;    //命名空间 名字空间两种叫法
 int main()
 {
     //输出
     printf("hello worldn");
     cout << "hello world" << endl;  //endl endline换行
     //输入
     int num;
     scanf("%d", &num);
     cin >> num;
     system("pause");
     return 0;
 }

说明

  • 不能直接使用cout,cin,像C一样,需要加上头文件,此外还要加上命名空间
 #include<iostream>
 //命名空间的使用
 //方式一:
 using namespace std;    
 //方式二
 using std::cout;
 using std::cin;
 using std::endl;
 //方式三
 //输出
 std::cout << "hello world" << std::endl;    //endl endline换行
  • 注意cout<<,cin>>里面的流操作符的方向
  • endl是n的意思,endline换行

命名空间

作用

作用:划分逻辑区域,解决名字冲突

创建

namespace 名字空间{}
 //1.创建名字空间
 namespace DeRoy
 {
     void fun()
     {
         cout << "我是DeRoy的fun函数" << endl;
     }
 }

使用

::作用域限定符

 #include<iostream>
 int main()
 {
     std::cout << "hello world" << std::endl;
     return 0;
 }

名字空间声明

using 名字空间::成员
 #include<iostream>
 using std::cout;
 using std::cin;
 using std::endl;
 namespace DeRoy
 {
     void fun()
     {
         cout << "我是DeRoy的fun函数" << endl;
     }
 }
 using DeRoy::fun;//DeRoy空间里面的fun函数全局可见
 int main()
 {
     cout << "hello world" << endl;
     fun();  //调用fun函数
     return 0;
 }

名字空间指令

using namespace 名字空间
 #include<iostream>
 using namespace std;    //命名空间 名字空间
 namespace DeRoy
 {
     void fun()
     {
         cout << "我是DeRoy的fun函数" << endl;
     }
 }
 using namespace DeRoy;
 int main()
 {
     cout << "hello world" << endl;
     fun();
     return 0;
 }

命名空间合并

#include<iostream>
using namespace std;	//命名空间 名字空间
namespace DeRoy
{
	void fun()
	{
		cout << "我是DeRoy的fun函数" << endl;
	}
}
namespace DeRoy	    //命名空间合并     同名空间合并
{
	void test()
	{
		cout << "我是DeRoy的test函数" << endl;
	}
}
int main()
{
	DeRoy::fun();
    DeRoy::test();
    return 0;
}

声明和定义分开

#include<iostream>
using namespace std;	//命名空间 名字空间
namespace DeRoy	    //命名空间合并     同名空间合并
{
	void test();
}
void DeRoy::test()	//命名空间成员函数 声明和定义分开
{
	cout << "我是DeRoy的out函数" << endl;
}
int main()
{
    DeRoy::test();
    return 0;
}

命名空间嵌套

//命名空间嵌套
namespace ShanXi
{
	namespace XiAn
	{
		namespace ChangAn
		{
			void SchoolName()
			{
				cout << "西北工业大学" << endl;
			}
		}
	}
}

命名空间别名

namespace Changan = ShanXi::XiAn::ChangAn;

来源:

C到C++​mp.weixin.qq.com
586a46d4c5cdd32c6fa768f0e0ce99f0.png

附C++基础视频资料:

链接:https://pan.baidu.com/s/1ECW2fpv5d5AB-gQFaycx4A

提取码:g86t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值