当前位置:文档之家› C++string类型总结

C++string类型总结

对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, si ze_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_t ype 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) cons t;注意:若at函数的参数pos无效,则抛出异常out_of_range;但如果oper ator[]的参数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, s ize_type n);3: string& replace(size_type p0, size_type n0, const string& st r);4: string& replace(size_type p0, size_type n0, const string& st r, size_type pos, size_type n);5: string& replace(size_type p0, size_type n0, size_type n, cha r 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 strin g& str);9: string& replace(iterator first0, iterator last0, size_type n, char c);10: string& replace(iterator first0, iterator last0, const_itera tor 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);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)之前插入字符串。

相关主题