Let’s Make C++ Great Again——string与常用字符处理函数
文章目录
string
在C++中,string是一个表示字符串的类,它提供了一系列方法来方便地操作字符串。与C语言中的字符串不同,string类是一个对象,可以像其他对象一样使用各种成员函数来处理它。
下面是一些常用的string方法:
length()
或size()
:返回字符串中字符的个数。substr(pos, len)
:返回从位置pos开始,长度为len的子串。append(str)
或operator+=(str)
:在字符串末尾添加str。insert(pos, str)
:在位置pos插入字符串str。erase(pos, len)
:删除从位置pos开始,长度为len的子串。replace(pos, len, str)
:用字符串str替换从位置pos开始,长度为len的子串。find(str)
或find(str, pos)
:在字符串中查找子串str,返回子串的起始位置,如果查找不到则返回string::npos
。compare(str)
:比较字符串与str,返回0表示相等,小于0表示当前字符串小于str,大于0表示当前字符串大于str。c_str()
:返回一个指向字符串内容的指针。empty()
:判断字符串是否为空,如果为空则返回true,否则返回false。clear()
:清空字符串中的所有字符。push_back(ch)
:在字符串末尾添加字符ch。pop_back()
:删除字符串末尾的一个字符。getline()
:从输入流中读取一行字符串,直到遇到换行符或文件结束符。at(index)
或operator[](index)
:返回字符串中索引为index的字符。front()
:返回字符串中的第一个字符。back()
:返回字符串中的最后一个字符。reserve(n)
:预留至少能容纳n个字符的空间,避免频繁分配内存。capacity()
:返回当前字符串能容纳的最大字符数。resize(n)
或resize(n, ch)
:调整字符串的大小为n,如果字符串变大,则在末尾填充字符ch;如果字符串变小,则删除末尾的字符。shrink_to_fit()
:将字符串的容量缩小到与其大小相同,释放多余的内存。
下面是一个使用string类的例子,将一个字符串中的大写字母转换为小写字母:
#include <iostream>
#include <string>using namespace std;int main() {string s = "Hello, World!";for (int i = 0; i < s.length(); i++) {if (s[i] >= 'A' && s[i] <= 'Z') {s[i] = s[i] - 'A' + 'a';}}cout << s << endl;return 0;
}
hello, world!
可以看到,程序成功地将字符串中的大写字母转换为小写字母。
使用string类的例子,统计一个字符串中单词的个数:
#include <iostream>
#include <string>using namespace std;int main() {string s = "Hello, World! This is a test.";int count = 0;for (int i = 0; i < s.length(); i++) {if (s[i] == ' ') {count++;}}count++; // 最后一个单词没有空格,所以需要额外加一次cout << "Number of words: " << count << endl;return 0;
}
Number of words: 6
可以看到,程序成功地统计了字符串中单词的个数。
总之,string类是一个非常实用的类,可以方便地处理字符串。在实际编程中,我们可以根据需要使用不同的方法来操作字符串,以实现各种功能。
在算法模拟题中
在算法模拟题中,string类是一个非常有用的工具,可以方便地处理字符串。下面是一些常见的使用string的算法模拟题:
翻转字符串:
将一个字符串翻转过来,例如将字符串"hello"变成"olleh"。可以使用string类的reverse()方法来实现。
#include <iostream>
#include <string>using namespace std;int main() {string s = "hello";reverse(s.begin(), s.end());cout << s << endl;return 0;
}
olleh
判断回文字符串:
判断一个字符串是否是回文字符串,即正着读和倒着读都一样,例如"level"是回文字符串,而"hello"不是。可以使用string类的reverse()方法或双指针来实现。
#include <iostream>
#include <string>using namespace std;int main() {string s = "level";string t = s;reverse(t.begin(), t.end());if (s == t) {cout << "Yes" << endl;} else {cout << "No" << endl;}return 0;
}
Yes
字符串查找:
在一个字符串中查找另一个字符串出现的位置,例如在字符串"hello, world"中查找子串"wor"的位置。可以使用string类的find()方法来实现。
#include <iostream>
#include <string>using namespace std;int main() {string s = "hello, world";string t = "wor";int pos = s.find(t);if (pos != string::npos) {cout << "Found at position " << pos << endl;} else {cout << "Not found" << endl;}return 0;
}
Found at position 7
字符串替换:
在一个字符串中将某个子串替换为另一个字符串,例如将字符串"hello, world"中的子串"world"替换为"universe"。可以使用string类的replace()方法来实现。
#include <iostream>
#include <string>using namespace std;int main() {string s = "hello, world";string t = "world";string u = "universe";int pos = s.find(t);if (pos != string::npos) {s.replace(pos, t.length(), u);}cout << s << endl;return 0;
}
hello, universe
总之,string类是一个非常实用的工具,在算法模拟题中可以方便地处理字符串。需要注意的是,在使用string类时,要注意字符串的长度、空间占用等问题,以避免出现不必要的错误。
常用字符处理函数
当然,以下是一些在编程中常用的字符处理函数:
strlen()
strlen() 函数可以返回一个字符串的长度,即该字符串中非空字符的数量。例如:
char s[] = "hello world";
int len = strlen(s); // len = 11
需要注意的是,strlen() 函数只能用于处理以 \\0 结尾的字符串,否则可能会导致未定义的行为。
strcpy()
strcpy() 函数可以将一个字符串复制到另一个字符串中。例如:
char s1[] = "hello";
char s2[10];
strcpy(s2, s1); // 将 s1 复制到 s2 中
需要注意的是,strcpy() 函数会将源字符串中的 \\0 也复制到目标字符串中,因此需要确保目标字符串有足够的空间来存储复制后的字符串。
strcat()
strcat() 函数可以将一个字符串追加到另一个字符串的末尾。例如:
char s1[] = "hello";
char s2[] = "world";
strcat(s1, s2); // 将 s2 追加到 s1 的末尾
需要注意的是,strcat() 函数会将源字符串中的 \\0 也追加到目标字符串的末尾,因此需要确保目标字符串有足够的空间来存储追加后的字符串。
strcmp()
strcmp() 函数可以比较两个字符串的大小关系。如果两个字符串相等,则返回 0;如果第一个字符串小于第二个字符串,则返回一个负数;如果第一个字符串大于第二个字符串,则返回一个正数。例如:
char s1[] = "hello";
char s2[] = "world";
int cmp = strcmp(s1, s2); // cmp < 0
需要注意的是,strcmp() 函数会比较两个字符串的每个字符,直到出现不同的字符或其中一个字符串结束。
toupper() 和 tolower()
toupper() 函数可以将一个小写字母转换为大写字母,而 tolower() 函数可以将一个大写字母转换为小写字母。例如:
char c = 'a';
char d = toupper(c); // d = 'A'
isdigit() 和 isalpha()
isdigit() 函数可以判断一个字符是否为数字字符(即 0 到 9 中的任意一个字符),而 isalpha() 函数可以判断一个字符是否为字母字符(即 A 到 Z 或 a 到 z 中的任意一个字符)。例如:
char c = '9';
if (isdigit(c)) {cout << c << " 是数字字符。" << endl;
}
这些只是编程中常用的一些字符处理函数的例子。通过使用这些函数,您可以使您的代码更简洁、易读和易于维护。
strncpy()
strncpy() 类似于 strcpy(),但它允许您从源字符串复制指定数量的字符到目标字符串中。例如:
char s1[] = "hello";
char s2[10];
strncpy(s2, s1, 3); // 复制 s1 的前 3 个字符到 s2 中
strchr() 和 strrchr()
strchr() 函数搜索字符串中指定字符的第一个出现位置,并返回该位置的指针,而 strrchr() 函数搜索字符串中指定字符的最后一个出现位置,并返回该位置的指针。例如:
char s[] = "hello world";
char* p = strchr(s, 'o'); // p 指向第一个 'o'
p = strrchr(s, 'o'); // p 指向最后一个 'o'
strstr()
strstr() 函数搜索字符串中指定子字符串的第一个出现位置,并返回该位置的指针。例如:
char s[] = "hello world";
char* p = strstr(s, "world"); // p 指向 "world" 子字符串
isalnum() 和 isxdigit()
isalnum() 函数可以判断一个字符是否为字母或数字字符,而 isxdigit() 函数可以判断一个字符是否为十六进制数字字符。例如:
char c = 'A';
if (isalnum(c)) {cout << c << " 是字母或数字字符。" << endl;
}
if (isxdigit(c)) {cout << c << " 是十六进制数字字符。" << endl;
}
isspace()
isspace() 函数可以判断一个字符是否为空格字符(即空格、制表符、换行符、回车符、换页符或垂直制表符中的任意一个字符)。例如:
char c = ' ';
if (isspace(c)) {cout << c << " 是空格字符。" << endl;
}
isupper() 和 islower()
isupper() 函数可以判断一个字符是否为大写字母,而 islower() 函数可以判断一个字符是否为小写字母。例如:
char c = 'A';
if (isupper(c)) {cout << c << " 是大写字母。" << endl;
}
sprintf()
sprintf() 函数可以通过替换占位符来格式化字符串。例如:
int x = 42;
char s[20];
sprintf(s, "答案是 %d。", x); // s = "答案是 42。"
这些只是编程中常用的一些字符处理函数的例子。通过使用这些函数,您可以使您的代码更简洁、易读和易于维护。