23 C++ 中的字符串类

1 历史遗留问题

  • C 语言不支持真正意义上的字符串
  • C 语言用字符数组一组函数实现字符串操作
  • C 语言不支持自定义类型,因此无法获得字符串类型

2 解决方案

  • 从 C 到 C++ 的进化引入了自定义类型
  • 在 C++ 中可以通过类完成字符串类型的定义
  • 【问题】C++ 中的原生类型系统是否包含字符串类型?
    • 不包含

3 标准库中的字符串类

  • C++ 语言直接支持 C 语言的所有概念

  • C++ 语言中没有原生的字符串类型

  • C++ 标准库提供了 string 类型

    • string 直接支持字符串连接
    • string 直接支持字符串的大小比较
    • string 直接支持字串查找和提取
    • string 直接支持字符串的插入和替换
  • 示例1:字符串类的使用

    • Demo

      #include <iostream>
      #include <string>
      
      using namespace std;
      
      void string_sort(string a[], int len)
      {
          // 选择排序
          for(int i = 0; i < len; i++) {
              for(int j = i; j < len; j++) {
                  if( a[i] > a[j] ) {  // 字符串比较
                      swap(a[i], a[j]);
                  }
              }
          }
      }
      
      string string_add(string a[], int len)
      {
          string ret = "";
          
          for(int i = 0; i < len; i++) {
              ret += a[i] + "; ";  // 字符串相加
          }
          
          return ret;
      }
      
      int main()
      {
          string sa[6] = {
              "Hello World",
              "C#",
              "Java",
              "C++",
              "Python",
              "TypeScript"
          };
          
          string_sort(sa, 6);
          
          for(int i = 0; i < 6; i++) {
              cout << sa[i] << endl;
          }
          
          cout << endl;
          
          cout << string_add(sa, 6) << endl;
          
          return 0;
      }
      
    • 编译运行

      C#
      C++
      Hello world
      Java
      Python
      TypeScript
      
      C#; C++; Hello world; Java; Python; TypeScript; 
      

4 字符串和数字的转换

  • 标准库中提供了相关的类对字符串和数字进行转换

  • 字符串流类(sstream)用于 string 的转换

  • <sstream> :相关头文件

  • istringstream :字符串输入流

  • ostringstream :字符串输出流

  • 使用方法

    • string => 数字

      istringstream iss("123.45");
      double num;
      iss >> num;  //返回值为bool类型,true为转换成功,false为转换失败
      
    • 数字 => string

      sstringstream oss;
      oss << 543.21;  //返回值为oss本身
      string s = oss.str();
      
  • 示例2:字符串和数字的转换

    • Demo

      #include <iostream>
      #include <sstream>
      #include <string>
      
      using namespace std;
      
      
      #define TO_NUMBER(s, n) (istringstream(s) >> n)
      #define TO_STRING(n) (((ostringstream&)(ostringstream() << n)).str())
      
      int main()
      {
          istringstream iss("234.567");
          double num;
          if(iss >> num) {
              cout << num << endl;  // 234.567
          }
          
          iss("abcdefg");
          if(iss >> num) {
              cout << num << endl;
          }
          
          ostringstream oss;
          oss << 543 << "." << 21;
          string s = oss.str();
          cout << s << endl;
          
      
          double n = 0;  
          if( TO_NUMBER("234.567", n) ) {
              cout << n << endl;    
          }   
          if( TO_NUMBER("abcdefg", n) ) {
              cout << n << endl;    
          }
      
          string s1 = TO_STRING(12345);
          cout << s1 << endl;  // 12345  
          
          return 0;
      }
      
  • 编译运行

    234.567
    12345
    

5 字符串循环右移

  • 示例:字符串 abcdefg 循环右移 3 位后得到 efgabcd

  • Demo

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // 函数
    string right_func(const string& s,unsigned int n)
    {
        string ret = "";
        unsigned int pos = 0;
        
        // abc ==>1 cab
        // abc ==>3 abc ==> cab
        n = n % s.length();
        pos = s.length() - n;
        ret = s.substr(pos);     // 取出要放在最前面的子字符串 
        ret += s.substr(0,pos);  // 将剩余的子字符串放在后面
    }
    
    // 重载右移操作符
    string operator >> (const string& s, unsigned int n)
    {
        string ret = "";
        unsigned int pos = 0;
        
        n = n % s.length();
        pos = s.length() - n;
        ret = s.substr(pos);
        ret += s.substr(0, pos);
        
        return ret;
    }
    
    int main()
    {
        string s1 = right_func("abcdefg",8);
        cout << s1 << endl;     // gabcdef
        
        string s2 = ("abcdefg" >> 3); 
        cout << s2 << endl;     // efgabcd
        
        return 0;
    }
    
原文地址:https://www.cnblogs.com/bky-hbq/p/13903843.html