ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

第10天(中等题 滑动窗口)

2026/8/9 11:19:53 拓冰建站 浏览量
第10天(中等题 滑动窗口)

打卡第十天
2道中等题

image

两个函数:

1.ans = move(t) 是将字符串 t 的内容转移给 ans,而不是进行拷贝。

详细

image

2.substr 是 C++ 字符串的标准成员函数,用于从原字符串中提取子串。

详细

函数原型:

string substr(size_t pos = 0, size_t len = npos) const;

pos:子串的起始位置(索引,从0开始)

len:子串的长度(要提取的字符数)

返回一个新的字符串,包含从 pos 开始的 len 个字符。

#include <iostream>
#include <string>
using namespace std;
int main() {string s = "Hello, World!";// 示例1:从位置7开始,提取5个字符string sub1 = s.substr(7, 5);cout << sub1 << endl;  // 输出: "World"// 示例2:从位置0开始,提取到结尾string sub2 = s.substr(0);cout << sub2 << endl;  // 输出: "Hello, World!"// 示例3:从位置5开始,提取2个字符string sub3 = s.substr(5, 2);cout << sub3 << endl;  // 输出: ", "// 示例4:只指定起始位置,不指定长度string sub4 = s.substr(7);cout << sub4 << endl;  // 输出: "World!"return 0;
}

耗时≈一小时 明天继续