#include #include using namespace std; typedef string String; int main() { // 默认构造函数,没有参数 string s1; // 赋值运算 s1 = "hello"; cout << s1 << endl; // 拷贝构造函数,等价于 string s2 = "hello"; string s2("hello"); cout << s2 << endl; // 拷贝构造函数 String s3("world!"); cout << s3 << endl; // args: (C string, number of chars) string s4("this is a C string", 10); // output is "this is a " cout << "s4 is " << s4 << endl; // args: (C++ string, start position, number of characters) string s5(s4, 3, 3); cout << "s5 is " << s5 << endl; // args: (number of characters, char) string s6(6, '#'); cout << "s6 is " << s6 << endl; // args: (start, end) string s7(s4.begin(), s4.end()-3); cout << "s7 is " << s7 << endl; string s8 = s7 + s6; cout << "s8 is " << s8 << endl; for(int i=0; i!=s8.size(); i++) { cout << i << " " << s8[i] << " "; } cout << endl; // 常量迭代器const_iterator;变量迭代器 iterator string::const_iterator ii; int i = 0; for(ii=s8.begin(); ii!=s8.end(); ii++) { cout << i++ << " " << *ii << endl; } return 0; }