C++ const,static,extern,rand()等一些例题分析
1、常量定义(const)
例:const int LENGTH = 10;
2、静态变量(static):
静态局部变量有以下特点:
该变量在全局数据区分配内存;
静态局部变量在程序执行到该对象的声明处时被首次初始化,即以后的函数调用不再进行初始化;
静态局部变量一般在声明处初始化,如果没有显式初始化,会被程序自动初始化为0;
它始终驻留在全局数据区,直到程序运行结束。但其作用域为局部作用域,当定义它的函数或语句块结束时,其作用域随之结束;
1 #include<iostream>
2 void func(void);
3 static int count = 10;
4 int main()
5 {
6 while (count--)
7 {
8 func();
9 }
10 return 0;
11 }
12 void func(void) {
13 static int i = 5;
14 i++; std::cout << "变量i为" << i;
15 std::cout << ",变量count为" << count<< std::endl;
16 }
结果:
变量i为6,变量count为9 变量i为7,变量count为8
变量i为8,变量count为7
变量i为9,变量count为6
变量i为10,变量count为5
变量i为11,变量count为4
变量i为12,变量count为3
变量i为13,变量count为2
变量i为14,变量count为1
变量i为15,变量count为0
变量i为8,变量count为7
变量i为9,变量count为6
变量i为10,变量count为5
变量i为11,变量count为4
变量i为12,变量count为3
变量i为13,变量count为2
变量i为14,变量count为1
变量i为15,变量count为0
---------------------------------
static int i = 5;
改为 int i = 5;
改为 int i = 5;
---------------------------------
结果:
变量i为6,变量count为9
变量i为6,变量count为8
变量i为6,变量count为7
变量i为6,变量count为6
变量i为6,变量count为5
变量i为6,变量count为4
变量i为6,变量count为3
变量i为6,变量count为2
变量i为6,变量count为1
变量i为6,变量count为0
结果:
变量i为6,变量count为9
变量i为6,变量count为8
变量i为6,变量count为7
变量i为6,变量count为6
变量i为6,变量count为5
变量i为6,变量count为4
变量i为6,变量count为3
变量i为6,变量count为2
变量i为6,变量count为1
变量i为6,变量count为0
3、extern 可置于变量或者函数前,以表示变量或者函数的定义在别的文件中。另外,extern也可用来进行链接指定。
例:
main.ccp:
1 #include <iostream>
2 extern int count;
3 void write_extern(void)
4 {
5 std::cout << "Count is " << count << std::endl;
6 }
support.cpp:
1 #include <iostream>
2 extern int count;
3 void write_extern(void)
4 {
5 std::cout << "Count is " << count << std::endl;
6 }
4、生成随机数rand()
1.srand函数是随机数发生器的初始化函数。原型:void srand(unsigned seed);
(1)为了防止随机数每次重复,常常使用系统时间来初始化,即使用 time函数来获得系统时间,然后将time_t型数据转化为(unsigned)型再传给srand函数,即:srand((unsigned) time(&t))
(2)还有一个经常用法,不需要定义time_t型t变量, 即: srand((unsigned)time(NULL)); 直接传入一个空指针
1 #include <iostream>
2 #include <time.h>
3 using namespace std;
4 int main() {
5 int i, j;
6 srand((unsigned)time(NULL));
7 for (i = 0; i < 10; i++){
8 j = rand();
9 std::cout << j << std::endl;
10 }
11 return 0;
12 }
注:计算机并不能产生真正的随机数,而是已经编写好的一些无规则排列的数字存储在电脑里,把这些数字划分为若干相等的N份,并为每份加上一个编号用srand()函数获取这个编号,然后rand()就按顺序获取这些数字,当srand()的参数值固定的时候,rand()获得的数也是固定的,所以一般srand的参数用time(NULL),因为系统的时间一直在变,所以rand()获得的数,也就一直在变,相当于是随机数了。(原文链接:https://blog.csdn.net/jx232515/article/details/51510336)
5、string str ("guozehui");跟string str = "guozehui";区别
(1)str.c_str()可以获取这个字符串的首地址
(2)string str = "ABC"是使用"ABC"为值来初始化一个string类
6、函数定义中return_type,当无返回值时,用void
如:int namestr(void)
7、使用time_t类型变量,如:time_t t;
8、迭代
1 #include <iostream>
2 using namespace std;
3 int age(int);
4 int main(void)
5 {
6 cout << age(5) << endl;
7 return 0;
8 }
9 int age(int n)
10 {
11 int c;
12 if (n == 1)
13 {
14 c = 10;
15 }
16 else
17 {
18 c = age(n - 1) + 2;
19 }
20 return c;
21 }
分析:
当 n = 1:c = 10;
当 n ≠ 1:c=age(1)+2*(n-1)
本文来自博客园,作者:꧁执笔小白꧂,转载请注明原文链接:https://www.cnblogs.com/qq2806933146xiaobai/p/12201265.html
分类:
其他开发语言-C/C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!