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;
    }

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

  • 相关阅读:
    Android layout属性大全
    如何看懂Code128条形码
    二维码
    在线条形码生成器
    GS1已分配给国家(地区)编码组织的前缀码
    POJ 3321 Apple Tree DFS序+fenwick
    bootstrap之WaitForIdle&amp;&amp;Clear
    ubuntu14操作系统chrome标签和书签乱码解决
    动态规划-hdoj-4832-百度之星2014初赛第二场
    截取符合指数分布的一部分样本的理论与实验
  • 原文地址:https://www.cnblogs.com/maxv/p/10786143.html
Copyright © 2011-2022 走看看