string类中有没有定位函数?
也即从第几个字符开始取几个字符!比如说string str="asdfghjkkasdf";从第3个字符开始取4个字符就是"dfgh"
#include<iostream> #include<string> using namespace std; int main() { string str="asdfghjkkasdf"; int i, n; cin >> i >> n; string str2(str,i-1,n);//string::string(const string&,int index,int size) cout << str2 << endl; char cstr3[100]; //int string::copy(char* buf,int size, int index) //copy函数不在字符串结尾处写上'\0' str.copy(cstr3,n,i-1); cstr3[n] = '\0'; cout << cstr3 << endl; return 0; }