【C++】string类的使用③(修改器Modifiers || 非成员函数重载Non-member function overloads)
🔥个人主页: Forcible Bug Maker
🔥专栏: STL || C++
目录
- 前言
- 🔥修改器(Modifiers)
- ==**operator+=**==
- ==append==
- ==push_back和pop_back==
- ==assign==
- ==insert==
- ==erase==
- ==replace==
- ==swap==
- 🔥非成员函数重载(Non-member function overloads)
- ==operator+==
- ==relation operators(string)==
- ==swap(string)==
- ==流插入和流提取重载==
- ==getline==
- 结语
前言
本篇博客主要内容:STL库中string的修改器(Modifiers)和非成员函数重载(Non-member function overloads)。
来到string类的使用第三篇,继续我们的内容,本篇博客将介绍如何使用STL库中string的成员函数修改串,以及重载给string的几个非成员函数。
🔥修改器(Modifiers)
顾名思义,就是一批能改动string串中内容的成员函数。
operator+=
这是一个成员函数的运算符重载。
简单说就是在串的末尾追加字符或字符串。
(1) string string对象
string& operator+= (const string& str);
(2) c-string 字符串指针
string& operator+= (const char* s);
(3) character 字符
string& operator+= (char c);
以上三个重载的功能用一句话概括:在当前string串的末尾追加字符或字符串。
共同的返回值:当前对象的引用(string&)。
使用样例:
// string::operator+= #include #include using namespace std; int main() { string name("John"); string family("Smith"); name += " K. "; // c-string name += family; // string name += '\n'; // character cout string str; string str2 = "Writing "; string str3 = "print 10 and then 5 more"; // used in the same order as described above: str.append(str2); // "Writing " str.append(str3, 6, 3); // "10 " str.append("dots are cool", 5); // "dots " str.append("here: "); // "here: " str.append(10, '.'); // ".........." str.append(str3.begin() + 8, str3.end()); // " and then 5 more" cout string str("hello world"); str.push_back('!'); cout string str; string base = "The quick brown fox jumps over a lazy dog."; // used in the same order as described above: str.assign(base); cout string str = "to be question"; string str2 = "the "; string str3 = "or not to be"; string::iterator it; // used in the same order as described above: str.insert(6, str2); // to be (the )question str.insert(6, str3, 3, 4); // to be (not )the question str.insert(10, "that is cool", 8); // to be not (that is )the question str.insert(10, "to be "); // to be not (to be )that is the question str.insert(15, 1, ':'); // to be not to be(:) that is the question it = str.insert(str.begin() + 5, ','); // to be(,) not to be: that is the question str.insert(str.end(), 3, '.'); // to be, not to be: that is the question(...) str.insert(it + 2, str3.begin(), str3.begin() + 3); // (or ) cout std::string str("This is an example sentence."); cout string base = "this is a test string."; string str2 = "n example"; string str3 = "sample phrase"; string str4 = "useful."; // replace signatures used in the same order as described above: // Using positions: 0123456789*123456789*12345 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) cout string str1("hello"); string str2("world"); cout string firstlevel("com"); string secondlevel("cplusplus"); string scheme("http://"); string hostname; string url; hostname = "www." + secondlevel + '.' + firstlevel; url = scheme + hostname; cout string foo = "alpha"; string bar = "beta"; if (foo == bar) cout string str1("hello"); string str2("world"); cout string str; cin str; cout string name; cout
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理!
部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!
图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!