zoukankan      html  css  js  c++  java
  • string 用法

    参考:https://blog.csdn.net/y990041769/article/details/8763366


    1:string对象的定义和初始化以及读写

    string s1;      默认构造函数,s1为空串

    string s2(s1);   将s2初始化为s1的一个副本

    string s3("valuee");   将s3初始化一个字符串面值副本

    string s4(n,'c');   将s4 初始化为字符'c'的n个副本

    cin>>s5;  读取有效字符到遇到空格

    getline(cin,s6);  读取字符到遇到换行,空格可读入,知道‘ ’结束(练习在下一个代码中),

    getline(cin,s7,'a'); 一个直到‘a’结束,其中任何字符包括' '都能够读入

    2:string对象操作

    s.empty()  判断是否为空,bool型

    s.size() 或 s.length() 返回字符的个数

    s[n]  返回位置为n的字符,从0开始计数

    s1+s2 连接,看下面例子:

        可用此方法给字符串后面添加字符如:s=s+'a'; 

        a:  string s2=s1+", ";  //对,把一个string对象和一个字符面值连接起来是允许的

        b:  string s4="hello "+", ";   //错,不能将两个字符串面值相加

        c:  string s5=s1+", "+"world";   //对,前面两个相加相当于一个string对象;

        d:  string s6="hello" + ", " +  s2;  //错

    (注:字符串尾部追加还可用s.append("abc")函数添加)

    s1=s2  替换

    s1==s2  相等,返回true或false

    !=,<,<=,>,>=  字符串比较,两个字符串短的与长的前面匹配,短的小于长的

    3:string对象中字符的处理(头文件cctype)

        isalnum(c)  如果c是字母或数字,返回 true

        isalpha(c)  如果c是字母,返回true

        iscntrl(c)  c是控制符,返回true

        isdigit(c)  如果c是数组,返回true

        isgraph(c)  如果c不是空格,则可打印,,则为true

        islower(c)  如果c是小写字母,则为true

        isupper(c)  如果c是大写字符,则为true

        isprint(c)  如果c是可打印的字符,则为true

        ispunct(c)  如果c是标点符号,则为true

        isspace(c) 如果c是空白字符,则为true

        isxdigit(c) 如果c是十六进制数,则为true

        tolower(c) 如果c是大写字符,则返回其小写字母,否则直接返回c

        toupper(c)  跟tolower相反

    4:string对象中一些函数

    /*-------------------------插入函数----------------------------------包括迭代器操作和下标操作,下标操作更灵活*/

    s.insert( it , p );  把字符串p插入到it的位置

    s.insert(p,n,t);   迭代器p元素之前插入n个t的副本

    s.insert(p,b,e);     迭代器p元素之前插入迭代器b到e之间的所有元素

    s.insert(p,s2,poe2,len); 在下标p之前插入s2下标从poe2开始长度为len的元素

    s.insert(pos,cp,len);  下标pos之前插入cp数组的前len个元素。

    /*-----------------------替换函数-------------------------------*/

    s.assign(b,e); 用迭代器b到e范围内的元素替换s

    s.assign(n,t); 用n个t的副本替换s

    a.assign(s1,pos2,len);从s1的下标pos2开始连续替换len个。

    s.replace ( 3 , 3 , " good " ) ;   从第三个起连续三个替换为good

    s.substr(i,j)   截取s串中从i到j的子串  //string::npos  判断字符串是否结束

    /*-----------------------删除函数-----------------------------*/

    s.erase( 3 )||s.erase ( 0 , 4 ) ;  删除第四个元素或第一到第五个元素

    /*----------------------其他函数-----------------------------*/

    s.find ( " cat " ) ;  超找第一个出现的字符串”cat“,返回其下标值,查不到返回 4294967295,也可查找字符;

    s.append(args); 将args接到s的后面

    s.compare ( " good " ) ;  s与”good“比较相等返回0,比"good"大返回1,小则返回-1;

    reverse ( s.begin(), s.end () );  反向排序函数,即字符串反转函数

    6:string与数值的相互转换

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <numeric>
    #include <vector>
    #include <cstdio>
    #include <sstream>
    using namespace std;
     
    //c++方法:将数值转换为string
    string convert_to_string(double x)
    {
        ostringstream o;
        if(o << x)
            return o.str();
        return "conversion error";
    }
    //c++方法,将string转化为数值
    double convert_from_string(const string &s)
    {
        istringstream i(s);
        double x;
        if(i >> x)
            return x;
        return 0.0;
    }
    int main(int argc,char *argv[])
    {
        //将数值转换为string的第一种方法:c方法
        char b[10];
        string a;
        sprintf(b,"%d",1975);  //数值转化为string
        a=b;
        cout<<a<<endl;
     
        string cc=convert_to_string(1976);
        cout<<cc<<endl;
     
        string dd="115165";
        int p=convert_from_string(dd)+2;
        cout<<p<<endl;
        return 0;
    }

    推荐一些字符串的题目:
    hdoj 2017 字符串中统计数字,直接调用上面s.digit()函数
    hdoj 1020  判断输出重复、水题、
    hdoj 1062 逆转字符串 注意1:getchar()吸收3后' ',2:空格不止有一个
    hdoj 1039,字符串处理,清晰思路,可以写三个判断条件的3个函数,调用函数判断,思路清晰,容易判断;
    hdoj 1088 对字符串按一个一个处理。一次性输入一行不好控制
    hdoj 1113 map容器+字典序。值得做
    hdoj 1161 tolower() 函数转化为小写就ok
    1200、1251、1256、1288、1321、1328、1379、1804、1860、 1982、1984、2017、2024、2025、2026、2027、2043、2052、2054、2072、2074、2087、2131、 2137、2140、2163、2203、2206、2352、2500、2549、2564、2565、2567、2572、2609、2607、 2707、2708、2719、2721、2723、

    感谢我参考的那篇博客的博主,收获良多,希望也帮助了此时正在看这篇博客的你。

  • 相关阅读:
    luoguP1080 国王游戏 题解(NOIP2012)(贪心+高精)
    luoguP1079 Vigenère 密码 题解(NOIP2012)
    luoguP2184 贪婪大陆 题解(树状数组)
    luoguP2680 运输计划 题解(二分答案+树上差分)
    树链剖分总结
    树上差分总结
    luoguP3258 [JLOI2014]松鼠的新家 题解(树上差分)
    简单差分(保证你一看就懂)
    luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)
    luoguP1541 乌龟棋 题解(NOIP2010)
  • 原文地址:https://www.cnblogs.com/liubilan/p/9377421.html
Copyright © 2011-2022 走看看