string构造函数及使用:
#include <iostream>
#include <string>
int main()
{
using namespace std;
string one("Lottery Winner!"); // string(const char *)
cout << one << endl;
string two(20 , '$'); // string(整形,char c),初始化一个string对象全为$
cout << two << endl;
string three(one); // string(const string & )
cout << three << endl;
one += "Oops!"; // 重载了+=运算符
cout << one << endl;
three[0] = 'p'; // []重载运算符
string four; // string()构造一个长度为零的字符串
four = two + three; // +重载运算符
cout << four << endl;
char alls[] = "All's well that ends well";
string five(alls,20); // string(const char * , n),n超过字符串仍然复制相应的个数的字符
cout << five << "!\n";
string six(alls + 6 , alls + 10);
cout << six << ","; // string(Iter begin , Iter end),初始化区间[begin , end)内的字符,类似指针
string seven(&alls[6] , &alls[10]); // 同上
cout << seven << "...\n";
string eight(four , 7 , 16); // string(cosnt string & , n = 0 , n = npos).其中string::npos为字符串的最大长度
cout << eight << " inmotion" << endl;
return 0;
}
结果