目录
(一)命名空间
1、命名空间的使用
为什么需要命名空间?
如何简化命名空间的使用?
使用 using 声明
引入特定的对象
2、自己编写命名空间
情况一:两个命名空间内变量名重复
情况二:命名空间与全局变量冲突
情况三:命名空间与局部变量冲突
3、命名空间封装函数
4、命名空间嵌套
5、作用域运算符 ::
总结
(二)格式化输出
你可能已经注意到,我们的程序中使用的是 std::cout
和 std::cin
,而不是直接写 cout
和 cin
。这是因为 C++ 中的所有标准库内容都放在了一个叫 std
的命名空间里。命名空间的作用是为了避免不同代码之间的名字冲突。
想象一下,如果有两个人都用“张三”这个名字,大家会混淆他们是谁。命名空间就像在名字前面加上姓氏一样,帮助区分不同的功能。例如,std::cout
表示标准库中的 cout
,而 cout
可能在其他地方有不同的定义。
如果你觉得每次都写 std::
太麻烦,可以使用下面两种方法:
using
声明在程序开始的地方加上:
using namespace std;
这样,你就可以直接写 cout
、cin
,而不用每次都加 std::
了。例如:
#include
using namespace std;
int main() {
cout << "请输入两个数:" << endl;
int a, b;
cin >> a >> b;
cout << "a + b = " << a + b << endl;
return 0;
}
如果你只想简化某些部分的代码,也可以只引入需要的部分:
using std::cout;
using std::cin;
using std::endl;
这样你就只需要给这些部分去掉 std::
,其他的内容不受影响。
在 C++ 中,我们可以自己定义命名空间,将相关的变量、函数等封装到一个命名空间里,避免与其他部分的代码产生冲突。定义命名空间的基本格式如下:
namespace 命名空间名{ 数据类型1 变量1; 数据类型2 变量2; ... 数据类型n 变量n; }
下面通过三个例子来展示在不同情况下如何使用命名空间:
当两个命名空间中定义了同名的变量时,直接使用变量名会产生歧义。为了解决这个问题,我们需要通过命名空间名来区分变量。
#include
#include
using namespace std;
// 定义两个命名空间
namespace test_1 {
char name[20];
int age;
}
namespace test_2 {
char name[20];
int age;
}
// 使用两个命名空间
using namespace test_2;
using namespace test_1;
int main()
{
// 由于两个命名空间的变量名重复,不能直接使用变量名,否则会产生歧义。
// 需要使用“命名空间名::变量名”的方式来访问。
test_1::age = 18;
test_2::age = 30;
cout << "test_1::age = " << test_1::age << endl;
cout << "test_2::age = " << test_2::age << endl;
return 0;
}
当命名空间中的变量与全局变量发生冲突时,通常使用作用域运算符 ::
来访问全局变量,避免歧义。
这种方式我们称为 匿名空间 或者 域调用
#include
#include
using namespace std;
// 定义两个命名空间
namespace test_1 {
char name[20];
int age;
}
namespace test_2 {
char name[20];
int age;
}
using namespace test_1;
using namespace test_2;
// 全局变量
int age;
int main()
{
test_1::age = 18;
test_2::age = 30;
// 使用 "::age" 访问全局变量,避免与命名空间中的变量冲突 <匿名空间> <域调用>
::age = 50;
cout << "test_1::age = " << test_1::age << endl;
cout << "test_2::age = " << test_2::age << endl;
cout << "Global age = " << ::age << endl;
return 0;
}
当命名空间中的变量与局部变量发生冲突时,程序会优先使用局部变量。此时,可以通过命名空间名来明确使用命名空间中的变量。
#include
#include
using namespace std;
// 定义两个命名空间
namespace test_1 {
char name[20];
int age;
}
namespace test_2 {
char name[20];
int age;
}
// 使用两个命名空间
using namespace test_2;
using namespace test_1;
int main()
{
test_1::age = 18;
test_2::age = 30;
// 局部变量
int age;
age = 50; // 优先使用局部变量
cout << "test_1::age = " << test_1::age << endl;
cout << "test_2::age = " << test_2::age << endl;
cout << "Local age = " << age << endl;
return 0;
}
在 C++ 中,命名空间不仅可以封装变量,还可以封装函数。这种方式可以将不同功能模块分开,避免命名冲突。通过命名空间封装函数,我们可以轻松管理同名的函数、变量,使代码结构更加清晰。
下面通过一个示例展示如何将函数封装在命名空间中,并解释不同情况下的使用方法。
示例代码:
#include
#include
using namespace std;
// 命名空间问题
namespace test_1 {
char name[20];
int age;
int my_add(int x, int y); // 函数声明
}
namespace test_2 {
char name[20];
int age;
int my_add(int x, int y); // 函数声明
}
// 函数定义
int test_1::my_add(int x, int y)
{
cout << "test_1::my_add: " << x + y << endl;
return x + y;
}
int test_2::my_add(int x, int y)
{
cout << "test_2::my_add: " << x + y << endl;
return x + y;
}
int main()
{
// 访问命名空间中的变量
test_1::age = 18;
test_2::age = 30;
// 局部变量
int age;
age = 50; // 局部变量优先使用
// 调用命名空间中的函数
int result1 = test_1::my_add(5, 10); // 调用 test_1 的函数
int result2 = test_2::my_add(7, 20); // 调用 test_2 的函数
cout << "局部变量 age = " << age << endl;
cout << "test_1::age = " << test_1::age << endl;
cout << "test_2::age = " << test_2::age << endl;
return 0;
}
在 C++ 中,命名空间不仅可以独立存在,还可以嵌套在其他命名空间中。嵌套命名空间的作用是将更细粒度的功能模块分组管理。通过命名空间的嵌套,我们可以进一步组织代码,避免命名冲突。
#include
#include
using namespace std;
// 命名空间问题
namespace group {
int value;
namespace zhangsan {
int value;
}
namespace lisi {
int value;
}
}
using namespace group;
int main()
{
// 访问问题
lisi::value = 60;
zhangsan::value = 50;
return 0;
}
::
在 C++ 中,::
被称为作用域运算符,它用于告诉编译器我们想要访问哪个范围内的某个变量、函数或者类。简单来说,它可以帮助我们明确指定要使用的对象在哪个地方定义的。
常见的使用场景有以下几种:
命名空间中的使用
就像前面提到的,我们用 std::cout
来表示标准库中的 cout
。这里的 ::
就表示我们正在使用 std
命名空间里的 cout
。
std::cout << "Hello, World!" << std::endl;
这里的 ::
明确告诉编译器:我们要用的是 std
命名空间里的 cout
和 endl
。
全局作用域
如果在局部作用域(如函数内部)中,存在与全局变量同名的变量,可以使用 ::
来访问全局变量。例如:
int num = 10; // 全局变量
int main() {
int num = 5; // 局部变量
std::cout << "局部 num: " << num << std::endl; // 输出 5
std::cout << "全局 num: " << ::num << std::endl; // 使用 :: 访问全局 num,输出 10
return 0;
}
这里的 ::num
表示全局变量 num
,避免和局部变量 num
冲突。
类中的使用
当类有多个成员函数或成员变量时,作用域运算符也用于区分类中的具体成员。例如:
class MyClass {
public:
static int value;
};
int MyClass::value = 0; // 使用 :: 来定义类的静态成员
这里 MyClass::value
表示的是 MyClass
类的静态成员 value
。
命名空间:使用 `std::cout` 时的 `::` 表示使用 `std` 命名空间中的 `cout`。
全局变量:可以用 `::` 来区分全局变量和局部变量。
类中的使用:`::` 用于访问类的成员函数或静态变量。
#include "iostream"
#include "iomanip"
using namespace std;
int main()
{
int num = 99;
cout << num << endl;
// 输出不同进制的值
cout << dec << num << endl; // 10进制输出
cout << hex << num << endl; // 16进制输出
cout << oct << num << endl; // 8 进制输出
// setbase 可以设置输出进制 8 10 16
cout << setbase(8) << num << endl;
// 字符串操作
// char *str_ptr = "hello"; 错误的 C++ 中不允许
const char *str_ptr = "hello";
// 打印字符串
cout << str_ptr << endl;
// 打印地址
cout << static_cast (str_ptr) << endl;
// 浮点型
double pi = 3.1415926;
cout << pi << endl;
// 输出 3 位
cout << setprecision(3) << pi << endl;
// 输出 4 位
cout << setprecision(8) << pi << end
/* 上述所有的输出方式都比较复杂 记不住 直接使用 printf 是兼容的 */
return 0;
}