zoukankan      html  css  js  c++  java
  • string容器

    一、string特性

    string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的,string与char*都可以用来表示字符串。

    说到string的特性,就不得不和char*类型的字符串对比:

    1、char*是一个指针,string是一个类

    string封装了char*,管理这个字符串,是一个char*型的容器。

    2、string封装了很多实用的成员方法

    查找find,拷贝copy,删除delete,替换replace,插入insert

    3、不用考虑内存释放和越界

    string管理char*所分配的内存,每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

    4、string和char*可以相互转换,string转char*通过string提供的c_str()方法。

    //string转char*
    string str=“itcast”;
    const char* cstr=str.c_str();
    //char*转string
    char* s=“itcast”;
    string sstr(s);

    二、string初始化、拼接、赋值、查找、替换、比较、子串、插入和删除

    1、string构造函数

    用string初始化字符串分两类:用“=”号就是拷贝初始化,否则就是直接初始化。

    默认构造函数:
    string(); //构造一个空的字符串string s1。
    拷贝构造函数:
    string(const string &str); //构造一个与str一样的string。如string s1(s2)。
    带参数的构造函数
    string(const char *s); //用字符串s初始化
    string(int n,char c); //用n个字符c初始化

     例子:

    string s1;//初始化字符串,空字符串
    string s2 = s1; //拷贝初始化,深拷贝字符串
    string s3 = "I am Yasuo"; //直接初始化,s3存了字符串
    string s4(10, 'a'); //s4存的字符串是aaaaaaaaaa
    string s5(s4); //拷贝初始化,深拷贝字符串
    string s6("I am Ali"); //直接初始化
    string s7 = string(6, 'c'); //拷贝初始化,cccccc

    2、string存取字符操作

    char& operator[](int n);//通过[]方式取字符
    char& at(int n);//通过at方法获取字符
    //例子:
    string s="itcast";
    char c=s[1];
    c=s.at(1);

    3、string基本赋值操作

     

    4、string拼接

    5、string查找和替换

    6、string比较

    7、string子串

    8、string插入和删除

    三、案例

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <string>
    using namespace std;
    
    //string初始化
    void test01()
    {
        string s1;//调用无参构造,初始化字符串,空字符串
        string s2(10, 'a');
        string s3("abcdefg");
        string s4(s3);
    
        cout << s1 << endl;//空字符串
        cout << s2 << endl;//aaaaaaaaaa
        cout << s3 << endl;//abcdefg
        cout << s4 << endl;//abcdefg
    }
    
    //string赋值操作
    void test02()
    {
        string s1;//调用无参构造,初始化字符串,空字符串
        string s2("appp");
        s1 = "abcdefg";
        cout << s1 << endl;
        s1 = s2;
        cout << s1 << endl;
        s1 = "a";
        cout << s1 << endl;
    
        //成员方法
        s1.assign("jkl");
        cout << s1 << endl;
    }
    
    //取值操作
    void test03()
    {
        string s1 = "abcdefg";
    
        //重载[]操作符
        for (int i = 0;i < s1.size();i++)
        {
            cout << s1[i] << " ";
        }
        cout << endl;
    
        //at成员函数
        for (int i = 0;i < s1.size();i++)
        {
            cout << s1.at(i) << " ";
        }
        cout << endl;
    
        //这两种方法的区别:
        //[]方式 如果访问越界,直接挂了
        //at方式 访问越界 抛异常out_of_range
    
        try {
            cout << s1.at(100) << endl;
        }
        catch (...) {
            cout << "越界!" << endl;
        }
    }
    
    //string拼接操作
    void test04()
    {
        string s = "abcd";
        string s2 = "1111";
        s += "abcd";
        s += s2;
        cout << s << endl;//abcdabcd1111
    
        string s3 = "2222";
        s2.append(s3);
        cout << s2 << endl;//11112222
    
        string s4 = s2 + s3;
        cout << s4 << endl;//111122222222
        
    }
    
    //string查找操作
    void test05()
    {
        string s = "abcdefghjfgkl";
        //查找第一次出现的位置
        int pos = s.find("fg");
        cout << "pos:" << pos << endl;//pos:5
    
        //查找最后一次出现的位置
        pos = s.rfind("fg");
        cout << "pos:" << pos << endl;//pos:9
    }
    
    //string替换操作
    void test06()
    {
        string s = "abcdefg";
        s.replace(0, 2, "111");
        cout << s << endl;//111cdefg
    }
    
    //string比较操作:
    //compare函数在>时返回1,<时返回-1,==时返回0.
    //比较区分大小写,比较时参考字典顺序,排越前面的越小。
    //大写的A比小写的a小。
    void test07()
    {
        string s1 = "abcd";
        string s2 = "abed";
        if (s1.compare(s2) == 0)
        {
            cout << "s1与s2相等" << endl;
        }
        else if(s1.compare(s2) > 0)
        {
            cout << "s1大于s2" << endl;
        }
        else
        {
            cout << "s1小于s2" << endl;//s1小于s2
        }
    }
    
    //string子串操作
    void test08()
    {
        string s = "abcdefg";
        string mysubstr = s.substr(1, 3);
        cout << mysubstr << endl;//bcd
    }
    
    //string插入和删除操作
    void test09()
    {
        string s = "abcdefg";
        s.insert(3, "111");
        cout << s << endl;//abc111defg
    
        s.erase(0, 2);
        cout << s << endl;//c111defg
    }
    
    int main(void)
    {
        //test01();
        //test02();
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
        //test08();
        test09();
    
        return 0;
    }

     部分参考了:https://blog.csdn.net/zyq522376829/article/details/46792893

  • 相关阅读:
    【五月每日总结】
    【Codeforces Round #405 ( Div 2)】题解
    【Codeforces Round #406 (Div. 2)】题解
    【HUD-5790】Prefix (主席树+tire)
    【倍增】LCM QUERY
    【hackerrank】Week of Code 30
    【Codeforces Round #404 (Div. 2)】题解
    【数论】逆元
    mongodb复制集部署文档
    合并SQL 调优
  • 原文地址:https://www.cnblogs.com/yuehouse/p/10087767.html
Copyright © 2011-2022 走看看