//c++中字符串的处理获取一行函数 #include <iostream> using namespace std; int main() { string s; getline(cin,s); cout<<s<<endl; }
//c++中对于操作符重载的一些现象 #include <iostream> using namespace std; int main() { string s1,s2; int a=5; s1+="hello";//根据c++的重载,会将hello加入到字符串中 s1+=" world!" ;//与上面的规则相同 s1+='b';//可以加入单独的字符,不一定为字符串 s1+=98;//如果加数字的话将会对应的看作为ascii码,从而转换为对应的字符 cout<<s1<<endl; s2+=(a+'0');//想把常量a所代表的数字加到字符串中 cout<<s2<<endl; return 0; }
//c++中字符串的排序 #include <iostream> #include<algorithm> using namespace std; int main() { string s="adefcbg"; cout<<*s.begin()<<endl;//输出字符串的第一个字母 cout<<*--s.end()<<endl;//s.end()代表了字符串的末尾‘/0',因此需要前移一位才能读到‘g’ sort(s.begin(),s.end());//定位好开始和结束的位置之后即可以实现排序 cout<<s<<endl; return 0; }