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,
        //
      
    }
    不一样的烟火
  • 相关阅读:
    Poj 2017 Speed Limit(水题)
    Poj 1316 Self Numbers(水题)
    Poj 1017 Packets(贪心策略)
    Poj 1017 Packets(贪心策略)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    Poj 2662,2909 Goldbach's Conjecture (素数判定)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2388 Who's in the Middle(快速排序求中位数)
    poj 2000 Gold Coins(水题)
    poj 2000 Gold Coins(水题)
  • 原文地址:https://www.cnblogs.com/cstdio1/p/10884978.html
Copyright © 2011-2022 走看看