s.substr(star, length)是用来获取字符串s中的一段字串
Examp:s=“0123456789”;s.substr(2, 5)=“23456”;s.substr(0, 5)=“01234”;
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s;
while (cin >> s) {
for (int i = 0; i < 5; i ++) {
cout << s.substr(i, 5) << endl;
}
}
}
输入asdfghjkl
结果为:
asdfg
sdfgh
dfghj
fghjk
ghjkl
输入asdfghj
结果为:
asdfg
sdfgh
dfghj
fghj
ghj
输入:as
结果为:
as
sterminate called after throwing an instance of ' std::out_of_range'
what(): basic_string::substr: __pos (which is 3) > this->size() (which is 2)
This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.