zoukankan      html  css  js  c++  java
  • STL---string

    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        //string构造函数
        string str1;
        cout << str1.c_str() << endl; //c_str 返回一个const char *的头指针
    
        string str2(5, 'a'); //第一个参数是一个size_t 的类型,第二个参数是一个字符类型
        cout << str2.c_str() << endl; 
    
        string str3("luojianyi"); //第二种构造函数
        cout << str3.c_str() << endl;
    
        string str4("abcdefg", 3); //第一个参数是const char *的类型,然后第二个是取得第一个参数的前几个
        cout << str4.c_str() << endl;
    
        string str5(str3, 1, 5); //第一个参数是一个string类型,第二和第三个参数是一个范围
        cout << str5.c_str() << endl;
    
        string str6(str3); //拷贝构造函数
        cout << str6.c_str() << endl;
    
        //string 的重载运算符
        string str7;
        cin >> str7;
        cout << str7 << endl;
        cout << str7[3] << endl;
        cout << str7.at(3) << endl;
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        string str1(5, 'w');
        //指定下标的修改
        str1[1] = 'a';
        str1.at(2) = 'b';
        cout << str1.c_str() << endl;
        
    
        //中间插入
          //way 1
        string str2("abcdefg");
        string str3("lllll");
        str2.insert(2,str3); //在str2下标为2的地方插入str3
        cout << str2.c_str() << endl;
    
          //way 2
        string str4("abcdef");
        string str5("abcdefg");
        str4.insert(3, str5, 0, 2); //将str5的0  1 下标插入到str4的下标的3位置
        cout << str4.c_str() << endl;
          //way 3
        string str6("abcdef");
        str6.insert(3, 5, 'v');  //在str6下标为3的地方插入5个‘v’
        cout << str6.c_str() << endl;
    
        
        //尾巴插入
         //way 1 重载+=运算符
        string str7("aaaaa");
        string str8("bbbbb");
        str7 += str8;
        cout << str7.c_str() << endl;
        str7 += "ccccc";
        cout << str7.c_str() << endl;
         //append
        string str9("aaaaa");
        str9.append("bbbbb");
        cout << str9.c_str() << endl;
        //append 还有其他添加方式,需要的时候可以去查询
    
        //重新赋值
            // 重新 =
            // cin>>
            // 使用assign方法
    
        //eraser方法
        string str10("abcdefg");
        str10.erase(2, 3); //从下标为2的地方擦除3个元素
        cout << str10.c_str() << endl;
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        string str1("abc");
        string str2("abcd");
        //比较函数 重载运算符 
        cout << (str1 > str2) << endl; //这里使用运算符进行比较的时候需要加上括号
        //compare 函数
        cout << str1.compare(str2) << endl;
    
        //比较函数的定义
            //从两个字符串第一个开始按照ascii码进行比较
            //abc 和 abcd 前三个字符都是相等的,的那个比较第四个字符的时候,前面的字符串已经没有了,所以后面一个字符串大
            //abc 和 av  当比较到第二个字符的时候,v 大于 b 所以后面一个字符串大
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        //查找
        string str1("abcdef");
        string str2("bc");
        cout << str1.find(str2, 0) << endl; //从str1的下标0开始寻找str2
        
        cout << str1.find(str2, 2) << endl; //从str1的下标2开始寻找str2
        cout << (int)str1.find(str2, 2) << endl; //将上面一条语句返回的结果强转为int
    
    
        //返回子串
        string str3("abcdefg");
        cout << str3.substr(2, 3) << endl; //从下标为2开始返回三个字符子串
    
        //交换
        string str4("abc");
        string str5("abcde");
        str5.swap(str4);
        cout << str4 << endl;
        cout << str5 << endl;
    
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        //使用迭代器遍历string结构
        string str1("123456789");
        string::iterator ite;
    
        //way 1
        ite = str1.begin();
        for (rsize_t i = 0; i < str1.size(); i++)
        {
            cout << *ite << " ";
            ite++;
        }
        cout << endl;
    
        //way 2
        ite = str1.begin();
        for (rsize_t i = 0; i < str1.size(); i++)
        {
            cout << ite[i] << " ";
        }
        cout << endl;
    
        //way 3
        for (ite = str1.begin(); ite != str1.end(); ite++)
        {
            cout << *ite << " ";
        }
        cout << endl;
    
        //补充 我们可以通过迭代器访问元素,也可以使用迭代器修改元素
        string str2("aaaaaaaaaaa");
        string::iterator ite1;
        ite1 = str2.begin();
        ite1[3] = 'b';
        cout << str2 << endl;
    
        //跌倒器失效,但我们给一个迭代器绑定了一个容器,当容器扩容的时候其实就是相当于重新申请了内存空间,这个时候我们原来绑定的迭代器就会失效,需要重新绑定
        return 0;
    }
    View Code
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        string str1("abc");
        string str2("def");
        str1.append(str2.begin(), str2.begin() + 3);
        cout << str1 << endl;
    
    
        string str3("abc");
        string str4("def");
        str3.append(str4.begin(), str4.end());
        cout << str3 << endl;
    
    
        return 0;
    }
    View Code
    #include<iostream>
    #include<string>
    #include<algorithm> 
    #include<functional>
    using namespace std;
    
    void fun(char ch)
    {
        cout << ch << " ";
    }
    int main()
    {
        string str1("37428797934748");
        for_each(str1.begin(), str1.end(), fun);
        //前两个参数是范围,第三个参数是一个操作函数
        //使用了一个算法 所以要包含 algorithm 头文件
        cout << endl;
    
        //排序
        sort(str1.begin(), str1.end());  //默认从小到大排序
        for_each(str1.begin(), str1.end(), fun);
        cout << endl;
    
        sort(str1.begin(), str1.end(), greater<char>());
        //第三个参数是仿函数 需要添加一个头文件 functional
        for_each(str1.begin(), str1.end(), fun);
        cout << endl;
    
    
        return 0;
    }
    View Code
  • 相关阅读:
    java中的CAS
    浅谈尾递归
    LeetCode 第 222 场周赛
    LeetCode 第 216 场周赛
    C++代码整洁之道
    LeetCode 第 211 场周赛
    LeetCode 每日一题 51.N 皇后
    LeetCode 第 201 场周赛
    LeetCode 第 32 场双周赛
    [转]演讲实录丨DevOps之十倍速原则
  • 原文地址:https://www.cnblogs.com/luojianyi/p/9614879.html
Copyright © 2011-2022 走看看