zoukankan      html  css  js  c++  java
  • string

    初始化string对象的方式:

     1 string s1;//默认初始化,s1是一个空串
     2 
     3 string s2(s1);//s2是s1的副本
     4 
     5 string s2 = s1;//等价于s2(s1),s2是s1的副本
     6 
     7 string s3("hello");//s3是字面值"hello"的副本
     8 
     9 string s3 = "hello";//等价于s3("hello"),s3是字面值"hello"的副本
    10  
    11 string s4(10, 'c');//把s4初始化成连续10个字符'c'组成的串

    通过 “=” 初始化一个变量,实际上执行的是拷贝初始化。不使用 "=" 则执行的是直接初始化。

    string对象上的操作:

     1 os << s;//将s写到输出流os当中,返回os
     2 
     3 is >> s;//从 is 中读取字符串赋给s, 字符串以空白分隔,返回 is
     4 
     5 getline(is, s);//从 is 中读取一行赋给 s, 以换行分隔,返回 is
     6 
     7 s.empty();//s为空串返回true, 否则返回false
     8 
     9 s[n];//返回 s 中第 n 个字符的引用,位置 n 从 0 计起
    10 
    11 s1 + s2;//返回s1, s2连接后的结果
    12 
    13 s1 = s2;//用 s2 d的副本代替 s1 中原来的字符
    14 
    15 s1 == s2;//若s1与s2相等则返回true,否则返回 false
    16 
    17 s1 != s2;//若 s1 与 s2 不相等则返回 true, 否则返回 false
    18 
    19 <, <=, >, >=;//比较string对象字典序大小

    处理 string 对象中的字符

    cctype头文件中的函数

     1 isalnum(c);//当 c 时字母或数字时为真
     2 
     3 isalpha(c);//当 c 时字母时为真
     4 
     5 iscntrl(c);//当 c 时控制字符时为真
     6 
     7 isdigit(c);//当 c 时数字时为真
     8 
     9 isgraph(c);//当 c 不是空格但可打印时为真
    10 
    11 islower(c);//当 c 时小写字母时为真
    12 
    13 isupper(c);//当 c 是大写字母时为真
    14 
    15 tolower(c);//如果 c 是大写字母,输出对应的小写字母,否则原样输出 c 
    16 
    17 toupper(c);//如果 c 是小写字母,输出对应的大写字母,否则原样输出 c
    18 
    19 isxdigit(c);//当 c 是16进制数字时为真
    20 
    21 isprint(c);//当 c 是可打印字符时为真(即 c 是空格或 c 具有可视形式)
    22 
    23 ispunct(c);//当 c 是标点符号时为真(即 c 不是控制符,数字,字母,可打印空白中的一种)
    24 
    25 isspace(c);//当 c 是空白时为真(即 c 是空格,横向制表符,纵向制表符,回车符,换行符,进纸符中的一种)

    使用基于范围的 for 语句访问 string 对象中的字符

     1 string str("hello");
     2 for(auto indx : str){
     3     indx = toupper(indx);//indx不是引用,所以 str 对象没有被改变
     4 }
     5 cout << str << endl;//输出 hello
     6 
     7 for(auto &indx : str){
     8     indx = toupper(indx);//indx是引用,所以 str 中的字符被替换成了大写字符
     9 }
    10 cout << str << endl;//输出 HELLO
  • 相关阅读:
    POJ 1015 Jury Compromise【DP】
    POJ 1661 Help Jimmy【DP】
    HDU 1074 Doing Homework【状态压缩DP】
    HDU 1024 Max Sum Plus Plus【DP,最大m子段和】
    占坑补题。。最近占的坑有点多。。。
    Codeforces 659F Polycarp and Hay【BFS】
    Codeforces 659E New Reform【DFS】
    Codeforces 659D Bicycle Race【计算几何】
    廖大python实战项目第四天
    廖大python实战项目第三天
  • 原文地址:https://www.cnblogs.com/geloutingyu/p/7899578.html
Copyright © 2011-2022 走看看