zoukankan      html  css  js  c++  java
  • STL之String容器

    摘要:本文主要介绍了字符串string容器的使用。

    1、简单介绍

    1.1概念

    C风格字符串(以空字符结尾的字符数组)太过复杂难于掌握,不适合大程序的开发,所以C++标准库定义了一种string类,定义在头文件<string>。

    1.2 String和c风格字符串的比较

    • Char*是一个指针,String是一个类,string封装了char*,管理这个字符串,是一个char*型的容器。
    • String封装了很多实用的成员方法,包括:查找find,拷贝copy,删除delete 替换replace,插入insert等。
    • 每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

    2、String类的使用总结

    2.1 构造函数

    string() 创建一个空的字符串 例如: string str
    string(const string& str)

    使用一个string对象初始化另一个string对象

    string(const char* s) 使用字符串s初始化
    string(int n, char c) 使用n个字符c初始化

    2.2 string基本赋值操作

    string& operator=(const char* s) char*类型字符串 赋值给当前的字符串
    string& operator=(const string &s) 把字符串s赋给当前的字符串
    string& operator=(char c) 字符赋值给当前的字符串
    string& assign(const char *s) 把字符串s赋给当前的字符串
    string& assign(const char *s, int n) 把字符串s的前n个字符赋给当前的字符串
    string& assign(const string &s) 把字符串s赋给当前字符串
    string& assign(int n, char c) 用n个字符c赋给当前字符串
    string& assign(const string &s, int start, int n) 将s从start开始n个字符赋值给字符串

    2.3 string存取字符操作

    char& operator[](int n) 通过[]方式取字符
    char& at(int n) 通过at方法获取字符

    2.4 string拼接操作

    string& operator+=(const string& str) 重载+=操作符
    string& operator+=(const char* str) 重载+=操作符
    string& operator+=(const char c) 重载+=操作符
    string& append(const char *s) 把字符串s连接到当前字符串结尾
    string& append(const char *s, int n) 把字符串s的前n个字符连接到当前字符串结尾
    string& append(const string &s) 同operator+=()
    string& append(const string &s, int pos, int n) 把字符串s中从pos开始的n个字符连接到当前字符串结尾
    string& append(int n, char c) 在当前字符串结尾添加n个字符c

    2.5 string查找和替换

    int find(const string& str, int pos = 0) const 查找str第一次出现位置,从pos开始查找
    int find(const char* s, int pos = 0) const 查找s第一次出现位置,从pos开始查找
    int find(const char* s, int pos, int n) const 从pos位置查找s的前n个字符第一次位置
    int find(const char c, int pos = 0) const 查找字符c第一次出现位置
    int rfind(const string& str, int pos = npos) const 查找str最后一次位置,从pos开始查找
    int rfind(const char* s, int pos = npos) const 查找s最后一次出现位置,从pos开始查找
    int rfind(const char* s, int pos, int n) const 从pos查找s的前n个字符最后一次位置
    int rfind(const char c, int pos = 0) const 查找字符c最后一次出现位置
    string& replace(int pos, int n, const string& str) 替换从pos开始n个字符为字符串str
    string& replace(int pos, int n, const char* s) 替换从pos开始的n个字符为字符串s

    2.6 string比较操作

    compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

    int compare(const string &s) const 与字符串s比较
    int compare(const char *s) const 与字符串s比较

    2.7 string子串

    string substr(int pos = 0, int n = npos) const 返回由pos开始的n个字符组成的字符串

    2.8 string插入和删除操作

    string& insert(int pos, const char* s) 插入字符串
    string& insert(int pos, const string& str) 插入字符串
    string& insert(int pos, int n, char c) 在指定位置插入n个字符c
    string& erase(int pos, int n = npos) 删除从Pos开始的n个字符

    2.9 string和c-style字符串转换

    string 转 char* string str = "itcast"; const char* cstr = str.c_str();
    char* 转 string char* s = "itcast"; string str(s);

    提示:在c++中存在一个从const char*到string的隐式类型转换,却不存在从一个string对象到C_string的自动类型转换。对于string类型的字符串,可以通过c_str()函数返回string对象对应的C_string.通常,程序员在整个程序中应坚持使用string类对象,直到必须将内容转化为char*时才将其转换为C_string.

    3、代码示例

      1 #include<iostream>
      2 #include<string>
      3 
      4 using namespace std;
      5 
      6 void test01() {
      7     string str1;  //string类默认构造
      8     string str2(str1);  //string类的拷贝构造
      9     string str3 = str1; //调用的是拷贝构造函数
     10 
     11     string str4 = "abcd";
     12     string str5("abcd");
     13     string str6(10,'a');  //用10个a初始化str6
     14     cout << str4 << endl;
     15     cout << str5 << endl;
     16     cout << str6 << endl;
     17 
     18     str1.assign("abcde", 3); //将给定字符串的前3个取出赋值给str1
     19     cout << str1 << endl;
     20 
     21     string str7;
     22     str7.assign("abcdefg", 2, 3); //从第2个位置开始拿三个给str7赋值
     23     cout << str7 << endl;
     24 }
     25 
     26 void test02() {   //取字符操作
     27     string s = "hello world";
     28 
     29     for (int i=0;i<s.size();i++)
     30     {
     31         //cout << s[i] << endl;
     32         cout << s.at(i) << endl;
     33     }
     34     try   //这里是为了探讨[]和at的区别,前者直接死掉,后者系统指明位置无效
     35     {
     36         //cout << s[50] << endl;
     37         cout << s.at(50) << endl;
     38     }
     39     catch (out_of_range &e)
     40     {
     41         cout << e.what() << endl;
     42     }
     43 }
     44 
     45 void test03() {  //拼接,查找,替换
     46     string s1 = "";
     47     string s2 = "爱中国";
     48     s1 += s2;  //运算符重载
     49     cout << s1 << endl; 
     50     s1.append("山西");  //类内函数在当前字符串后加字符串
     51     cout << s1 << endl;
     52 
     53     string s3 = "abcdefg";
     54     int pos1 = s3.find("def");
     55     int pos2 = s3.rfind("def");  //两种方式均可以,只是查找方向不同而已
     56     cout << "pos1=" << pos1 << endl;
     57     cout << "pos2=" << pos2 << endl;
     58 
     59     string s4 = "hello";
     60     s4.replace(1, 2, "哪吒大战悟空");  //注意是段替换
     61     cout << s4 << endl;
     62 }
     63 
     64 void test04() {   //字符串比较
     65     string s1 = "abc";
     66     string s2 = "abcd";
     67     if (s1.compare(s2)==0)  
     68     {
     69         cout << "s1和s2相等" << endl;
     70     }
     71     else if (s1.compare(s2)==1)
     72     {
     73         cout << "s1大于s2" << endl;
     74     }
     75     else {
     76         cout << "s1小于s2" << endl;
     77     }
     78 }
     79 
     80 void test05() {    //获取字符串的字串
     81     string s1 = "abcdefg";
     82     string s2 = s1.substr(2, 4);
     83     cout << s2 << endl;
     84 
     85     //举例从邮件中获取一个用户名
     86     string email = "liming@qq.com";
     87     int pos = email.find("@");
     88     string username = email.substr(0,pos);
     89     cout << username << endl;
     90 }
     91 
     92 void test06() {    //字符串的插入、删除
     93     string s1 = "hello";
     94     s1.insert(2,"  abc  ");
     95     cout << s1 << endl;
     96 
     97     s1.erase(2,7);
     98     cout << s1<<endl;
     99 }
    100 
    101 //char *类型的字符串和string类型的字符串的转换
    102 void func1(string s) {
    103     cout << s << endl;
    104 }
    105 void func2(const char *s) {
    106     cout << s << endl;
    107 }
    108 void test07() {
    109     string s = "abc";
    110     const char *p = s.c_str();  //注意const不能少
    111     func2(p);
    112 
    113     const char *pp = "abcdefg";  //注意const不能少
    114     string ss(pp);
    115     func1(ss);
    116 }
    117 
    118 void test08()
    119 {
    120     string s = "abcde";
    121     char& a = s[2];
    122     char& b = s[3];
    123 
    124     a = '1';
    125     b = '2';
    126 
    127     cout << s << endl;
    128     cout << (int*)s.c_str() << endl; //注意这里打印的是地址
    129 
    130     s = "ppppppppppppppppp";
    131 
    132     a = '1';  //此时无法修改,原因是经过了内存的重新分配
    133     b = '2';  //此时a,b还指向原来的地址,所以出错
    134 
    135     cout << s << endl;
    136     cout << (int*)s.c_str() << endl;
    137 
    138 }
    139 
    140 void test09() {   //变大写和变小写
    141     string s = "abCdEfg";
    142 
    143     for (int i = 0; i < s.size(); i++)
    144     {
    145         //s[i] = toupper(s[i]);
    146 
    147         //全变小写
    148         s[i] = tolower(s[i]);
    149     }
    150     cout << s << endl;
    151 }
    152 
    153 int main() {
    154     //test01();
    155     //test02();
    156     //test03();
    157     //test04();
    158     //test05();
    159     //test06();
    160     //test07();
    161     //test08();
    162     test09();
    163     system("pause");
    164     return 0;
    165 }
  • 相关阅读:
    cube.js 基于http 通道的数据实时更新bug 解决
    cube.js 基于http 通道的数据实时更新
    cube.js websocket 实时数据更新说明
    phpspy 进行php 项目性能分析
    使用groovy 下载maven依赖包
    java 几个不错的代码生成工具
    语言性能分析工具总结
    使用rbspy 分析ruby应该性能
    dremio v18.0 软件包可以使用了
    nginx rewrite查询字符串重写问题
  • 原文地址:https://www.cnblogs.com/lzy820260594/p/11363002.html
Copyright © 2011-2022 走看看