//string对象的初始化
#include <iostream> #include <string> //typedef std::basic_string<char> string;
using namespace std; typedef string String; int main() { // with no arguments
string s1; //默认构造函数:没有参数或参数有默认值
String s2("hello"); //普通构造函数 String就是string
s1 = "Anatoliy"; //赋值运算符
String s3(s1); //拷贝构造函数 string s3 =s1;
cout << "s1 is: " << s1 << endl; cout << "s2 is: " << s2 << endl; cout << "s3 is: " << s2 << endl; // first argument C string // second number of characters
string s4("this is a C_sting", 10); cout << "s4 is: " << s4 << endl; // 1 - C++ string // 2 - start position // 3 - number of characters
string s5(s4, 6, 4); // copy word from s3 cout << "s5 is: " << s5 << endl; // 1 - number characters // 2 - character itself
string s6(15, '*'); cout << "s6 is: " << s6 << endl; // 1 - start iterator(迭代器 ) // 2 - end iterator(迭代器 )
string s7(s4.begin(), s4.end() - 5); cout << "s7 is: " << s7 << endl; // 通过=初始化string对象
string s8 = "Anatoliy"; cout << "s8 is: " << s8 << endl; string s9 = s1 + "hello"+ s2; //s1 + "hello"+ s2的结果是string类型的对象(变量)
cout << "s9 is: " << s9 << endl; return 0; }
s1 is: Anatoliy
s2 is: hello
s3 is: Anatoliy
s4 is: this is aC
s5 is: s aC
s6 is: ***************
s7 is: this
s8 is: Anatoliy
s9 is: Anatoliyhellohello