首页 > 编程语言 > 详细

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

时间:2015-07-01 17:37:01      阅读:152      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!