對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物件的操作
字串比較
支援六種關係運算子(==、!=、>、>=、<、<=),其採用字典排序策略(與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;否則,返回正數。
字串相加
針對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);
字串賦值
字串賦值有兩種方式:一是利用成員過載運算子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);
字串追加
字串追加同樣有兩種方式:一是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);
讀取子串
獲取某個下標處的字元:一是用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返回實際拷貝的字元數。
替換子串
成員函式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();
插入字串
成員函式insert實現在某點處插入字串。簡化函式原型如下:
1: string& insert(size_type p0, const char *s);
2: string& insert(size_type p0, const char *s, size_type n);
總結型別題
一 填空題 1 六 1 班有29名男同學,21名女同學,女同學佔全班人數的 2 有兩根一樣長的鐵絲,一根圍成長方形,一根圍成圓,的面積大。3 甲數是10,乙數是8,甲數比乙數多 乙數比甲數少 4 乙個數的20 是30,這個數是 20噸的25 是 噸。5 9位同學參加比賽,每兩位同學都要比賽一場,一共...
程式設計題型別總結
求和類 1.計算s 1!2!3!4!n s 0k 1 input n to n for i 1 to n k k i s s k endfor s2.求 sum 0 for i 1 to 21 step 2 sum sum i 3 endfor sum sum 3.有一分數序列,求前20項之和 m ...
程式設計題型別總結
求和類 1.計算s 1!2!3!4!n s 0k 1 input n to n for i 1 to n k k i s s k endfor s2.求 sum 0 for i 1 to 21 step 2 sum sum i 3 endfor sum sum 3.有一分數序列,求前20項之和 m ...