zoukankan      html  css  js  c++  java
  • STL string 常见用法详解

    string 常见用法详解

    1. string 的定义

    //定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可
    string str;
    //如果要初始化,可以直接给string类型的变量进行赋值
    string str = "abcd";
    

    2. string 中内容的访问

    **(1) 通过下标访问 **

    //一般来说,可以直接像字符数组那样去访问string
    #include <stdio.h>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcd";
        for(int i = 0; i < str.length(); i++) {
            printf("%c ", str[i]);  //输出abcd
        }
        return 0;
    }
    
    //如果要读入和输出整个字符串,则只能用 cin 和 cout
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        strng str;
        cin >> str;
        cout << str;
        return 0;
    }
    
    //使用printf来输出string
    //即用c_str()将stringelixir转换为字符数组进行输出
    #include <stdio.h>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcd";
        printf("%s
    ", str.c_str());    //将string型str使用c_str()变为字符数组
        return 0;
    }
    

    **(2) 通过迭代器访问 **

    //定义迭代器
    string::iterator it;
    //通过*it来访问string里的每一位
    #include <stdio.h>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcd";
        for(string::iterator it = str.begin(); it != str.end(); it++) {
            printf("%c", *it);
        }
        return 0;
    }
    //string和vector一样,支持直接对迭代器进行加减某个数字
    

    3. string 常用函数实例解析

    **(1) operator+= **

    //这是string的加法,可以将两个string直接拼接起来
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string str1 = "abc",  str2 = "xyz", str3;
        str3 = str1 + str2; //将str1和str2拼接,赋值给str3
        str1 += str2;   //将str2直接拼接到str1上
        cout << str1 << endl;
        cout << str3 << endl;
        return 0;
    }
    

    **(2) compare operator **

    //两个string类型可以直接使用==, !=, <, <=, >, >=比较大小
    //比较规则是字典序
    #include <stdio.h>
    #include <string>
    using namespace std;
    int main() {
        string str1 = "aa", str2 = "aaa", str3 = "abc", str4 = "xyz";
        if(str1 < str2) printf("ok1
    ");    //如果str1字典序小于str2,输出ok1
        if(str1 != str3) printf("ok2
    ");   //如果str1字典序不等于str3,输出ok2
        if(str4 >= str3) printf("ok3
    ");   //如果str4字典序大于等于str3,输出ok3
        return 0;
    }
    

    (3) length()/size()

    //length()返回string长度,即存放的字符数,时间复杂度为O(1)
    //size() 与 length()基本相同
    string str = "abcxyz";
    printf("%d %d
    ", str.length(), str.size());
    

    (4) insert()

    //string的insert()函数有很多种写法,这里给出几个常用的写法时间复杂度为O(N)
    
    // 1. insert(pos, string), 在pos号位置插入字符串string
    string str = "abcxyz", str2 = "opq";
    str.insert(3, str2); //往str[3]处插入opq,这里str2的位置直接写"opq"也是可以的
    cont << str << endl;
    
    // 2. insert(it, it2, it3)
    //it 为原字符串的欲插入位置, it2 和 it3 为待插字符串的首尾迭代器,用来表示串[it2, it3)将被插在it的位置上
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcxyz", str2 = "opq";    //str是原字符串,str2是待插字符串
        //string的3号位(即c和x之间)插入str2
        str.insert(str.begin() + 3, str2.begin(), str2.end());
        cout << str << endl;
        return 0;
     }
    

    **(5) erase() **

     //erase()有两种用法:删除单个元素,删除一个区间的所有元素。时间复杂度均为O(N)
    
     // 1. 删除单个元素
     //str.erase(it)用于删除单个元素,it为需要删除的元素的迭代器
     #include <iostream>
     #include <string>
     using namespace std;
     int main() {
        string str = "abcdefg";
        str.erase(str.begin() + 4); //删除4号位(即 e)
        cout << str << endl;
        return 0;
    }
    
    // 2. 删除一个区间内的所有元素
    //删除一个区间内的所有元素有两种方法
    //str.erase(first, last)
    //其中first为需要删除的区间的其实迭代器,而last则为需要删除的的区间的末尾迭代器的下一个地址
    //即为删除[first, last)
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcdefg";
        //删除[str.begin() + 2, str.end() - 1)】内的元素,即cedf
        str.erase(str.begin() + 2, str.end() - 1);
        cout << str << endl;
        return 0;
    }
    
    //str.erase(pos, length)
    //其中pos为需要开始删除的起始位置,length为删除的字符个数
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcdefg";
        str.erase(3, 2);    //删除从3号位开始的2个字符,即de
        cout << str << endl;
        return 0;
    }
    

    **(6) clear() **

    //clear()用以清空string中的数据,时间复杂度一般为O(1)
    #include <stdio.h>
    #include <string>
    using namespace std;
    int main() {
        string str = "abcd";
        str.clear();    //清空字符串
        printf("%d
    ", str.length());
        return 0;
    }
    

    **(7) substr() **

    //substr(pos, len)返回从pos号位开始,长度为len的字串,时间复杂度为O(N)
    #include <iostream>
    #include <string>
    using namespace std
    int main() {
        string str = "Thank you for your smile.";
        cout << str.substr(0, 5) << endl;
        cout << str.substr(14, 4) << endl;
        cout << str.substr(19, 5) << endl;
    }
    

    (8) string::npos

    //string::npos是一个常数,其本身的值为-1
    //但由于是unsigned_int类型,因此实际上也可以认为是unsigned_int类型的最大值
    //string::npos用以作为find函数失配时的返回值。
    //下面实例中可以认为string::pos等于-1或者4295967295
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        if(string::npos == -1) {
            cout << "-1 is true." << endl;
        }
        if(string::npos == 4294967295) {
            cout << "4294967295 is also true."  << endl;
        }
        return 0;
    }
    

    (9) find()

    //str::find(str2), 当str2是str的子串时,返回其在str中第一次出现的位置
    //如果str2不是str的子串,那么返回string::npos
    //str::find(str2, pos), 从str的pos号位开始匹配str2, 返回值与上相同
    //时间复杂度为O(nm),其中n和m分别为str和str2的长度
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string str = "Thank you for your smile.";
        string str2 = "you";
        string str3 ="me";
        if(str.find(str2) != string::npos) {
            cout << str.find(str2) << endl;
        }
        if(str.find(str2, 7) != string::npos) {
            cout << str.find(str2, 7) << endl;
        }
        if(str.find(str3) != string::npos) {
            cout << str.find(str) << endl;
        } else {
            cout << "I know there is no position for me." << endl;
        }
        return 0;
    }
    

    (10) replace()

    //str.replace(pos, len, str2)把str从pos号位开始,长度为len的子串替换为str2
    //str.replace(it1, it2, str2)把str的迭代器[it1, it2)范围的子串替换为str2
    //时间复杂度为O(str.length())
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string str = "Maybe you will turn around.";
        string str2 = "will not";
        string str3 = "surely";
        cout << str.replace(10, 4, str2) << endl;
        cout << str.replace(str.begin(), str.begin() + 5, str3) << endl;
        return 0;
    }
    
  • 相关阅读:
    dotNet程序保护方案
    网络数据包捕获函数库Libpcap安装与使用(非常强大)
    Objectivec 中 nil, Nil, NULL和NSNull的区别
    对象的相等和恒等
    IOS SDK介绍
    iOS内存管理编程指南
    http权威指南读书笔记(三)——http报文
    http权威指南学习笔记(二)
    http权威指南读书笔记(一)
    CentOS 设置环境变量
  • 原文地址:https://www.cnblogs.com/isChenJY/p/11536674.html
Copyright © 2011-2022 走看看