关于string的成员函数substr

#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 do
6
does father go
o
**********************

**********************

猜你喜欢

转载自blog.csdn.net/zxx2096/article/details/81128471
今日推荐