关于string的成员函数substr

来源:互联网 发布:vr 刷新率 知乎 编辑:程序博客网 时间:2024/06/04 18:31
#include <iostream>#include <string>#include<vector>using namespace std;int main () {    string str="where does father go";    string str2 = str.substr (3,5); //两个参数版本(推荐)从位置3开始的5个字符          string::size_type pos = str.find("does");     string str3 = str.substr (pos);//一个参数版本,从位置pos开始知道最后    cout << str2 << endl;    cout << pos << endl;    cout << str3 << endl;    cout << str.substr(19) << endl;    cout << "**********************" << endl;    cout << str.substr(20) << endl;//最后一个的后面一位,输出空。这里的参数不能再变大了,否则运行出错    cout << "**********************" << endl;    getchar();    return 0;}

output:

re do6does father goo********************************************
0 0