zoukankan      html  css  js  c++  java
  • c++string类的简单介绍

    #include "iostream"
    #include "string"
    using namespace std;
    /*@author:浅滩
    *family:c++中string类的简单介绍
    *time:2019.2.12
    */
    int main()
    {
    string s;//string类的创建
    /*下述均是string的成员函数*/ 
    s = "123";//=直接进行赋值 
    cout <<"字符串长度为:"<< s.size() << endl;//返回字符串s的长度
    s += "125"; //在原来的尾部加上新字符串
    cout << "字符串s为:"<< s << endl;
    cout << s.empty() << endl;       //判断字符串是否为空,非空返回0 
    cout << "子串在字符串位置为:"<< s.find("125") << endl;//查找子串在字符串的位置
    cout << "插入之后新字符串为:"<< s.insert(0,"999")<< endl;//在原字符串第0位后插入字符串"110" 
    //insert 用法很多这只是其中一种而已,在此不做过多赘述 
    cout <<"符的最大可能个数为:"<< s.max_size () <<endl;//返回字符的最大可能个数
    string s1="999123125";
    cout << (s == s1 ? 1 : 0) << endl; //string类可以用==,! =,<,<=,>,>=,compare()比较字符串内容 
    string s2 = s1.substr(0,3);//提取字符串s1从0开始字符长度为3的子串赋值给s2
    cout << s1.substr()<<endl; //由此可以看出s1.substr()默认参数传的是s1.substr(0,s1.length()); 
    cout << "字符串s2为:"<<s2 << endl;
    //用法一:用str替换指定字符串从起始位置pos开始长度为len的字符 
        //  string& replace (size_t pos, size_t len, const string& str); 
         
        cout << '101' ;//输出为A,101(八进制数)转化为十进制数为65,而A的ascll码就是65,
        //
      
    }
    不一样的烟火
  • 相关阅读:
    WordPress网站绑定多个域名的方法
    htpasswd 命令使用
    在Windows下用OpenSSL生成证书步骤
    WCF中关于List和数据的转换问题
    NET2.0的配置文件
    C# Attribute
    c#自定义属性
    VS2005中读写配置文件(方法二)
    c#的反射
    Asp.NET 操作配置文件 Steven Pei 博客园
  • 原文地址:https://www.cnblogs.com/cstdio1/p/10884978.html
Copyright © 2011-2022 走看看