您的位置:首页 > 编程语言 > C语言/C++

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

2015-11-03 16:52 99 查看
初始化和定义

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


字符串操作

string s;
s.empty();
s.size();//返回值类型string::size_type
getline(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: