• C++ STL之string常用指令


    string,大小可变的字符串,有些类似于C中的字符数组。

    只记载本人在ACM中常用的函数,并且全部经过程序测试。

    1、初始化

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

    string s2(s1);——将s2初始化为与s1相同

    string s3("aaa");——将s3初始化为aaa

    string s4(3, 'b');——将s4初始化为bbb

    2、输入输出

    能用cin,cout;不能用scanf,printf。

    用cin读入会忽略开头所有空白字符(如空格,换行符,制表符),读取字符直至再次遇到空白字符。

    用getline能整行读入(不会忽略前驱空格),读入得到的字符串末尾没有换行符。

    3、普通运算符

    s[n],s1 + s2,s1 = s2,s1 == s2,!=, <, <=, >, >=均保持它们惯有的含义

    4、insert插入

    s1.insert(迭代器, 单个字符);——如s1.insert(s1.begin(), 's');

    5、erase删除

    s1.erase(数字a, 数字b);——删除s1[a]开始,删除b个

    s1.erase(迭代器)——删除迭代器指示的那个元素

    s1.erase(迭代器a, 迭代器b)——删除迭代器a到迭代器b之间的所有元素,删除迭代器a指示元素,不删b

    6、clear清空

    7、repalce替换

    与erase的一三个用法相似,不过没有第二个用法

    8、empty字符串为空返回真,否则返回假

    9、substr函数,截取string中的一段。s = s.substr(a, b)则为将s变成自己的从第a位开始,长度为b的子串。(从第0位开始)比如s = "12345",s = s.substr(1, 3),则s = "234"。

    测试程序部分为(测试过程中不小心删掉了部分- -)

     1 /*
     2  * Author:  Plumrain
     3  * Created Time:  2013-09-05 15:53
     4  * File Name: string.cpp
     5  */
     6 #include<iostream>
     7 #include<cstdio>
     8 #include<cstring>
     9 #include<string>
    10 
    11 using namespace std;
    12 
    13 #define out(x) cout<<#x<<":"<<(x)<<endl
    14 #define tst(a) cout<<#a<<endl
    15 
    16 void test_1()
    17 {
    18     string s1;
    19     out (s1);
    20     s1 = "aaa";
    21     string s2(s1);
    22     out (s2);
    23     string s3("bbb");
    24     out (s3);
    25     string s4(3, 'c');
    26     out (s4);
    27 }
    28 
    29 void test_2()
    30 {
    31     string s1;
    32     cin >> s1;
    33     cout << s1 << endl;
    34     getline(cin, s1);
    35     cout << s1 << endl;
    36 }
    37 
    38 void test_3()
    39 {
    40     string s1 = "ccc";
    41     string s2 = "aaaaaaaa";
    42     s2 = s1;
    43     out (s2);
    44     out (s2.size());
    45     out (s1);
    46     out (s1.size());
    47 }
    48 
    49 void test_insert()
    50 {
    51     string s1 = "aaaaa";
    52     s1.insert (s1.begin(), 's');
    53     s1.insert (s1.end(), 's');
    54     s1.insert (s1.begin() + (s1.end()-s1.begin()) / 2, 91);
    55     out (s1); 
    56 }
    57 
    58 void test_erase()
    59 {
    60     string s1 = "abcdefghi";
    61 //    s1.erase(2, 3);
    62     s1.erase(s1.begin(), s1.end());
    63     out (s1.size());
    64     out (s1);
    65 }
    66 
    67 void test_replace()
    68 {
    69     string s = "abcdefghi";
    70     s.replace(2, 3, "rr");
    71     s.replace(s.begin(), s.end()-1, "rrr");
    72 //    s.replace(s.begin(), "rrr");
    73     out (s);
    74 }
    75 
    76 int main()
    77 {
    78     test_1 ();
    79     test_2 ();
    80     test_3 ();
    81     test_insert ();
    82     test_erase ();
    83     test_replace ();
    84     return 0;
    85 }
    View Test Code
    ------------------------------------------------------------------
    现在的你,在干什么呢?
    你是不是还记得,你说你想成为岩哥那样的人。
  • 相关阅读:
    python2.7 目录下没有scripts
    pycharm配置appium 提示unsrsloved reference
    查看cookie的快捷方法
    在线linux 平台
    selenium配置Chrome驱动
    ImportError: sys.meta_path is None, Python is likely shutting down
    基本控件文档-UISwitch属性---iOS-Apple苹果官方文档翻译
    基本控件文档-UISlider属性---iOS-Apple苹果官方文档翻译
    基本控件文档-UISegment属性----iOS-Apple苹果官方文档翻译
    基本控件文档-UILabel属性---iOS-Apple苹果官方文档翻译
  • 原文地址:https://www.cnblogs.com/plumrain/p/stl_string.html
Copyright © 2020-2023  润新知