c++常用函数
文章目录
C-Style
ctype
| 函数 | 功能 |
|---|---|
| int isalnum(int c) | 判断一个字符是否为英文字母或数字 |
| int isalpha(int c) | 检查所传的字符是否是字母 |
| int islower(int c) | 检查所传的字符是否是小写字母 |
| int isupper(int c) | 检查所传的字符是否是大写字母 |
| int isdigit(int c) | 检查所传的字符是否为阿拉伯数字0 到9 |
| int isspace(int c) | 检查所传的字符是否是空格字符(空格、tab) |
| int tolower(int c) | 把大写字母转换为小写字母 |
| int toupper(int c) | 把小写字母转换为大写字母 |
math
| 函数 | 功能 |
|---|---|
| double modf(double x, double *integer) | 返回值为小数部分(小数点后的部分),并设置 integer 为整数部分 |
| double pow(double x, double y) | 返回 x 的 y 次幂 |
| double sqrt(double x) | 返回大于或等于 x 的最小的整数值 |
| double ceil(double x) | 返回大于或等于 x 的最小的整数值 |
| double floor(double x) | 返回小于或等于 x 的最大的整数值 |
stdlib
| 函数 | 功能 |
|---|---|
| double atof(const char *str) | 把字符串str转换为浮点数(double) |
| int atoi(const char *str) | 把字符串str转换为整数(int) |
| long int atol(const char *str) | 把字符串str转换为整数(long) |
| int abs(int x) | 返回 x 的绝对值 |
| int rand(void) | 返回一个范围在 0 到 RAND_MAX 之间的伪随机数 |
| void srand(unsigned int seed) | 该函数播种由函数 rand 使用的随机数发生器 |
stdio
| 函数 | 功能 |
|---|---|
| int sprintf(str, "%d", num) | 把num内容以流形式输入到str |
| int sscanf(str, "%d", &num) | 把str内容以流的形式输入到num |
| int getchar(void) | 从标准输入 stdin 获取一个字符(一个无符号字符) |
| int putchar(int char) | 把参数 char 指定的字符(一个无符号字符)写入到标准输出 stdout 中 |
string
| 函数 | 功能 |
|---|---|
| int memcmp(const void str1, const void str2, size_t n) | 比较前n个字符,字典序,相同返回0,str1 |
| void memcpy(void dest, const void *src, size_t n) | 从 src 复制 n 个字符到 dest |
| void memset(void str, int c, size_t n) | 复制字符 |
| char strcat(char dest, const char *src) | 把 src 所指向的字符串追加到 dest 所指向的字符串的结尾 |
| char strncat(char dest, const char *src, size_t n) | 把 src 所指向的字符串追加到 dest 所指向的字符串的结尾,直到 n 字符长度为止 |
| int strcmp(const char str1, const char str2) | 把 str1 所指向的字符串和 str2 所指向的字符串进行比较 |
C++
string
npos:以字符串子集为参数的函数以 npos 为“所有到达终点的方式”指示器
构造函数
| 函数 | 功能 |
|---|---|
| string(str) | 生成str的复制品 |
| string(str, pos) | 生成pos开头到结尾的字符串 |
| string(str, pos, strlen) | 生成pos开头,长度为strlen的字符串 |
| string(cstr) | 生成c风格字符串的复制品 |
| string(cstr, cstrlen) | 生成cstr [0, cstrlen)的字符串 |
| string(num, c) | 生成包含num个c字符的串 |
| string(beg, end) | 生成[beg, end) 的字符串 |
元素访问
| 函数 | 功能 |
|---|---|
| operator[] | |
| front | |
| back | |
| c_str | 返回指向c字符串的临时指针【注】与strcpy(cstr,s.c_str());搭配使用 |
操作
| 函数 | 操作 |
|---|---|
| clear, push_back, pop_back, operator+=(后附字符到结尾), resize | |
| insert | (见下文重载) |
| erase | (见下文重载) |
| replace, copy, swap | (不用) |
| substr(pos = 0, count = npos) | 返回[pos, pos+count)的子串 |
insert
str.insert(index, count, char ch) 在index处插入count个字符ch
str.insert(index, cstr) 在index处插入c字符串cstr
str.insert(index, cstr, count) 在index处插入cstr的[0,count)子串
str.insert(index, str) 在index处插入str(c++字符串)
str.insert(index, str, index_str, count) 在index处插入str.substr(index_str, count);子串
str.insert(iterator pos(属于str的迭代器), first, last) 在迭代器pos处插入[first, last)的子串
erase
str.erase(index) 擦除[index, end)
str.erase(iterator pos) 擦除pos位置的字符
str.erase(iterator first, iterator last) 擦除[first, last)
查找
| 函数 | 功能 |
|---|---|
| size_type find(str/cstr/ch, pos, count) ; find_first_of( ) | 寻找字符串/字符从pos开始第一次出现的位置 |
| size_type rfind(str/cstr/ch, pos, count) ; find_last_of( ) | 最后一次出现的位置 |
若不存在则返回npos返回的是size_type(无符号整数)类型
非成员函数
operator+
数值转换
| 函数 | 功能 |
|---|---|
| stoi; stol; stoll (str, pos = 0) | 字符串转换为整数 |
| stof; stod; stold (str, pos = 0) | 字符串转换为浮点数 |
| to_string(value) | 整数/浮点数转化为字符串 |
vector
构造函数
| 函数 | 功能 |
|---|---|
| vector(count) | |
| vector(count, value) | |
| vector(first, last) | |
| vector(vector& other) | |
| vector{1,2,3... } 初值列表 |
set
构造函数
| 函数 | 功能 |
|---|---|
| set(first, last) | 范围构造函数 |
| set(set& other) | 复制构造函数 |
| set {1,2,3...} 初值列表 | initializer_list 构造函数 |
成员函数
| 函数 | 功能 |
|---|---|
| st.insert(key) | 插入key值 |
| st.count(key) | 返回key值个数 |
| st.find(key) | 返回key值的迭代器 |
| st.contains(key) | 检查是否有key |
map
构造函数
默认构造函数 | 范围构造函数 | 复制构造函数 | initializer_list 构造函数
成员函数
| 函数 | 功能 |
|---|---|
| operator[] | 插入元素 |
| mp.count(key) | |
| mp.find(key) | |
| mp.contains(key) |
multiset
允许多个值存在的set
构造函数
默认构造函数 | 范围构造函数 | 复制构造函数 | initializer_list 构造函数
成员函数
| 函数 | 功能 |
|---|---|
| mlst.insert(key) | 插入元素 |
| mlst.count(key) | |
| mlst.find(key) | |
| mlst.contains(key) |
multimap
构造函数
默认构造函数 | 范围构造函数 | 复制构造函数 | initializer_list 构造函数
成员函数
| 函数 | 功能 |
|---|---|
| mlmp.insert(pair/initializer_list) | 插入元素 |
| mlmp.count(key) | |
| mlmp.find(key) | |
| mlmp.contains(key) |
unorderd_multiset
哈希表元素顺序随机(与插入顺序不同!)
unorderd_multimap
二维哈希表
pair
构造pair
- pair<int, string> (1, "ss");
- make_pair(1, "ss");
元素访问:
pr.first, pr.second
算法
algorithm
不修改序列的操作
| 函数 | 功能 |
|---|---|
| for_each(first, last, 可执行对象op) | 对[first, last)每个对象执行op |
| for_each_n(first, n, 可执行对象op) | 对[first, first+n)每个对象执行op |
| count/count_if(first, last, op) | 返回符合op的对象个数 |
| find/find_if(fist, last, oj/op) | 返回第一个等于oj或符合op的迭代器 |
| find_end, find_first_of |
修改序列的操作
| 函数 | 功能 |
|---|---|
| copy, copy_n | |
| fill(first, last, value); fill_n(first, count, value) | 赋值value给[first, last) |
| swap(&a, &b) (均是引用) | 交换元素 |
| reverse(first, last) | 反转元素 |
| unique(first, last) | 消除连续的重复元素,返回尾后迭代器 |
排序
| 函数 | 功能 |
|---|---|
| is_sorted(first, last, comp) | 检查是否有序,可选用comp定序 |
| sort(first, last, comp) |
二分操作
| 函数 | 功能 |
|---|---|
| lower_bound(first, last, value) | 返回指向第一个大于等于value的元素迭代器 |
| upper_bound(first, last, value) | 返回指向第一个大于给定值的元素的迭代器 |
最大/最小操作
| 函数 | 功能 |
|---|---|
| max/min(a, b) | |
| max/min_element(first, last) | 寻找第一个最大元素 |
| equal(first1, last1, first2) | 若范围内相等返回true |
numeric
| 函数 | 功能 |
|---|---|
| iota(first, last, value) | 用以value开始递增的元素填充[first, last) |
| accumulate(first, last, init) | 以init为初值累加[first, last)元素 |
正则表达式
#include <regex>
regex
| 函数 | 功能 |
|---|---|
| regex (re) | re:正则表达式 |
| regex (re, f) | f:标志 regex::icase表示匹配过程中忽略大小写 |
smatch
| 函数 | 功能 |
|---|---|
| smatch.str(x) | x=0:匹配字符串 x=i:子表达式i |
查找:
string s; regex patt;
for(sregex_iterator it(s.begin(), s.end(), patt), end_it; it != end_it; it++)
cout << it->str() << endl;
替换:
string fmt; //替换规则,$x表示第x个表达式
string s = regex_replace(s, patt, fmt);
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论