C++ 实现一个函数,函数完成如下功能: 1.函数的输入为一个数组,数组的成员个数不定(即:可能为 0 个,也可能为多个) 2.函数找到成员的最大元素和最小元素,并能让函数的调用者知道最大元素和最小元素 是哪一个
摘要:#include <iostream> #include <string> #include <windows.h> using namespace std; bool find_max_min(int a[], int len, int* max, int* min) { if (len < 1)
阅读全文
posted @
2022-10-09 14:23
wshidaboss
阅读(43)
推荐(0) 编辑
有一个整形数组,a[3] = {7,2,5},要求使用指针实现数组成员由小到大的顺序排列,即结果为:a[3] = {2,5,7};
摘要:#include <iostream> #include <string> #include <windows.h> using namespace std; void swap(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } int mai
阅读全文
posted @
2022-10-08 16:52
wshidaboss
阅读(23)
推荐(0) 编辑
C++ 实现含有中文字符的字符串逆转,如: “我是小萌新” 转换成“新萌小是我”
摘要:设指针p1指向初始字符串s,再新建一个字符串tmp用来存放逆转后的字符。此时可以把p1和tmp看成两条链表,指针p2为实现逆转的中转站,p2的长度为tmp和s的长度之和,接下来就通过链表的特性把p1和p2的值进行逆转。 #include <iostream> #include <string> #i
阅读全文
posted @
2022-10-04 16:52
wshidaboss
阅读(121)
推荐(0) 编辑
C++ 声明const引用(常引用)
摘要:语法:const Type& name = var; const 引用让变量拥有只读属性 1)const & int e 相当于 const int * const e 2)普通引用 相当于 int *const e1 3)当使用常量(字面量)对const引用进行初始化时,C++编译器会为常量值分配
阅读全文
posted @
2022-09-23 15:17
wshidaboss
阅读(113)
推荐(0) 编辑
C/C++ 使用四种方法实现交换两个数(包括不使用第三个变量) 引用作为函数的参数
摘要:#include <iostream> #include <string> #include <windows.h> using namespace std; //方式一 使用指针 void swap1(int* a, int* b) { //指针作为函数的参数 int tmp = *a; *a =
阅读全文
posted @
2022-09-22 17:52
wshidaboss
阅读(1200)
推荐(0) 编辑
C++ void类型指针
摘要:void => 空类型 void* => 空类型指针,只存储地址的值,丢失类型,无法访问,要访问其值,我们必须对这个指针做出正确的类型转换,然后再间接引用指针。 所有其它类型的指针都可以隐式自动转换成void类型指针,反之需要强制转换。 #include <iostream> #include <s
阅读全文
posted @
2022-09-22 17:16
wshidaboss
阅读(53)
推荐(0) 编辑
C++ 数组与指针的区别
摘要:数组:数组是用于储存多个相同类型数据的集合。 指针:指针是一个变量,但是它和普通变量不一样,它存放的是其它变量在内存中的地址。 1. 赋值 数组:只能一个一个元素的赋值或拷贝 指针:指针变量可以相互赋值 2. 表示范围 数组有效范围就是其空间的范围,数组名使用下表引用元素,不能指向别的数组 指针可以
阅读全文
posted @
2022-09-17 17:03
wshidaboss
阅读(362)
推荐(0) 编辑
C++ 指针数组和数组指针(存储指针的数组和存储数组的指针)
摘要:1.指针数组的应用: int* qishou[2];//定义一个有两个元素的指针数组,每个元素都是一个指针变量 #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { i
阅读全文
posted @
2022-09-17 13:11
wshidaboss
阅读(246)
推荐(0) 编辑
C++ 二级指针和多级指针的访问和应用
摘要:1.二级指针 #include <iostream> #include <windows.h> #include <string> using namespace std; int main() { int guizi2 = 888; //存有私密文件的第二个柜子; int* guizi1 = &g
阅读全文
posted @
2022-09-12 17:15
wshidaboss
阅读(127)
推荐(0) 编辑
C++ const的应用
摘要:#include <iostream> #include <string> #include <windows.h> using namespace std; int main() { int girl = 19; int boy = 21; int const* p1 = &girl; //不能修
阅读全文
posted @
2022-09-12 10:56
wshidaboss
阅读(12)
推荐(0) 编辑
C++ 指针与整数之间、指针与指针之间的加减运算
摘要:1.指针与整数之间的加减运算: 1)指针加减数字表示的意义是指针在数组中位置的移动; 对于整数部分而言,它代表的是一个元素,对于不同的数据类型,其数组的元素占用的字节是不一样的, 比如指针 + 1,并不是在指针地址的基础之上加 1 个地址,而是在这个指针地址的基础上加 1 个元素占用的字节数: 1)
阅读全文
posted @
2022-09-11 17:18
wshidaboss
阅读(1150)
推荐(0) 编辑
C++ 指针的自增和自减操作 逆转字符串反向输出
摘要:1.指针的自增 #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { int ages[] = {21,51,26,36,41,25,36}; int len = si
阅读全文
posted @
2022-09-11 15:56
wshidaboss
阅读(231)
推荐(0) 编辑
C++ 指针的定义、初始化和访问
摘要:1.为什么要使用指针? 1)函数的值传递,无法通过调用函数,来修改函数的实参; 2)被调用函数需要提供更多的“返回值”给调用函数; 3)减少值传递时带来的额外开销,提高代码执行效率。 2.指针的初始化 1)32 位系统中,int 整数占 4 个字节,指针同样占 4 个字节 2)64 位系统中,int
阅读全文
posted @
2022-09-10 11:11
wshidaboss
阅读(282)
推荐(0) 编辑
C++ 打印地形数据中的峰点、谷点、最高点和最低点的数值及其位置(坐标)
摘要:#include <iostream> #include <windows.h> #include <string> #include <fstream> #define N 10 using namespace std; bool isPeak(int a[N][N], int i, int j)
阅读全文
posted @
2022-09-04 23:07
wshidaboss
阅读(227)
推荐(0) 编辑
C++ 打印杨辉三角/贾宪三角/帕斯卡三角
摘要:#include <iostream> #include <iomanip> #include <windows.h> #include <fstream> #include <string> using namespace std; #define N 10 int main() { int a[
阅读全文
posted @
2022-09-01 19:05
wshidaboss
阅读(51)
推荐(0) 编辑
C++ 地形导航系统之确定峰点的位置
摘要:#include <iostream> #include <windows.h> #include <fstream> #define N 64 using namespace std; bool isPeak(int grid[][N], int r, int c); int main() { i
阅读全文
posted @
2022-08-31 20:20
wshidaboss
阅读(36)
推荐(0) 编辑
C++ 二维数组作为函数的参数
摘要:数组作为函数的参数传递,不是单纯的值传递,传递的是数组本身(访问的是同一个地址,相同的值)。 版本1:指明参数: #include <iostream> #include <windows.h> using namespace std; void part1(int put[3][4]) { for
阅读全文
posted @
2022-08-31 11:35
wshidaboss
阅读(127)
推荐(0) 编辑
C++ 多维数组的访问
摘要:1.可以把一维数组想象成一排士兵,把二维数组想象成一个士兵方阵,把三维数组想象成多个士兵方阵。这样,当你要找其中的一个士兵时,你只要知道他在哪个方阵(从 0、1、2 中选择),在哪一行(从 0-3)中选择,在哪一列(从 0-4 中选择),就可以找到他了。 #include <iostream> #i
阅读全文
posted @
2022-08-29 13:00
wshidaboss
阅读(90)
推荐(0) 编辑
C++ 二维数组的访问
摘要:方法一: #include <iostream> #include <windows.h> #include <string> using namespace std; int main() { int i = 0, j = 0; int a[4][5]; //给数组成员赋值 for (int i
阅读全文
posted @
2022-08-28 20:59
wshidaboss
阅读(43)
推荐(0) 编辑
C++ 用函数打印员工的平均工资
摘要:#include <iostream> #include <windows.h> #include <string> using namespace std; float averageSalary(int n[],int i) { float sum = 0; for (int x = 0; x
阅读全文
posted @
2022-08-28 15:45
wshidaboss
阅读(49)
推荐(0) 编辑