标签:int namespace 笔记 名字 C++ using 空间 程序设计 标识符
目录
第一个程序"Hello world"
#include <iostream> //01hello.cpp
using namespace std;
int main(){
cout << "Hello world!" << endl;
return 0;
}
1.C++的编译器g++,如果使用gcc编译,需要带上 -std=c++0x,指定其使用标准C++的运行库
2.源文件扩展名:.cpp .cc .C .cxx .c++
-
最好使用.cpp
#include <iostream> //02hello.cc
using namespace std;
int main(){
cout << "Hello world!" << endl;
return 0;
}
3.头文件#include <iostream> iostream—— IO 流 (in out stream,输入输出流)
#include <iostream> //03hello.cpp 不使用名字空间
int main(){
std::cout << "Hello world!" << std::endl;
std::cout << 1919 << std::endl;
std::cout << 3.14 << std::endl;
std::cout << 2012 << "," << 1314 << "," << std::endl;
return 0;
}
-
C++中标准库文件一般都没有.h
-
当然也允许有.h
-
-
C++标准库中的内容放在一个叫做std名字空间中
-
using namespace std;
-
cout 对象 标准输出对象
-
cin 对象 标准输入对象
-
cerr 对象 标准错误输出对象
-
endl 对象 换行对象
-
-
#include <iostream> //04info.cpp
using namespace std;
int main(){
char name[40] = {};
cout << "input name:";
cin >> name;
int age = 0;
cout << "input age:";
cin >> age;
cout << "input salary:";
float salary = 0.0;
cin >> salary;
int score[3]={};
cin >> score[0] >> score[1] >> score[2];
cout << "name:" << name << ",age:" << age << ",salary:" << salary << endl;
cout << "score:" << score[0] << " " << score[1] << " " << score[2] << endl;
return 0;
}
-
< < 输出运算符 对应使用 cout cerr
-
> > 输入运算符 对应使用 cin
-
#include <stdio.h> <=> #include <cstdio> (两者相等价)
#include <stdio.h> //<cstdio> //05c.cpp
#include <iostream>
using namespace std;
int main(){
char name[40] = {};
int age = 0;
printf("input name and age:");
scanf("%s",name);
cin >> age;
cout << "name:" << name << endl;
printf("age:%d\n",age);
return 0;
}
名字空间 又称命名空间
1.C++语言对程序中的标识符(类型、函数、变量)按照某种逻辑,划分成若干个组(空间)
-
对标识符的逻辑划分
-
类似cin cout endl C++标准库的函数,对象,类型都位于std名字空间中
-
避免了名字的冲突
2.定义名字空间的语法
namespace 名字空间的名字{
//全局变量
//类型定义
//函数
}
-
::作用域限定符
3.使用名字空间
-
(1) :: 作用域限定符 名字空间名::标识符
-
表示明确访问是特定名字空间中的特定标识符
-
最麻烦
-
-
-
(2) 名字空间指令
-
using namespace 名字空间名;
-
该语句可以在任何地方
-
表示在该指令之后,对指令所指的名字空间中的所有标识符对当前作用域都可见,接下来都可以直接访问该名字空间中的标识符,不需要加作用域限定符(也可以加)
-
★对当前作用域可见 (全局变量
-
最省事
-
-
-
(3)名字空间声明
-
using 名字空间::标识符
-
将指定名字空间中的某个标识符(成员)引入到当前作用域,可以直接访问
-
★导入当前作用域 (局部变量
-
#include <iostream> //06namespace.cpp
using namespace std;
//定义名字空间
namespace wd1{
int g = 1024;
void func(void){
cout << "func" << endl;
}
struct Stu{
int no;
char name[40];
};
}
int main(){
using wd1::g;
cout << g << endl;
cout << wd1::g << endl;
wd1::func();
struct wd1::Stu s = {};
using namespace wd1;
cout << g << endl;
func();
struct Stu s2 = {};
return 0;
}
匿名空间
-
编译器为没有指明名字空间的全局区的标识符置于匿名名字空间中
-
如果要指明访问匿名名字空间中的标识符
-
直接用 ::标识符
-
-
:: 作用域限定符
-
名字空间名:: 标识符 访问指定名字空间中的标识符
-
:: 标识符 访问匿名名字空间中的标识符 可以区分同名的全局变量和局部变量
#include <iostream> //07nonamespace.cpp
using namespace std;
//如果一个全局的标识符没有指定名字空间,编译器将会为其缺省地置于匿名名字空间中
int gv = 1024;
int main(){
cout << gv << endl;
cout << ::gv << endl; //访问指定名字(匿名名字)空间中的标识符
int gv = 9527;
cout << gv << endl;//访问局部的 局部优先原则 同名的局部变量会隐藏同名的全局变量
cout << ::gv << endl;//访问全局的
return 0;
}
名字空间合并
-
一个程序中,如果名字空间名字相同,则认为是同一个名字空间
-
里面的标识符会进行合并
#include <iostream> //08namespacesame.cpp
using namespace std;
namespace myspace{
int x = 1024;
void func(){
cout << "func" << endl;
}
}
namespace myspace{
int y = 9527;
void bar(){
cout << "bar" << endl;
}
}
int main(){
using namespace myspace;
cout << x << endl;
cout << y << endl;
func();
bar();
return 0;
}
名字空间嵌套
-
一个名字空间包含另外一个名字空间
-
不能直接 using namespace 里层名字空间; 对于当前作用域而且是不可见的
-
using namespace 外层名字空间;
-
using namespace 里层名字空间;
-
using namespace 外层名字空间::里层名字空间;
-
#include <iostream> //09namespacecontain.cpp
using namespace std;
//int x = 1;
namespace s1{
int x = 1024;
namespace s2{
int y = 9527;
namespace s3{
int z = 1314;
}
}
namespace s4{
int x = 520;
}
}
int main(){
using s1::x;
cout << x << endl;
cout << s1::x << endl;
//cout << y << endl;
cout << s1::s2::y << endl;
using s1::s2::y;
cout << y << endl;
using namespace s1;
using s2::y;
//using namespace s3;
using namespace s1::s2::s3;
cout << z << endl;
using namespace s4;
cout << x << endl;//有两个名字空间中都有x 因为它们对当前作用域没有优先级关系 所以会有歧义
return 0;
}
-
注意:
-
如果用using namespace 名字空间;进行名字空间声明当前作用域可见
-
如果不同的两个作用域中含有相同名字和标识符,不访问没有问题
-
但是如果用直接访问,则会产生歧义
-
using namespace 名字空间; 进行声明不会有歧义,调用会有歧义
-
using 名字空间名::标识符 可能会有歧义
-
#include <iostream> //10namespace.cpp
using namespace std;
namespace s1{
int x = 1111;
}
namespace s2{
int x = 9527;
}
int main(){
cout << s1::x << endl;
cout << s2::x << endl;
using namespace s1;
cout << x << endl;
using namespace s2;
// cout << x << endl;//会有歧义
cout << s1::x << endl;
cout << s2::x << endl;
using s2::x; //导入到当前作用域
cout << x << endl;
cout << s1::x << endl;
//using s1::x;//当前作用域中有一个x了
return 0;
}
PS:名字空间适合在多文件中进行编程,每个组员使用不同的名字空间防止变量名or函数名冲突导致歧义,更方便的实现多文件多人共同编程
标签:int,namespace,笔记,名字,C++,using,空间,程序设计,标识符 来源: https://blog.csdn.net/m0_51080225/article/details/120374847
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。