zoukankan      html  css  js  c++  java
  • C++字符串

    历史遗留问题:

      -C语言不支持真正意义上的字符串;

      -C语言字符数组和一组函数实现字符串操作;

      -C语言不支持在定义类型,因此无法获得字符串类型;

    solution:

      -从C到C++的进化过程引入了自定义类型;

      -在C++中可以通过类完成字符串类型的定义;

    question:

      C++中原生类型系统是否包含字符串类型?

    标准库中的字符串类型

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

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

    C++提供 了string类型

      -string直接支持字符串连接;

      -string直接支持字符串的大小比较;

      -string直接支持子串查找和替换;

      -string直接支持字符串的插入和替换;

    实例分析:

    字符串类的使用.cpp

     1 #include <iostream>
     2 #include <sstream>
     3 #include <string>
     4 
     5 using namespace std;
     6 
     7 
     8 #define TO_NUMBER(s, n) (istringstream(s) >> n)
     9 #define TO_STRING(n) (((ostringstream&)(ostringstream() << n)).str())
    10 
    11 int main()
    12 {
    13 
    14     double n = 0;
    15    
    16     if( TO_NUMBER("234.567", n) )
    17     {
    18         cout << n << endl;    
    19     }
    20 
    21     string s = TO_STRING(12345);
    22 
    23     cout << s << endl;     
    24     
    25     return 0;
    26 }

    实例:

    abcdefg 循环右移3位,得到 efgabcd

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 string operator >> (const string& s, unsigned int n)
     7 {
     8     string ret = "";
     9     unsigned int pos = 0;
    10     
    11     n = n % s.length();
    12     pos = s.length() - n;
    13     ret = s.substr(pos);
    14     ret += s.substr(0, pos);
    15     
    16     return ret;
    17 }
    18 
    19 int main()
    20 {
    21     string s = "abcdefg";
    22     string r = (s >> 3);
    23     
    24     cout << r << endl;
    25     
    26     return 0;
    27 }
  • 相关阅读:
    uva 11355(极角计算)
    hdu 1029(hash)
    hdu 1024(dp)
    SPOJ DISUBSTR(字符串hash)
    SPOJ DISUBSTR(后缀数组)
    【Leetcode】Evaluate Reverse Polish Notation
    【Leetcode】Reverse Words in a String
    【Leetcode】Maximum Product Subarray
    【Leedcode】Insertion Sort List
    【Leetcode】Sort List
  • 原文地址:https://www.cnblogs.com/lemaden/p/10119157.html
Copyright © 2011-2022 走看看