标准库string类型

曾经终败给现在 2021-12-14 00:27 361阅读 0赞

一、string 对象的定义和初始化的方式

  1. 1 string s1
  2. 2 string s2s1);
  3. 3 string s3"hello");
  4. 4 string s4n'c'); //将s4初始化为字符'c'的n个副本

二、string 对象的读写

  1. 1、读入未知数目的string对象
  2. 例: int main()
  3. \{
  4. string word;
  5. while(cin>>word)
  6. cout<<word<<endl;
  7. return 0;
  8. \}
  9. 2、用getline读取整行文本
  10. getline函数接受二个参数:一个输入流对象和一个string对象.遇到换行符停止
  11. 例:int main()
  12. \{
  13. string line;
  14. while(getline(cin,line))
  15. cout<<line<<endl;
  16. return 0;
  17. \}

三、string 对象中字符的处理

  1. string对象中的单个字符处理的的函数一般都在cctype头文件中
  2. cctype中定义的函数:
  3. isalnum(c) //若c是字母或数字,则为true。
  4. isalpha(c) //若c是字母。则为true。
  5. iscntrl(c) //若c是控制字符,则为true。
  6. isdigit(c) //若c是数字,则为true。
  7. isgraph(c) //若c不是空格,但可打印,则为true。
  8. islower(c) //若c是小写字母,则为true。
  9. ispunct(c) //若c是标点符号,则为true。
  10. isspace(c) //若c是空白字符,则为true。
  11. isupper(c) //若c是大写字母,则为true。
  12. isxdigit(c) //若c是十六进制数,则为true。
  13. tolower(c) //若c是大写字母,则返回小写字母形式,否则直接返回c。
  14. toupper(c) //若c是小写字母,则返回其大写字母形式,否则直接返回c。
  15. 例:统计字符串中标点符号的个数
  16. string s("hello world!!!!");
  17. string::size\_type punct\_cnt=0;
  18. for(string::size\_type index=0;index!=s.size();+index)
  19. if(ispunct(s\[index\]))
  20. ++punct\_cnt;
  21. cout<<punct\_cnt<<endl;
  22. 整理一下这些东西,相信以后用得着。。。。。

转载于:https://www.cnblogs.com/james1207/p/3260350.html

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读