zoukankan      html  css  js  c++  java
  • C++ string类学习总结

      C++字符串技术 string类

       string对象在大多数应用中被用来消除对char*指针的使用,支持所期望的一些操作;

      可以转换成char*,保持和现代代码的兼容性,还能自动处理内存管理;

      一些string的实现采用了引用计数,带来了比基于char*的字符串更佳的性能。

      大多数的编译器已经实现了C++标准的std::string类~

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
     
    /* stlstring.cpp
        C++字符串技术  string类
        string对象在大多数应用中被用来消除对char*指针的使用,支持所期望的一些操作;
        可以转换成char*,保持和现代代码的兼容性,还能自动处理内存管理;
        一些string的实现采用了引用计数,带来了比基于char*的字符串更佳的性能。
        大多数的编译器已经实现了C++标准的std::string类~
    */


    #include <iostream>
    #include <string>

    using namespace std;

    int main(void)
    {
        
    //构造字符串,给字符串赋值
        string myString1("Michael Joessy!");
        string myString2(
    10'q');
        string myString3 = 
    "Michael Jordan~";
        string myString4(myString1);
        string myString5 = myString3;

        cout << myString1 << endl;
        cout << myString2 << endl;
        cout << myString3 << endl;
        cout << myString4 << endl;
        cout << myString5 << endl;

        
    //字符串读入和读出
        //string::operator>>  string::operator<<    string::getline
        string str;
        
    //getline(cin, str, ' ');
        cout << str << endl;

        
    //字符串判断函数
        /*字符串判断为空函数*/
        string empString(
    "");
        
    if (empString.empty())
        {
            cout << 
    "empString is empty!" << endl;
        }
        
    /*字符串长度函数*/
        string testString = 
    "1234567890";
        cout << testString << endl;
        cout << 
    "testString length is " << testString.length() << endl;
        cout << 
    "testString size is " << testString.size() << endl;
        testString.resize(
    5);
        cout << testString << endl;
        cout << 
    "testString length is " << testString.length() << endl;
        cout << 
    "testString size is " << testString.size() << endl;
        
    /*提示:length()和size()的功能是一样的,size()是基于STL概念提出来的,而length()还是原来C语言的东西,
                为了更好地符合STL的编程概念,建议使用size()。
        */


        
    /*字符串指针函数*/
        string str1(
    "023");
        cout << 
    "str1 = " << str1 << endl;
        cout << 
    "str1 = " << str1.c_str() << endl;      //c_str()返回字符串的指针

        
    //增加字符串成员
        string strAppend("Michael ");
        cout << strAppend << endl;
        
    char szTest[] = "Jackson";
        strAppend.append(szTest);
        cout << strAppend << endl;
        string strInsert = 
    " great";
        strAppend.append(strInsert, 
    06);
        cout << strAppend << endl;
        strAppend.append(strInsert.begin(), strInsert.end());
        cout << strAppend << endl;

        strAppend += 
    " person";
        cout << strAppend << endl;

        strAppend.push_back(
    '!');
        cout << strAppend << endl;

        
    //字符串比较函数
        /*关系运算符 == > >= < <=*/
        string strt1 = 
    "Michael";
        string strt2 = 
    "Michael";
        
    if (strt1 == strt2)
        {
            cout << 
    "strt1 == strt2" << endl;
        }
        string strt3 = 
    "Michael Joessy";
        
    if (strt3 >= strt1)
        {
            cout << 
    "strt3 >= strt1" << endl;
        }
        
    /*compare*/
        
    if (strt1.compare(strt2) == 0)
        {
            cout << 
    "strt1 == strt2" << endl;
        }
        
    if (strt3.compare(strt1) != 0)
        {
            cout << 
    "strt3 != strt1" << endl;
        }

        
    //字符串查找
        string strfind("abcdefg");
        
    int nPos = 0;
        nPos = strfind.find(
    'c');
        cout << 
    "The char c pos is " << nPos << endl;
        
    char szArray[] = "def";
        nPos = strfind.find(szArray);
        cout << 
    "The szArray[] pos is " << nPos << endl;
        
    char szArrayNone[] = "drf";
        nPos = strfind.find(szArrayNone);
        cout << 
    "The szArrayNone[] pos is " << nPos << endl;

        nPos = strfind.rfind(
    'c');
        cout << 
    "The char c pos is " << nPos << endl;
        nPos = strfind.rfind(szArray);
        cout << 
    "The szArray[] pos is " << nPos << endl;
        nPos = strfind.rfind(szArrayNone);
        cout << 
    "The szArrayNone[] pos is " << nPos << endl;

        string strCha = 
    "aaabbbcccdddeeefffggghhh";
        nPos = strCha.find_first_of(
    'b');
        cout << 
    "The find_first_of b pos is " << nPos << endl;
        nPos = strCha.find_first_not_of(
    'b');
        cout << 
    "The find_first_not_of b pos is " << nPos << endl;
        nPos = strCha.find_last_of(
    'b');
        cout << 
    "The find_last_of b pos is " << nPos << endl;
        nPos = strCha.find_last_not_of(
    'b');
        cout << 
    "The find_last_not_of b pos is " << nPos << endl;

        
    //字符串下标和字串函数
        string strSub = "Hello World!";
        cout << strSub << endl;
        
    char ch1 = strSub[6];
        
    char ch2 = strSub.at(11);
        strSub.at(
    11) = '~';
        cout << strSub << endl;
        strSub.assign(
    "Hello Man!");
        cout << strSub << endl;

        cout << strSub.substr(
    69) << endl;

        
    //字符串其它函数
        string strOrg = "Hello Hero!";
        cout << strOrg << endl;
        strOrg.insert(
    0"At ");
        cout << strOrg << endl;
        cout << strOrg.capacity() << endl;
        cout << strOrg.size() << endl;
        strOrg.replace(
    02"Ha");
        cout << strOrg << endl;

        cin.get();
        
    return 0;
    }

  • 相关阅读:
    POJ 2752 KMP中next数组的理解
    KMP详解
    HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂
    POJ 3220 位运算+搜索
    反素数深度分析
    POJ 2886 线段树单点更新
    求反素数的方法
    CV第八课 GPU/CPU
    49. 字母异位词分组
    48. 旋转图像
  • 原文地址:https://www.cnblogs.com/MakeView660/p/7016238.html
Copyright © 2011-2022 走看看