zoukankan      html  css  js  c++  java
  • string函数详解(配案例)

    多说无益上码~

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int main()
    {
    //1.    字符串的创建
        string a(4,'a');
        cout<<a<<endl<<endl;
        //1.以 a 为原字符 4单位大小
        string b("bbbbbb");
        cout<<b<<endl<<endl;
        //2.任意大小的字符串
        string c(a,0,4) ;
        cout<<c<<endl<<endl;
        //3.把某一字符串的某一部分
        //(0位置开始4个长度)给c
        system("pause");
        system("cls");
    //2.   字符串的添加 append();
        int num=153;
        string d(a);//或者 d=d+a;
        d.append(b);
        cout<<d<<endl<<endl;
        //1.在d的末尾添加字符串a
        d.append(b,0,2);
        cout<<d<<endl<<endl;
        //2.在d的末尾添加字符串
        //b(0位置开始,2个长度)的数据
        /*貌似还有一种d.appenf(string,int)
        结尾添加前int个我编译器有问题?*/
        d.append(4,'~') ;
        cout<<d<<endl<<endl;
        //3.添加4个 ~ 字符
        //4. 也可以添加 char int 型到句尾(待找...貌似有点问题)
        system("pause");
        system("cls");
    //3. 字符串赋值 assign();
        string e,f("123456");
        e.assign(f);
        e+=' ';
        cout<<e<<endl<<endl;
        //感觉就像是append不过是抹除-覆盖
        e.assign(f,3,3);
        e+=' ';
        cout<<e<<endl<<endl;
        //类似
        /*貌似还有一种d.assign(string,int)
        赋值前int个我编译器有问题?*/
        e.assign(3,'6');
        cout<<e<<endl<<endl;
        //赋值3个6;
        system("pause");
        system("cls");
    //4.   at 只想 位置
        char fd;
        cout<<e<<endl;
        fd=e.at(2);
        cout<<fd<<endl<<endl;
        if(a.compare(0,2,b,0,2)==0)
            cout<<a<<endl<<endl;
        if(a.compare(b)<0)
            cout<<b<<endl<<endl;
        if(a.compare(0,2,"aaa",0,2)==0)
            cout<<"+++"<<endl<<endl;
        return 0;
    }

    先更新几个 占个位置嘛,嘻嘻.

  • 相关阅读:
    开关门(结构体)
    洗牌问题(找规律)
    七夕节(hd1215)干嘛今天做这题T_T
    三角形(hd1249)
    寒冰王座(hd1248)
    钱币兑换问题(hd1284)
    计算机模拟(hd1283)
    回文数猜想(hd1282)
    贪吃蛇代码
    变形课hd1181(DFS)
  • 原文地址:https://www.cnblogs.com/maxv/p/10786143.html
Copyright © 2011-2022 走看看