ARTICLE · 1511700
GESP 4 级 函数说明
GESP 4 级 函数说明
1. min / max — 求最小值 / 最大值
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 #include <bits/stdc++.h>using namespace std;// 头文件:<algorithm>// 函数原型:// template<class T>// const T& min(const T& a, const T& b);//// template<class T>// const T& max(const T& a, const T& b);// 返回值:返回 a 和 b 中较小/较大的那个(引用)// 注意:a 和 b 类型必须相同,否则编译错误int main(){ // min:返回两个值中的较小者 int a = 10, b = 20; cout << min(a, b) << endl; // 10 // max:返回两个值中的较大者 cout << max(a, b) << endl; // 20 // 支持 double 等类型 double x = 3.14, y = 2.71; cout << min(x, y) << endl; // 2.71 cout << max(x, y) << endl; // 3.14 // 支持 char(按 ASCII 码比较) cout << min('A', 'Z') << endl; // 65 (A) cout << max('A', 'Z') << endl; // 90 (Z) // 不支持不同类型混用: // min(10, 3.14); // 编译错误!类型不同 // 三个值求最值:嵌套调用 int p = 5, q = 8, r = 3; cout << max(p, max(q, r)) << endl; // 8 cout << min(p, min(q, r)) << endl; // 3 // 注意:min/max 返回的是引用,当参数为表达式时 // 会产生临时对象,不能取其地址 // int* ptr = &min(a, b); // 危险!b 是左值可以,但表达式不行 return 0;}
2. abs — 求绝对值
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstdlib>(C 风格)或 <cmath>(C++ 风格)// 函数原型:// int abs(int n); // int 类型// long abs(long n); // long 类型// long long abs(long long n); // C++11,long long 类型// double abs(double n); // <cmath> 中的重载// float abs(float n);// long double abs(long double n);// 返回值:返回 n 的绝对值(类型与参数相同)int main(){ // int 类型 cout << abs(-5) << endl; // 5 cout << abs(10) << endl; // 10 cout << abs(0) << endl; // 0 // long long 类型(大整数) long long big = -1000000000000LL; cout << abs(big) << endl; // 1000000000000 // double 浮点类型 cout << abs(-3.14) << endl; // 3.14 cout << abs(-0.5) << endl; // 0.5 // 注意:abs 对 INT_MIN / LONG_LONG_MIN 等 // 最小负数取绝对值会溢出(补码表示不对称) // int val = abs(INT_MIN); // 未定义行为! // 因为 INT_MIN = -2147483648,其绝对值 2147483648 超过 int 范围 // 浮点数绝对值用 fabs(<cmath>)更明确 // fabs 只处理浮点类型,不处理整数 double d = -2.718; cout << fabs(d) << endl; // 2.718 // 竞赛中常用 abs 处理整数,fabs 处理浮点数 // 但 <bits/stdc++.h> 下 abs 对 double 也有效 return 0;}
3. memset — 内存块设置
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// void* memset(void* ptr, int value, size_t num);// 参数:// ptr - 指向要填充的内存块的指针// value - 要设置的值(按 unsigned char 逐字节填充)// num - 要填充的字节数// 返回值:返回 ptr(即原指针)int main(){ // === 典型用法 1:将数组所有元素初始化为 0 === int a[10]; // memset 按字节填充,参数是字节数 // 对 int 数组,总字节数 = 元素个数 × sizeof(int) memset(a, 0, sizeof(a)); // 等价于:for (int i = 0; i < 10; i++) a[i] = 0; for (int i = 0; i < 10; i++) cout << a[i] << " "; // 全 0 cout << endl; // === 典型用法 2:将数组初始化为 -1 === // -1 的补码是 0xFF...FF,逐字节填充 0xFF 后恰好是 -1 int b[10]; memset(b, -1, sizeof(b)); cout << b[0] << endl; // -1 // === 错误用法 1:memset 不能用于初始化为 1 === int c[10]; memset(c, 1, sizeof(c)); // 按字节填充 0x01,每个 int 变成 0x01010101 = 16843009 cout << c[0] << endl; // 16843009(不是 1!) // === 错误用法 2:对非 POD 类型使用 memset === // string s[10]; // memset(s, 0, sizeof(s)); // 错误!string 有内部指针,会内存泄漏 // === 错误用法 3:sizeof 使用错误 === // memset(a, 0, 10); // 只设置了前 10 个字节,不是 10 个 int // 正确:memset(a, 0, sizeof(a)); // 整个数组 // 正确:memset(a, 0, 10 * sizeof(int)); // 显式指定字节数 // 对字符数组,sizeof 直接等于元素个数(char 占 1 字节) char s[100]; memset(s, 0, sizeof(s)); // 正确:char 数组 return 0;}
4. memcpy — 内存块拷贝
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// void* memcpy(void* dest, const void* src, size_t num);// 参数:// dest - 目标地址(拷贝到哪里)// src - 源地址(从哪里拷贝)// num - 要拷贝的字节数// 返回值:返回 dest(即目标地址)// 注意:dest 和 src 不能重叠(重叠用 memmove)int main(){ // === 典型用法 1:拷贝数组 === int src[5] = {1, 2, 3, 4, 5}; int dst[5] = {0}; // 将 src 的全部内容拷贝到 dst // 参数:目标地址,源地址,字节数 memcpy(dst, src, sizeof(src)); for (int i = 0; i < 5; i++) cout << dst[i] << " "; // 1 2 3 4 5 cout << endl; // === 典型用法 2:拷贝部分元素 === int part[3]; // 只拷贝前 3 个元素(3 × 4 = 12 字节) memcpy(part, src, 3 * sizeof(int)); for (int i = 0; i < 3; i++) cout << part[i] << " "; // 1 2 3 cout << endl; // === 典型用法 3:拷贝结构体数组 === struct Point { int x, y; }; Point pts1[3] = {{1, 2}, {3, 4}, {5, 6}}; Point pts2[3]; memcpy(pts2, pts1, sizeof(pts1)); cout << pts2[1].x << " " << pts2[1].y << endl; // 3 4 // === 典型用法 4:memcpy 与 memove 的区别 === // memcpy 不保证处理重叠区域,memmove 保证 int arr[5] = {1, 2, 3, 4, 5}; // 将 arr[0..2] 拷贝到 arr[2..4](重叠!) // memcpy(arr + 2, arr, 3 * sizeof(int)); // 未定义行为! memmove(arr + 2, arr, 3 * sizeof(int)); // 安全 for (int i = 0; i < 5; i++) cout << arr[i] << " "; // 1 2 1 2 3 cout << endl; // === 注意事项 === // 1. memcpy 不会检查 dest 是否有足够空间,需要调用者保证 // 2. 对非 POD 类型(如 string、vector)不能使用 memcpy // 3. 字节数参数通常用 sizeof 计算,不要硬编码 // 4. 复制方向:从 src 到 dest return 0;}
5. strlen — 获取字符串长度
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// size_t strlen(const char* str);// 返回值:返回字符串 str 的长度(不含结尾的 '\0')// 注意:str 必须以 '\0' 结尾,否则行为未定义int main(){ // === 典型用法 === const char* s1 = "Hello"; cout << strlen(s1) << endl; // 5 char s2[] = "GESP"; cout << strlen(s2) << endl; // 4 char s3[100] = "C++"; cout << strlen(s3) << endl; // 3 // === strlen 与 sizeof 的区别 === char str[] = "Hello"; // strlen:实际字符个数(不含 '\0') cout << strlen(str) << endl; // 5 // sizeof:数组占用总字节数(含 '\0') cout << sizeof(str) << endl; // 6(5 个字符 + 1 个 '\0') // 对指针:sizeof 只返回指针本身大小(8 字节) const char* p = "Hello"; cout << strlen(p) << endl; // 5 cout << sizeof(p) << endl; // 8(64 位系统指针大小) // === 字符串长度遍历的两种方式 === // 方式 1:用 strlen 获取长度 char msg[] = "GESP"; int len = strlen(msg); for (int i = 0; i < len; i++) cout << msg[i]; cout << endl; // 方式 2:用 '\0' 判断结束(更高效,避免重复计算长度) for (int i = 0; msg[i] != '\0'; i++) cout << msg[i]; cout << endl; // === 注意:strlen 的 O(n) 时间复杂度 === // 在循环中多次调用 strlen 会导致性能问题 char big[1000] = "test"; // 错误:每次循环都调用 strlen,O(n²) // for (int i = 0; i < strlen(big); i++) { ... } // 正确:先存长度,再循环 // int n = strlen(big); // for (int i = 0; i < n; i++) { ... } // === 空字符串 === char empty[] = ""; cout << strlen(empty) << endl; // 0 // === 未以 '\0' 结尾的字符数组(危险!) === // char bad[3] = {'A', 'B', 'C'}; // 没有 '\0' // cout << strlen(bad); // 未定义行为!会越界读取直到遇到 '\0' return 0;}
6. strcmp — 字符串比较
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// int strcmp(const char* str1, const char* str2);// 返回值:// < 0 : str1 小于 str2(按字典序)// = 0 : str1 等于 str2// > 0 : str1 大于 str2// 比较规则:逐字符比较 ASCII 码,遇到第一个不同字符时决定大小int main(){ // === 基本比较 === cout << strcmp("apple", "apple") << endl; // 0(相等) cout << strcmp("apple", "apply") << endl; // 负数('e' < 'y') cout << strcmp("apply", "apple") << endl; // 正数('y' > 'e') // === 大小写不同 === cout << strcmp("Apple", "apple") << endl; // 正数('A'=65 < 'a'=97?不对,是负数) // 实际上 'A'(65) < 'a'(97),所以 "Apple" < "apple" // 结果是负数 // === 长度不同时的比较 === cout << strcmp("abc", "abcdef") << endl; // 负数("abc" 是 "abcdef" 的前缀) // 较短字符串遇到 '\0','\0' 的 ASCII 码 0 小于任何字符 // 所以前缀字符串小于较长字符串 // === 在排序中的用法 === // 用 strcmp 作为比较函数对字符串数组排序 const char* names[] = {"Bob", "Alice", "Charlie", "alice"}; int n = 4; // 冒泡排序(按字典序) for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (strcmp(names[j], names[j + 1]) > 0) { swap(names[j], names[j + 1]); } } } // 结果:Alice, Bob, Charlie, alice(大写字母 < 小写字母) for (int i = 0; i < n; i++) cout << names[i] << " "; cout << endl; // === strcmp 与 == 的区别 === char s1[] = "hello"; char s2[] = "hello"; // s1 == s2 比较的是地址,不是内容! // cout << (s1 == s2) << endl; // 0(false,地址不同) // 正确比较内容: cout << strcmp(s1, s2) << endl; // 0(相等) // === 不区分大小写的比较 === // stricmp / strcasecmp:不区分大小写(非标准,但常用) // #ifdef _WIN32 // cout << stricmp("Hello", "HELLO") << endl; // 0 // #else // cout << strcasecmp("Hello", "HELLO") << endl; // 0 // #endif // 手动实现不区分大小写比较: auto toLower = [](char c) { return (c >= 'A' && c <= 'Z') ? c + 32 : c; }; // 或使用 tolower() 函数(<cctype>) return 0;}
7. strcpy / strncpy — 字符串拷贝
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// char* strcpy(char* dest, const char* src);// 拷贝 src 到 dest(含 '\0'),不检查 dest 空间// char* strncpy(char* dest, const char* src, size_t n);// 最多拷贝 n 个字符,不保证以 '\0' 结尾// 返回值:返回 destint main(){ // === strcpy:基本拷贝 === char src[] = "Hello GESP"; char dst1[100]; // 确保 dest 空间足够 // 将 src 拷贝到 dst1(含结尾 '\0') strcpy(dst1, src); cout << dst1 << endl; // Hello GESP // === strcpy 的危险:缓冲区溢出 === char small[5]; // 只有 5 个字节! // strcpy(small, "Hello GESP"); // 危险!源字符串长度 > 目标空间 // 会导致缓冲区溢出,覆盖相邻内存,可能引发程序崩溃或安全漏洞 // === strncpy:安全拷贝(限制拷贝长度) === char dst2[10]; // 最多拷贝 9 个字符(留 1 个给 '\0') strncpy(dst2, "Hello GESP", 9); dst2[9] = '\0'; // 手动添加 '\0'(strncpy 不保证以 '\0' 结尾) cout << dst2 << endl; // Hello GES // strncpy 的行为细节: char dst3[10] = {}; strncpy(dst3, "Hi", 10); // 如果 src 长度 < n,剩余位置用 '\0' 填充 cout << dst3 << "end" << endl; // Hiend // === 手动实现安全拷贝(C++ 风格) === // 更推荐用 C++ string 类,避免 C 风格字符串的陷阱 string s = "Hello GESP"; string t = s; // 自动管理内存,无需担心缓冲区溢出 cout << t << endl; // === 常见用途:将字符串拷贝到结构体成员 === struct Student { char name[50]; int score; }; Student stu; strcpy(stu.name, "Alice"); // 安全:50 字节足够 // strncpy(stu.name, "Alice", 50); // 更安全的方式 cout << stu.name << " " << stu.score << endl; return 0;}
8. strcat / strncat — 字符串拼接
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// char* strcat(char* dest, const char* src);// 将 src 追加到 dest 末尾(覆盖 dest 的 '\0')// char* strncat(char* dest, const char* src, size_t n);// 最多追加 n 个字符,自动添加 '\0'// 返回值:返回 destint main(){ // === strcat:基本拼接 === char str[100] = "Hello"; // 将 " World" 追加到 str 末尾 strcat(str, " World"); cout << str << endl; // Hello World // 多次拼接 char path[200] = "/usr"; strcat(path, "/local"); strcat(path, "/bin"); cout << path << endl; // /usr/local/bin // === strcat 的危险:缓冲区溢出 === char small[10] = "Hi"; // strcat(small, " there!!!"); // 危险!拼接后超过 10 字节 // 会导致缓冲区溢出 // === strncat:安全拼接(限制追加长度) === char safe[20] = "Hello"; strncat(safe, " GESP World", 5); // 只追加前 5 个字符 cout << safe << endl; // Hello GESP // strncat 自动在末尾添加 '\0' // 与 strncpy 不同,strncat 总是以 '\0' 结尾 // === 手动拼接(C 风格,更可控) === char buf[100] = "Hello"; int pos = strlen(buf); // 找到当前末尾位置 const char* add = " GESP"; // 逐字符拷贝 for (int i = 0; add[i] != '\0'; i++) { buf[pos++] = add[i]; } buf[pos] = '\0'; // 添加结尾 '\0' cout << buf << endl; // Hello GESP // === C++ 风格(推荐) === string s = "Hello"; s += " GESP"; // 简洁安全,自动管理内存 s += " 2024"; cout << s << endl; // Hello GESP 2024 // === 注意:dest 必须以 '\0' 结尾 === // char bad[10] = {'H', 'i'}; // 没有 '\0'! // strcat(bad, "!"); // 未定义行为!strcat 找不到 '\0' 位置 return 0;}
9. strstr / strchr — 字符串/字符查找
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 #include <bits/stdc++.h>using namespace std;// 头文件:<cstring> 或 <string.h>// 函数原型:// char* strstr(const char* str, const char* substr);// 在 str 中查找 substr 第一次出现的位置// 返回值:找到则返回指向该位置的指针;未找到返回 NULL//// char* strchr(const char* str, int c);// 在 str 中查找字符 c 第一次出现的位置// 返回值:找到则返回指向该位置的指针;未找到返回 NULL//// char* strrchr(const char* str, int c);// 在 str 中查找字符 c 最后一次出现的位置int main(){ const char* text = "Hello GESP, welcome to GESP exam"; // === strstr:查找子串 === // 查找 "GESP" 在 text 中第一次出现的位置 const char* p = strstr(text, "GESP"); if (p != NULL) { // p 指向 "GESP" 的起始位置 // 通过指针偏移可以输出从该位置开始的剩余字符串 cout << "找到位置:" << (p - text) << endl; // 6 cout << "剩余内容:" << p << endl; // GESP, welcome to GESP exam } // 查找不存在的子串 const char* q = strstr(text, "xyz"); if (q == NULL) { cout << "未找到子串" << endl; } // === strchr:查找字符 === const char* r = strchr(text, 'o'); if (r != NULL) { cout << "字符 'o' 首次出现位置:" << (r - text) << endl; // 4 } // 查找字符 'o' 最后一次出现的位置 const char* s = strrchr(text, 'o'); if (s != NULL) { cout << "字符 'o' 最后出现位置:" << (s - text) << endl; // 24 } // === 遍历所有匹配位置 === // 用 strchr 循环查找所有 'e' const char* pos = text; int count = 0; while ((pos = strchr(pos, 'e')) != NULL) { count++; cout << "第 " << count << " 个 'e' 位置:" << (pos - text) << endl; pos++; // 跳过当前字符,继续查找 } // === 查找并提取子串 === // 找到 "GESP" 后,提取其后的内容 const char* found = strstr(text, "to"); if (found != NULL) { // 从 found 位置拷贝到缓冲区 char buf[50]; strncpy(buf, found, 9); buf[9] = '\0'; cout << "提取内容:" << buf << endl; // to GESP e } // === 注意:strstr/strchr 返回的是原字符串中的指针 === // 不是新分配的内存,不要用 delete 释放 return 0;}















































































