C++中string类的处理字符串构造函数用法, 以及getline cin.getline()输入的区别

//测试string的七个构造函数
 string one("Lottery Winner");
 cout<<one<<endl;
 string two(20,'$');    //20个元素的string对象,每个元素初始为$
 cout<<two<<endl;
 string three(one);    //复制构造函数
 cout<<three<<endl;
 one+=" Oops!";     //overload +=
 cout<<one<<endl;
 two="Sorry! That was ";
 three[0]='p';
 string four;
 four=two+three;     //overload + =
 cout<<four<<endl;
 char alls[]="All`s well that ends well";
 string five(alls,20);   //将five初始化为alls的前20个字符,即使超过了alls结尾也没事
 cout<<five<<endl;
 string six(alls+6,alls+10);  //将six初始化alls的[6,10)之间的字符,6包含,10不包含。注:这是下标
 cout<<six<<", ";
 string seven(&five[6],&five[10]);//同six
 cout<<seven<<"...\n";
 string eight(four,7,16);   //将eight初始化为four中,从位置7(下标)开始的16个字符,或者结尾;
 cout<<eight<<" in motion!"<<endl;


 

posted @ 2015-03-13 12:33  cbam  阅读(157)  评论(0编辑  收藏  举报