string函数介绍
1.字符串的定义:
字符串是用双引号括起来的0个或多个字符组成的序列,可以包括字母、数字、符合、空格等字符。
2.字符串的输入:
不含空格的输入:cin>>变量名;
示例:
#include <iostream>
#include <string>
int main()
{
// 声明一个字符串变量s
std::string s;
// 从标准输入读取一个字符串并存储到s中
std::cin >> s;
// 将读取到的字符串输出到标准输出
std::cout << s << std::endl;
return 0;
}
含空格的输入:getline(cin,变量名)
示例:
#include <iostream>
#include <string>
int main()
{
// 声明一个字符串变量s
std::string s;
// 从标准输入读取一个字符串并存储到s中
getline(std::cin, s);
// 将读取到的字符串输出到标准输出
std::cout << s << std::endl;
return 0;
}
3.字符串输出
使用for循环逐个输出:
#include <iostream>
#include <string>
int main()
{
// 声明一个字符串变量s
std::string s;
// 从标准输入读取一个字符串并存储到s中
getline(std::cin, s);
// 用循环的方式遍历字符串s中的每个字符
for (int i = 0; i < s.size(); i++) {
// 输出字符串s中的每个字符
std::cout << s[i];
}
return 0;
}
使用cout整体输出:
#include <iostream>
#include <string>
int main()
{
// 声明一个字符串变量s
std::string s;
// 从标准输入读取一个字符串并存储到s中
getline(std::cin, s);
// 将读取到的字符串输出到标准输出
std::cout << s << std::endl;
return 0;
}
4. 字符串函数
find()函数
- 功能:在字符串中查找字符或子串。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s = "hello world";
// 查找字符 'l' 在字符串 s 中的位置,返回首次出现的下标
int pos1 = s.find('l');
std::cout << "字符 'l' 首次出现的位置:" << pos1 << std::endl;
// 查找子串 "world" 在字符串 s 中的位置,返回首次出现的下标
int pos2 = s.find("world");
std::cout << "子串 'world' 首次出现的位置:" << pos2 << std::endl;
return 0;
}
insert()函数
- 功能:在字符串中插入一个子串。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s = "hello";
// 在字符串 s 的下标 2 前插入子串 "abc"
s.insert(2, "abc");
std::cout << s << std::endl;
return 0;
}
substr()函数
- 功能:提取一个字符串的子串。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s = "abcdefg";
// 从下标 2 开始提取长度为 3 的子串
std::string sub = s.substr(2, 3);
std::cout << sub << std::endl;
return 0;
}
replace()函数
- 功能:替换字符。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s = "hello world";
// 将字符串 s 中从下标 6 开始长度为 5 的子串替换为 "universe"
s.replace(6, 5, "universe");
std::cout << s << std::endl;
return 0;
}
reverse()函数
- 功能:实现整个字符串的反转。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s = "abcdef";
// 反转字符串 s
s.reverse(s.begin(), s.end());
std::cout << s << std::endl;
return 0;
}
5. 字符串应用
有效字符统计
- 功能:统计字符串中非空白字符(例如字母数字、标点符号等)的个数。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s;
getline(std::cin, s);
int count = 0;
for (int i = 0; i < s.size(); i++) {
if (!isspace(s[i])) {
count++;
}
}
std::cout << "非空白字符个数:" << count << std::endl;
return 0;
}
统计单词个数
- 思路:通过分析可以看出除了最后一个单词,其余单词后都有一个空格,也就是单词数 = 空格数 + 1。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string s;
int num = 0;
getline(std::cin, s);
for (int i = 0; i < s.size(); i++) {
if (s[i] =='') {
num++;
}
}
std::cout << num + 1 << std::endl;
return 0;
}
词频统计
- 思路:首先在字符串 a 中以 a[0]为首元素提取 b.size()个字符组成子串和字符串 b 比较;接下来再以 a[1]为首元素提取 b.size()个字符和字符串 b 比较,直到 a.size() - b.size()截止。
- 示例:
#include <iostream>
#include <string>
int main()
{
std::string a, b, c;
getline(std::cin, a);
getline(std::cin, b);
int lena = a.length(), lenb = b.length(), sum = 0;
for (int i = 0; i <= lena - lenb; i++) {
c = a.substr(i, lenb);
if (c == b) {
sum++;
}
}
std::cout << sum << std::endl;
return 0;
}