zoukankan      html  css  js  c++  java
  • C++STL学习笔记_(1)string知识

    /*============================================
    string是STL的字符串类型,通常用来表示字符串  =
    ============================================*/
    #include <iostream>
    using namespace std;
    #include "string"//string的头文件
    #include "algorithm"
    
    /*============================================
    string对象的初始化                           =
    ============================================*/
    void main21()
    {
        string s1 = "aaa";     //第一种初始化方法
        string s2("bbb");     //第二种初始化的方法
        string s3 = s2;      //通过拷贝构造函数来初始化对象s3
        string s4(10,'a');  //用n个字符c初始化
    
        cout<<"s1:"<<s1<<endl;
        cout<<"s2:"<<s2<<endl;
        cout<<"s3:"<<s3<<endl;
        cout<<"s4:"<<s4<<endl;
    
    }
    /*============================================
    string的遍历                                 =
    ============================================*/
    void main22()
    {
        string s1 = "abcdefg";
        //1.数组方式
        for (int i = 0;i<s1.length();i++)                           
        {
            cout<<s1[i]<<" ";//出现错误不抛异常,引起程序的中断
        }
        //2.通过迭代器的方式
        for (string::iterator it = s1.begin();it != s1.end();it++) 
        {
            cout<<*it<<" ";
        }
        cout<<endl;
        //3.string的at方式
        for (int i = 0;i<s1.length();i++)
        {
            cout<<s1.at(i)<<" ";//可以抛出异常
        }
        try
        {
            for (int i = 0;i<s1.length() + 3;i++)
            {
                cout<<s1.at(i)<<" "<<endl;//可以抛出异常
            }
        }
        catch (...)
        {
            cout<<"发生异常"<<endl;
        }
    }
    
    //字符指针和string的转换
    void main23()
    {
        string s1 = "aaa bbb";//char*====>string
    
        //s1====>char *
        printf("s1:%s
    ",s1.c_str());
    
        //char*====>string
    
        //s1的内容拷贝到buf中
        char buf1[128] = {0};
        s1.copy(buf1,3,0);//注意,只给你copy3个字符 不会变为C风格的字符串
        cout <<"buf1:"<<buf1<<endl;
    }
    
    //链接字符串
    void main24()
    {
        string s1 = "aaa";
        string s2 = "bbb";
        s1        = s1 + s2;
        cout<<"s1:"<<s1<<endl;
    
        string s3 = "333";
        string s4 = "444";
        s3.append(s4);
        cout<<"s3:"<<s3<<endl;
    }
    
    //字符串的查找和替换
    void main25()
    {
        string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
        //第一次出现wbm的 index
        int index = s1.find("wbm",0);//位置下标是从0开始的
        cout<<"index:"<<index<<endl;
    
        //求wbm每次出现的数组下标
        int offindex = s1.find("wbm",0);
        while(offindex != string::npos)
        {
            cout<<"offindex:"<<offindex<<endl;
            offindex = offindex + 1;
            offindex = s1.find("wbm",offindex);
        }
    
        //案例2
        string s3    = "aaa bbb ccc";
        s3.replace(0,3,"AAA");
        cout<<"s3:"<<s3<<endl;
    
    
        offindex        = s1.find("wbm",0);
        while(offindex != string::npos)
        {
            cout<<"offindex:"<<offindex<<endl;
            s1.replace(offindex,3,"WBM");
            offindex = offindex + 1;
            offindex = s1.find("wbm",offindex);
        }
        cout <<"s1:"<<s1<<endl;
    }
    
    //截断(区间删除)和插入
    void main26()
    {
        string s1            = "hello hello2 hello1";
        //string &eras(int pos = 0,int n = pos);删除pos开始的n个字符,返回修改后的字符串
        string::iterator it = find(s1.begin(),s1.end(),'l');
        if (it != s1.end())
        {
            s1.erase(it);
        }
        cout<<"s1删除以后的结果:"<<s1<<endl;
    
        s1.erase(s1.begin(),s1.end());
        cout<<"s1删除以后的结果:"<<s1<<endl;
        cout<<"s1长度:"<<s1.length()<<endl;
        
        string s2 = "BBB";
        s2.insert(0,"AAA"); //头插法
        cout<<"s2结果:"<<s2<<endl;
        s2.insert(s2.length(),"CCC"); //尾插法
        cout<<"s2结果:"<<s2<<endl;
    }
    
    void main27()
    {
        string s1 = "AAAbbb";
        //1.函数的入口地址 2.函数对象 3.预定的函数
        transform(s1.begin(),s1.end(),s1.begin(),toupper); //全部变为大写
        cout <<"s1:"<<s1 <<endl;
    
        string s2 = "AAAbbb";
        transform(s1.begin(),s1.end(),s2.begin(),tolower); //全部变为小写
        cout <<"s2:"<< s2 <<endl;
    }
    int main()
    {
        //main21();
        //main22();
        //main23();
        //main24();
        //main25();
        //main26();
        main27();
        return 0;
    }
  • 相关阅读:
    【mybatis源码学习】缓存机制
    【maven】命令
    【传输协议】thrift的IDL语法
    【传输协议】thrift原理
    Go 语言极速入门
    SOFABolt 源码分析
    !!!后续博客写到简书 + 博客园留博客目录
    第一章 java nio三大组件与使用姿势
    netty源码解析目录
    mac下host配置 + mac修改了环境变量却不生效:zsh: command not found: xxx
  • 原文地址:https://www.cnblogs.com/wuchuanying/p/6227885.html
Copyright © 2011-2022 走看看