zoukankan      html  css  js  c++  java
  • C++ >Chapter3 标准库类型之string 类型

    一、代码 :LibType_String.cc

       主要熟悉标准库string类型的初始化、定义、常见的操作等。

    #include <iostream>
    #include <cctype>
    using namespace std;
    int main ()
    {
        /*1.define and intilatize a string object*/
        string s1;               // default Constructor
        cout<<"s1-->"+s1<<endl;
        string s2(s1);
        cout<<"s2-->"+s2<<endl;
        string s3("hello");
        cout<<"s3-->"+s3<<endl;
        string s4(3,'a');
        cout<<"s4-->"+s4<<endl;
    
        /*2.read and write a string object*/
        /*************************************************************
          *read and ignore all blank characters(e.g.space,newline,tabs)
          *read characters until encounter space,and stop 
        **************************************************************/
        string s;
        cin>>s;                 //read whitespace-separated into s;
        cout<<s<<endl;
        cin.clear();
        cin.sync();
        string s5,s6;
        cin>>s5>>s6;
        cout<<"s5-->"+s5<<" s6-->"+s6<<endl;
    
        /*3.read full line text,use getline*/
            string line;
        //read line at time untile end-of-file
        while(getline(cin,line))
            cout<<line<<endl;
    
        /*4.operations of a string object*/
        string s7("hello");
        string s8("world!");
        cout<<"the length of s7 is "+s7.size()<<endl;
        cout<<"is s8 empty"<<s8.empty()<<endl;
        cout<<"second character in s7 is "+ s7[1]<<endl;
        bool b = true;
        b = s7!=s8;
        cout<<"result of comparing s7 with s8-> "+s7<<b<<endl;
        b = s7==s8;
    
        cout<<"result of comparing s7 with s8-> "<<b<<endl;
        b = s7>=s8;
        cout<<"result of comparing s7 with s8 ->"<<b<<endl;
        b= s7<=s8;
        cout<<"result of comparing s7 with s8 ->"<<b<<endl;
        b = s7>s8;
        cout<<"result of comparing s7 with s8 ->"<<b<<endl;
        b = s7<s8;
        cout<<"result of comparing s7 with s8 ->"<<b<<endl;
        b=isalpha('a');
            cout<<"a is or not a alpha->"<<b<<endl;
        return 0;
        
    }

    二、编译、连接、运行结果:

    三、string对象中字符处理函数

         这些函数包含在cctype头文件中,如下:

    • isalnum(c) 如果c是字母或数字,则为true。
    • isalpha(c) 如果c是字母,则为true。
    • iscntrl(c) 如果c是控制字符,则为true。
    • isdigit(c) 如果c是数字,则为true。
    • isgraph(c) 如果c不是空格,但可以打印,则为true。
    • islower(c) 如果c是小写字母,则为true。
    • isaprint(c) 如果c是可打印字符,则为true。
    • ispunct(c) 如果c是标点符号,则为true。
    • isspace(c) 如果c是空白字符,则为true。
    • isupper(c) 如果c是大写字母,则为true。
    • isxdigit(c) 如果c是十六进制数,则为true。
    • tolower(c) 如果c是大写字母,则返回其小写形式,否则直接返回c。
    • toupper(c) 如果c是小写字母,则返回其大写形式,否则直接返回c。 
  • 相关阅读:
    设置IIS7/IIS7.5的FTP支持断点续传
    win10图片打开方式里没有默认照片查看器的解决方法
    使用C#的HttpWebRequest模拟登陆网站
    在Raid模式下装Win10找不到固态硬盘怎么办
    C# String 前面不足位数补零的方法
    [SQL Server]无法创建 SSIS 运行时对象,请验证 DTS.dll 是否可用及是否已注册
    HTTPS协议说明
    技术晨读_20160217
    技术晨读_20160215
    fastcgi安装
  • 原文地址:https://www.cnblogs.com/mizhongqin/p/C_plus_plus_StandardLibraryType_string.html
Copyright © 2011-2022 走看看