對C++中string類型的總結

string類對象的構造

簡化構造函數原型如下(注意,爲了簡便,把模板中最後一個默認參數省略了):

1:  explicit basic_string();
2:  string(const char *s);
3:  string(const char *s, size_type n);
4:  string(const string& str);
5:  string(const string& str, size_type pos, size_type n);
6:  string(size_type n, E c);
7:  string(const_iterator first, const_iterator last);

string對象的操作

  1. 字符串比較

            支持六種關係運算符(==、!=、>、>=、<、<=),其採用字典排序策略(與C中字符串比較策略完全一樣)。這六個關係運算符是非成員的重載運算符。而這些運算符都支持三種操作數組合:string op string、string op const char*、const char* op string(其中op是前面六種關係運算符中任意一種)。解釋:提供運算符的三種重載版本主要是從效率角度考慮的,其避免了臨時string對象的產生。

            另外,string類還提供了各種重載版本的成員函數compare來比較,簡化函數原型爲:

    1:  int compare(const string& str) const;
    2:  int compare(size_type p0, size_type n0, const string& str);
    3:  int compare(size_type p0, size_type n0, const string& str, size_type pos, size_type n);
    4:  int compare(const char* s) const;
    5:  int compare(size_type p0, size_type n0, const char* s) const;
    6:  int compare(size_type p0, size_type n0, const char* s, size_type n) const;
                 返回值:如果調用該函數的對象的比較序列小於操作數比較序列,則返回負數;若相等,則返回0;否則,返回正數。

     

  2. 字符串相加 
           

            針對string類提供了非成員重載operator+,支持string對象之間、string對象與const char*對象之間、string對象與char對象之間相加,並且operator + 兩邊的操作數的任意順序都支持。簡化函數原型如下: 
           

    1:  string operator+ (const string& lhs, const string& rhs);
    2:  string operator+ (const string& lhs, const char *rhs);
    3:  string operator+ (const string& lhs, char rhs);
    4:  string operator+ (const char *lhs, const string& rhs);
    5:  string operator+ (char lhs, const string& rhs);

     

  3. 字符串賦值 
           

            字符串賦值有兩種方式:一是利用成員重載運算符operator=;另外就是使用成員重載函數assign可以更加靈活地處理。這裏只提供簡化函數原型供參考: 

    1:  string& operator=(char c);
    2:  string& operator=(const char *s);
    3:  string& operator=(const string& rhs);
    4:  string& assign(const char *s);
    5:  string& assign(const char *s, size_type n);
    6:  string& assign(const string& str, size_type pos, size_type n);
    7:  string& assign(const string& str);
    8:  string& assign(size_type n, char c);
    9:  string& assign(const_iterator first, const_iterator last);

     

  4. 字符串追加 

            字符串追加同樣有兩種方式:一是operator+=;另外就是成員函數append。簡化函數原型如下: 

    1:  string& operator+=(char c);
    2:  string& operator+=(const char *s);
    3:  string& operator+=(const string& rhs);
    4:  string& append(const char *s);
    5:  string& append(const char *s, size_type n);
    6:  string& append(const string& str, size_type pos, size_type n);
    7:  string& append(const string& str);
    8:  string& append(size_type n, char c);
    9:  string& append(const_iterator first, const_iterator last);

     

  5. 讀取子串 

            獲取某個下標處的字符:一是用at成員函數;另外就是用operator[]。獲取子串,可以用成員函數c_str及substr,還有成員函數data和copy。簡化函數原型如下: 

    1:  reference operator[](size_type pos);
    2:  const_reference operator[](size_type pos) const;
    3:  reference at(size_type pos);
    4:  const_reference at(size_type pos) const;
    5:   
    6:  const char *c_str() const;
    7:  const char *data() const;
    8:  string substr(size_type pos = 0, size_type n = npos) const;
    9:  size_type copy(char *s, size_type n, size_type pos = 0) const;

            注意:若at函數的參數pos無效,則拋出異常out_of_range;但如果operator[]的參數pos無效,則屬於未定義行爲。所以at比operator[]更加安全。

            其中,copy返回實際拷貝的字符數。 

  6. 替換子串 

            成員函數replace實現替換某個子串。簡化函數原型如下: 

     1:  string& replace(size_type p0, size_type n0, const char *s);
     2:  string& replace(size_type p0, size_type n0, const char *s, size_type n);
     3:  string& replace(size_type p0, size_type n0, const string& str);
     4:  string& replace(size_type p0, size_type n0, const string& str, size_type pos, size_type n);
     5:  string& replace(size_type p0, size_type n0, size_type n, char c);
     6:  string& replace(iterator first0, iterator last0, const char *s);
     7:  string& replace(iterator first0, iterator last0, const char *s, size_type n);
     8:  string& replace(iterator first0, iterator last0, const string& str);
     9:  string& replace(iterator first0, iterator last0, size_type n, char c);
    10:  string& replace(iterator first0, iterator last0, const_iterator first, const_iterator last);

             這裏,可能需要用到這幾個函數得到整個字符序列: 

    1:  const_iterator begin() const;
    2:  iterator begin();
    3:  const_iterator end() const;
    4:  iterator end();

     

  7. 插入字符串 

            成員函數insert實現在某點處插入字符串。簡化函數原型如下: 

    1:  string& insert(size_type p0, const char *s);
    2:  string& insert(size_type p0, const char *s, size_type n);
    3:  string& insert(size_type p0, const string& str);
    4:  string& insert(size_type p0, const string& str, size_type pos, size_type n);
    5:  string& insert(size_type p0, size_type n, char c);
    6:  iterator insert(iterator it, char c);
    7:  void insert(iterator it, const_iterator first, const_iterator last);
    8:  void insert(iterator it, size_type n, char c);

            注意:insert函數是在插入點(p0 or it)之前插入字符串。 

  8. 刪除子串 

            成員函數 erase實現刪除某個子串。簡化函數原型如下: 

    1:  iterator erase(iterator first, iterator last);
    2:  iterator erase(iterator it);
    3:  string& erase(size_type p0 = 0, size_type n = npos);

            如果指定刪除的字符個數比字符串中從指定位置開始的剩餘字符個數還多,那麼只有這些字符被刪除。


  9. 查找子串 

            查找子串有六種方式,分別有五類成員函數與之應。

                    · find 查找控制字符序列中與操作字符序列匹配的第一個子串,並返回子串的起始位置;

                    · rfind 查找控制字符序列中與操作字符序列匹配的最後一個子串,並返回該子串的起始位置,相當於逆向查找;

                    · find_first_of 查找控制字符序列中第一個出現在操作字符序列中的字符的位置,並返回該位置;

                    · find_first_not_of查找控制字符序列中第一個不出現在操作字符序列中的字符的位置,並返回該位置;

                    · find_last_of 查找控制字符序列中最後一個出現在操作序列中的字符的位置,並返回該位置;

                    · find_last_not_of 查找控制字符序列中最後一個不出現在操作字符序列中的字符位置,並返回該位置;

             如果這些函數查找失敗,則返回string::npos。

             其中,find函數的簡化函數原型如下: 

    1:  size_type find(char c, size_type pos = 0) const;
    2:  size_type find(const char *s, size_type pos = 0) const;
    3:  size_type find(const char *s, size_type pos, size_type n) const;
    4:  size_type find(const string& str, size_type pos = 0) const;

            另外的五個函數的函數原型和find的函數原型類型類似,區別在於,如果是是逆序查找的函數(rfind, find_last_of, find_last_not_of),則pos參數默認值爲npos。


  10. 其它成員函數和友元函數 

     1:  size_type capacity() const;   // 返回當前字符串的存儲空間大小>=size()
     2:  void reserve(size_type n = 0);// 預留n個元素的存儲空間,保證capacity()>=n
     3:  bool empty() const;           // 若字符串爲空,返回true
     4:  size_type size() const;       // 返回字符串長?
     5:  size_type length() const;     // 等於size()
     6:  size_type max_size() const;   //返回string類中字符串的最大長度
     7:  void resize(size_type n, char c = ' '); //若長度不夠,則用c填充加長的部分;保證size()返回n
     8:  void swap(string& str);      //兩string對象交換,能在常數時間內完成(必須是使用相同allocator的兩對象,這裏都使用的默認的)
     9:  
    10:  // 其它非成員函數
    11:  istream& getline(istream& is, string& str);
    12:  istream& getline(istream& is, string& str, char delim);
    13:  ostream& operator<<(ostream& os, const string& str);
    14:  istream& operator>>(istream& is, const string& str);

            其中,istream& getline(istream& is, string& str); 相當於 istream& getline(istream& is, string& str, char delim = '\n');

            getline函數在下列三種情況下結束提取:

                    1)遇到文件結束符;

                    2)遇到分隔符delim。如果第一個就是分隔符,str爲空串(並且該分隔符被從流中讀出丟棄),但istream測試爲真;

                    3)如果已經提取了istream.max_size()個字符,那麼提取結束,並且將調用istream.setstate(ios_base::failbit),即此時返回的istream測試爲假。

            如果函數沒有提取到字符(包括分隔符),那麼將調用istream.setstate(failbit),此時測試istream爲假。

            默認情況下, istream& operator>>(istream& is, const string& str);在下列三種情況下結束提取: 
                    1)遇到文件結束符; 
                    2)遇到空白字符(空格、Tab、換行); 
                    3)如果已經提取了is.max_size()個字符,或者提取了is.width()(非0情況下)個字符。 
            如果沒有提取到任何非文件結束符的字符(包括空白字符),那麼將調用istream.setstate(failbit),此時測試istream爲假。

            例如,看看下面的循環:

    1:  while(cin >> word) 
    2:  { 
    3:      cout << "word read is: " << word << '\n'; 
    4:  }

     

            要中止上面的循環應該用文件結束符:  
                    Win——Ctrl+Z              Unix——Ctrl+D 
            並且,應該是輸入行的第一個字符就是文件結束符,然後回車才能結束循環。

原文 :http://jerrychoi.iteye.com/blog/455684

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章