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

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

  • 相关阅读:
    C#读取系统信息
    C# 读取驱动器盘符及信息
    for循环里的break,continue和return有什么差别
    monkey
    ZXing
    python中os模块的常用接口和异常中Exception的运用
    python中的字典应用实例
    python中的列表和字典
    python中如何单独测试一个函数的作用
    数据挖掘概念与技术PDF
  • 原文地址:https://www.cnblogs.com/maxv/p/10786143.html
Copyright © 2011-2022 走看看