补充了一些以前不知道的string类知识
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
//substr(pos,len),返回从pos号位开始,长度为len的元素
string str="Thank you for your smile";
cout<<str.substr(0,5)<<endl;
cout<<str.substr(14,4)<<endl;
cout<<str.substr(19,5)<<endl;
//string::npos是一个常数,本身=-1,usigned值,所以可以认为是4294967295
//str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置
//str.find(str2,7),从str的pos号位开始匹配str2,返回值同上
string str2="you";
string str3="me";
if(str.find(str2)!=string::npos)
cout<<str.find(str2)<<endl;
if(str.find(str2,7)!=string::npos)
cout<<str.find(str2,7)<<endl;
if(str.find(str3)!=string::npos)
cout<<str.find(str3)<<endl;
else
cout<<"I know there is no position for me."<<endl;
//str.replace(pos,len,str2)把str从pos号位开始,长度为len的子串替换为str2
//str.replace(it1,it2,str3)把str的迭代器[it1,it)范围的子串替换为str2
str="Maybe you will turn around";
str2="will not";
str3="surely";
cout<<str.replace(10,4,str2)<<endl;
cout<<str.replace(str.begin(),str.begin()+5,str3)<<endl;
return 0;
}