码迷,mamicode.com
首页 > 编程语言 > 详细

C++学习笔记(7)标准库string类

时间:2015-07-01 17:39:57      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

一、初始化string对象:

      直接初始化:string a("value");

      拷贝初始化:string a = "value";

二、读写string对象

      注:cin会忽略头尾空白处,保留空白符需要使用getline;

      empty函数判断是否为空,size函数计算字符串长度。

      不能把多个字面值直接相加赋值给string对象,字符串字面值不是string对象

三、范围for语句的使用

      

    string str("some,string!!!");
    for(auto c : str)
    {
        cout<< c << endl;//逐行打印每个字符
    }
    decltype(str.size()) punct_cnt = 0;
    for(auto i : str)
    {
        if (ispunct(i))
        {
            ++punct_cnt;
        }

    }
    cout<< punct_cnt<<endl;    //输出标点符号数4
  

  for(auto &b : str)
  {
    b = toupper(b); //小写变大写
  }
  cout<<str<<endl;

 

 

C++学习笔记(7)标准库string类

标签:

原文地址:http://www.cnblogs.com/Charles-Wang/p/4613641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!