cplusplus 模板类学习笔记
来源:互联网 发布:打狗棒 解密软件 编辑:程序博客网 时间:2024/06/15 08:41
string模板的成员函数:
const char * string::data() 和 const char * string::c_str() 返回字符串数组的const类型指针,此字符串数组代表string对象。此为string转 const char* 方法。
string::erase的三种实现:
(1)string& erase (size_t pos = 0, size_t len = npos);
(2)iterator erase (const_iterator p);
(3)iterator erase (const_iterator first, const_iterator last);
string::find的四种实现:从头(左)查找,rfind则是从尾(右)查找。
string (1) size_t find (const string& str, size_t pos = 0) const noexcept;
查找string类型的子串模式。如果找到则返回第一个匹配的位置,否则返回string::npos
c-string (2) size_t find (const char* s, size_t pos = 0) const;
查找char* 字符串,返回内容如上。
buffer (3) size_t find (const char* s, size_t pos, size_type n) const;
查找 字符数组(即不以’\0’结尾的数组)。需要指定第三个参数-查找个数n。返回值如上所述
character (4) size_t find (char c, size_t pos = 0) const noexcept;
查找一个字符的实现函数。
比较两个char* 型字符串:
reference->cstring 或 string.h -> memcmp(const void *ptr1,const void *ptr2, size_t num);相等为0,*ptr1>*ptr2 返回1 否则返回-1.
string::replace 的7种实现
string (1)
string& replace (size_t pos, size_t len, const string& str);
string& replace (const_iterator i1, const_iterator i2, const string& str);
substring (2)
string& replace (size_t pos, size_t len,const string& str, size_t subpos, size_t sublen);
pos为开始替换的位置,len为替换的字符个数;而subpos,sublen是使用[subpos,subpos+sublen)代替前面所说范围[pos,pos+len)
c-string (3)
string& replace (size_t pos, size_t len, const char* s);
string& replace (const_iterator i1, const_iterator i2, const char* s);
buffer (4)
string& replace (size_t pos, size_t len, const char* s, size_t n);
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
fill (5)
string& replace (size_t pos, size_t len, size_t n, char c);
string& replace (const_iterator i1, const_iterator i2, size_t n, char c);
range (6)
template
string& replace (const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);
initializer list (7)
string& replace (const_iterator i1, const_iterator i2, initializer_list il);
example:int main (){ std::string base="this is a test string."; std::string str2="n example"; std::string str3="sample phrase"; std::string str4="useful."; // replace signatures used in the same order as described above: // Using positions: 0123456789*123456789*12345 std::string str=base; // "this is a test string." str.replace(9,5,str2); // "this is an example string." (1) str.replace(19,6,str3,7,6); // "this is an example phrase." (2) str.replace(8,10,"just a"); // "this is just a phrase." (3) str.replace(8,6,"a shorty",7); // "this is a short phrase." (4) str.replace(22,1,3,'!'); // "this is a short phrase!!!" (5) // Using iterators: 0123456789*123456789* str.replace(str.begin(),str.end()-3,str3); // "sample phrase!!!" (1) str.replace(str.begin(),str.begin()+6,"replace"); // "replace phrase!!!" (3) str.replace(str.begin()+8,str.begin()+14,"is coolness",7); // "replace is cool!!!" (4) str.replace(str.begin()+12,str.end()-4,4,'o'); // "replace is cooool!!!" (5) str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful." (6) std::cout << str << '\n'; return 0;}
std::operator+ (string) 对字符串的加号的重载函数
string (1)
string operator+ (const string& lhs, const string& rhs);
string operator+ (string&& lhs, string&& rhs);
string operator+ (string&& lhs, const string& rhs);
string operator+ (const string& lhs, string&& rhs);
c-string (2)
string operator+ (const string& lhs, const char* rhs);
string operator+ (string&& lhs, const char* rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (const char* lhs, string&& rhs);
character (3)
string operator+ (const string& lhs, char rhs);
string operator+ (string&& lhs, char rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (char lhs, string&& rhs);
注:两个char * 字符串相加,使用strcat(a,b)函数。
std::ralational operators(string),两项字符串比较函数的重载。
(1)
bool operator== (const string& lhs, const string& rhs);
bool operator== (const char* lhs, const string& rhs);
bool operator== (const string& lhs, const char* rhs);
(2)
bool operator!= (const string& lhs, const string& rhs);
bool operator!= (const char* lhs, const string& rhs);
bool operator!= (const string& lhs, const char* rhs);
(3)
bool operator< (const string& lhs, const string& rhs);
bool operator< (const char* lhs, const string& rhs);
bool operator< (const string& lhs, const char* rhs);
(4)
bool operator<= (const string& lhs, const string& rhs);
bool operator<= (const char* lhs, const string& rhs);
bool operator<= (const string& lhs, const char* rhs);
(5)
bool operator> (const string& lhs, const string& rhs);
bool operator> (const char* lhs, const string& rhs);
bool operator> (const string& lhs, const char* rhs);
(6)
bool operator>= (const string& lhs, const string& rhs);
bool operator>= (const char* lhs, const string& rhs);
bool operator>= (const string& lhs, const char* rhs);
- cplusplus 模板类学习笔记
- JAVA - 泛型 vs CPLUSPLUS - 模板
- cplusplus
- cplusplus
- NDK帮助文档学习--CPLUSPLUS-SUPPORT.html
- C++模板类学习笔记
- C++学习笔记--类模板
- C++学习笔记(一)函数模板与类模板
- C++学习笔记_9:类模板
- CArray 动态数组模板类学习笔记
- c++学习笔记(15.类模板)
- C++学习笔记24-2 类模板
- C++ STL(标准模板类) 学习笔记
- Xdoclet学习笔记(模板)
- xsl学习笔记-模板
- 模板学习笔记(一)
- 模板学习笔记
- mako模板学习笔记
- 基于PHP的cURL快速入门
- 【Git笔记】如何在同主机同账户下实现多个gitlab帐号管理各自的remote repo
- 自定义Toast,从顶部掉下,然后再弹一下
- pat 2-05. 求集合数据的均方差(水题)
- 这是我在公司自己写的一个小项目模仿的一个互帮派APP后面那些我也没有时间完善希望希望的朋友帮我完善后面那些
- cplusplus 模板类学习笔记
- php学习-表单验证4
- genymotion模拟器问题
- Delphi 的运算符列表
- Android 版本号等等获取
- 狗 日 的 360
- C++出错提示英汉对照表
- Algorithms—80.Remove Duplicates from Sorted Array II
- easyui数据窗口datagrid的数据加载问题