当前位置:   article > 正文

(C++学习笔记一)命名空间 常量 字符串_namespace 里定义常量

namespace 里定义常量

3.1.有关于命名空间 namespace :

3.1.1. 简述:

  • 1. 在C++语言编写的程序中,变量和函数等的作用范围是有一定限制的。比如,在函数体中定义的一个临时变量就不可以在函数体外使用。为了解决变量和函数等的作用范围,在C++语言中引入了名空间的概念,并增加了关键字namespace和using.
  • 2.在一个名空间中可以定义一组变量和函数,这些变量和函数的作用范围一致,可以将这些变量和函数称为这个名空间的成员。
  • 3.通过名空间,可以在同一个文件中使用相同的变量名或函数名,只要它们属于不同的名空间。另外,名空间可以使得代码操作具有相同名字但属于不同库的变量。而且,名空间也可以提高C语言与C++语言的兼容性。

3.1.2.本体

1.using namespace std;//C++标准名称空间
  • 未引用标准名称空间时需在关键字前添加 std::
// using namespace std;//C++标准名称空间
 std::cout <<" a = "<< a <<  std::endl;
  • 1
  • 2

注意:这是一个C++标准名称空间,如果将之注释掉,很多C++关键字就会出错!

  • 引用时
using namespace std;//C++标准名称空间
cout <<" a = "<< a << endl;
  • 1
  • 2
2.常规应用
#include<iostream>

using namespace std;//C++标准名称空间

int a = 10;

namespace ns//类似于C语言结构体
{
	int a = 2;
	char s = 'f';
	int b = 9;
}

int main()
{
    cout <<" a = "<< a << endl;
	cout << ns::a << endl;
	cout << ns::s << endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3.2.system("pause"); :

  • 在C++中一般在main函数中的return之前添加system(“pause”);这样就可以看清楚输出的结果,pause会输出"press any key to continue. . ."。
    `

3.3.常量

C++定义常量两种方式:

3.3.1. #define宏常量#define 常量名 常量值

  • 通常在文件上方定义,表示一个常量,不可修改.

3.3.2. const修饰的变量const 数据类型 常量名 = 常量值

  • 通常在变量定义前加关键字const,修饰该变量为常量,不可修改.

#define day 7//宏定义
const int month = 12;

  • 1
  • 2
  • 3
  • 4

3.4. 字符串定义

char site1[7] = {'R', 'U', 'N', 'O', 'O', 'B'};
char site2[] = "RUNOOB";
string site3 = "runoob";
  • 1
  • 2
  • 3

3.5. 总结

#include<iostream>
using namespace std;//C++标准名称空间

void name(int nt);//声明函数 
void swap1(int* p1, int* p2);//输入两数指针,并使用解指针对两者数值进行交换

#define stc 10

int a = 10,day = 365;
int b = 0,c = 0;

namespace ns//类似于结构体
{
	int a = 11;
	char s = 'f';
	int b = 9;
	namespace tr//命名空间嵌套
	{
		int a = 12;
	}
}

struct score//成绩结构体 嵌套
{
	int SA;
	int SB;
};

struct staff//员工结构体
{
	string name;//姓名
	int age = 1;//年龄  //若不初始化将会有警告
	struct score sc = {1,1};//若不初始化将会有警告
};


char s[10] = { 'q','w','e','t' };//C风格字符串
char str1[10] = "RUNOOB";//C风格字符串
string str2[] = { "good" };//输出信息不对❌
string str3= "hello world";//C++风格字符串,需要加入头文件 string

bool asa = true;//bool 可直接赋值:true/false 1/0 当赋值不等于0或1时默认为1

int main()
{
	const int dc = 13;//const 修饰常量
//	double dou1;
	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int* arr_zz = &arr[4];
	int* arr_zz3 = &arr[3];
	int* p = arr;

	staff S1;//定义结构体变量
	staff S2 = { "MO",18,{1,2} };//定义结构体变量
	staff* S2_zz = &S2;

	cout << "a = " << a     << endl;
	cout << day    << " 天" << endl;
	cout <<  s     << endl;

	cout << "名称空间" << endl;
	cout << ns:: a    << endl;
	cout << ns::s     << endl;
	cout << ns::tr::a << endl;

	cout << "字符串" << endl;
	cout << "s = " << s << endl;
	cout << "\r\nstr1 = "  << str1 << endl;
	cout << "\r\nstr2 = " << str2 << endl;
	cout << "\r\nstr3 = " << str3 << endl;
	cout << "\r\nsizeof(s) = " << sizeof(s) << "\r\nsizeof(str1) = " << sizeof(str1) << endl;
	cout << "\r\nsizeof(str2) =" << sizeof(str2) << endl;

	asa = false;
	cout << "bool型变量"  << endl;
	cout << "asa = " << asa  << endl;
	cout << "sizeof(asa) = " << sizeof(asa) << "\r\n" << endl;

//	cout << "输入浮点型变量:" << endl;
//	cin >> dou1;
//	cout << "dou1 = " << dou1 << endl;
//	cout << "sizeof(dou1) = " << sizeof(dou1) << "\r\n" << endl;

	a = 0;
	a += 2;//a = a + 2;
	cout << "加等 \r\na = 0; a += 2;a = " << a << "\r\n" << endl;
	// +=,*=,-=,%=,/=;  变量 += 常量;  


	a = 10;
	b = 14;
	cout << "比较符" << endl;
	cout << (a > b) << "\r\n" << endl;//真假
	//比较符 == , != , < , > , <= , >= . 

	b = 12;
	c = (a < b) ? 1 : 0;
	cout << "三目运算符(a=10; b=12)\r\n" << "a < b ?  c = " << c << "\r\n" << endl;
	c = (a > b) ? 1 : 0;
	cout << "a > b ?  c = " << c << "\r\n" << endl;
	cout << "a > b ? " << endl;
	a > b ? cout << "true" << endl : cout << "false" << endl;//这里有一个需要注意的地方,注意语句结束的地点.
	cout << "\r\n" << endl;

	cout << "输出实验\r\n\r\n";
	for (a = 0; a < 10; a++)
	{
		for (b = 0; b < 10; b++)
		{
			cout << "*" << " ";//如果添加 <<endl 的话会执行换行
		}
		cout << endl;//换行,移位
	}
	cout << endl;//换行,移位

	//continue与break的区别:
	//continue: 结束本次循环,不执行本次循环余下程序.
	//break   : 结束整个循环.

	for (int a = 0; a < 100; a++)
	{
		if (a % 2 == 0)
		{
			continue;
		}
		cout << a << endl;
	}
	cout << endl;

	c = (sizeof(arr) / sizeof(int))-1;//
	cout << "arr[10] = {";
	for (; c > -1; c--)
	{
		cout << arr[9-c] << ",";
	}
	cout << "};\r\n";
	cout << "数组实验: \r\n" << "\r\n1.获取数组所占内存空间\r\n";
	cout << "sizeof(arr) = " << sizeof(arr) << endl;
	cout << "2.获取数组首地址\r\n" << "&arr = " << &arr << endl;//错误方法
	cout << "(int)&arr[0] = " << (int)&arr[0] << endl;
	cout << "(int)&arr[1] = " << (int)&arr[1] << endl;

	name(100);

	cout << endl << "指针实验" << endl;
//	int* arr_zz = &arr[0];
//	int* a_zz = &a;
	cout << "打印地址(arr[4]):" << "&arr[4] = " << &arr[4] << "    arr_zz = " << arr_zz << endl;
	cout << "打印地址(arr[3]:" << "&arr[3] = " << arr_zz3 << endl;
	cout << "*arr_zz= " << *arr_zz << "//`*`又称为解引用" << endl;// 通过* 操作指针变量指向的内存
	cout << "打印数组arr的首地址:" << "arr = " << arr << "   p = arr = " << p << "   &arr[0] = " << &arr[0] << endl;
	cout <<"指针各个类型所占内存大小:" << endl << "int*" <<sizeof(arr_zz) << endl;
	cout << "sizeof(char*) = " << sizeof(char*) << endl;
	cout << "sizeof(float*) = " << sizeof(float*) << endl;
	cout << "sizeof(double*) = " << sizeof(double*) << endl << endl;
	cout << "数组p = arr指针自加实验" << endl;
	for (a = 0; a < 10; a++)
	{
		cout << "a = " << a << endl
			<< "地址: " << p << endl
			<< "解地址:" << *p << endl;
		p++;
	}
	cout << "输入两数指针, 并使用解指针对两者数值进行交换:" << endl;
	a = 178;
	b = 14;
	cout << "a = " << a << "    b = " << b << endl;
	swap1(&a,&b);
	cout << "a = " << a << "    b = " << b << endl;
	cout << endl << endl;

	cout << "Struct Experiment" << endl;

	cout << "name S1  :" << S1.name << endl
		<< "    " << S1.age << endl
		<< "    " << S1.sc.SA << endl
		<< "    " << S1.sc.SB << endl;

	cout << "name S2  :" << S2.name << endl
		<< "    " << S2.age << endl
		<< "    " << S2.sc.SA << endl
		<< "    " << S2.sc.SB << endl;

	S1.name = "N1";
	S1.age = 23;
	S1.sc.SA = 250;
	S1.sc.SB = 50;
	S2_zz->name = "N2";
	S2_zz->age = 22;
	S2_zz->sc.SA = 22;
	S2_zz->sc.SB = 22;
	cout << "`.`赋值 name S1  : " << endl << S1.name << endl
		<< "    " <<S1.age << endl
		<< "    " << S1.sc.SA << endl
		<< "    " << S1.sc.SB << endl;
	cout << "`->`赋值 name S2  : " << endl << S2.name << endl
		<< "    " << S2.age << endl
		<< "    " << S2.sc.SA << endl
		<< "    " << S2.sc.SB << endl;
	cout << "staff* S2_zz = &S2 = " << S2_zz << endl;


	system("pause");
	return 0;
}

void name(int nt)
{
	int we = 0;
	we = ns::a + nt;
	cout << "\r\n调用函数 name :" << endl;
	cout << "we = ns::a + nt = " << we << "\r\nns::a = " << ns::a << "\r\nnt = " << nt << endl;
}

void swap1(int* p1, int* p2)
{
	int temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/996379
推荐阅读
相关标签
  

闽ICP备14008679号