C++ primer 学习笔记——字符串

来源:互联网 发布:日剧电影推荐 知乎 编辑:程序博客网 时间:2024/05/16 09:02

初始化和定义

string s1;string s2=s1;string s1("hello");//直接初始化string s2="hello";//拷贝初始化

字符串操作

string s;s.empty();s.size();//返回值类型string::size_typegetline(cin,s);//参数:流参数,字符串;返回s 的流参数s1+s2;//+俩侧必须有一项为string对象,字符串面值不是s1==s2;s1>s2;

字符串上字符操作

for(declaration:experssion)    statement

下标运算符 [],参数类型string::size_type

#include<iostream>#include<string>#include<cctype>using std::string;using std::cout;int main(){    string s("hello world ");    decltype(s.size()) punch_cnt = 0;//设置punch_cnt的类型为string::size_type    //for (auto c : s)    //{    //  if (ispunct(c))    //      ++punch_cnt;    //}    for (auto &c : s)        c = toupper(c);    cout << s;    cout << punch_cnt;    return 0;}
0 0
原创粉丝点击