為何C++如此難?同一個函數的五六個版本,C++string insert函數

前言

為何C++如此難?同一個函數的五六個版本,C++string insert函數

string 類的成員函數有很多,同一個名字的函數也常會有五六個重載的版本。篇幅所限,不能將這些原型一一列出並加以解釋。這裡僅對insert函數做以介紹,並直接給出應用的例子,通過例子,讀者可以基本掌握insert函數的用法。要想更深入地瞭解 string 類,還要閱讀 C++ 的參考手冊。

在index位置插入count個字符ch

<code>basic_string& insert( size_type index, size_type count, CharT ch );/<code>

測試案例:

<code>string str = "Loveyou";
string sstr = str.insert(0,2,'a');
cout

index位置插入一個常量字符串

<code>basic_string& insert( size_type index, const CharT* s );/<code>

測試案例:

<code>string str = "Loveyou";
string sstr = str.insert(1,"hello~");
cout

index位置插入常量字符串中的count個字符

<code>basic_string& insert( size_type index, const CharT* s, size_type count );/<code>

測試案例:

<code>string str = "Loveyou";
string sstr = str.insert(1,"hello~",3);
cout 

index位置插入常量string

<code>basic_string& insert( size_type index, const basic_string& str );/<code>

測試案例:

<code>string str = "Loveyou";
string sstr = str.insert(1,str);
cout

迭代器指向的pos位置插入count個字符ch

<code>void insert( iterator pos, size_type count, CharT ch );/<code>

測試案例:

<code>string str1 = "Loveyou";
str1.insert(++str1.begin(),2,'a');
cout

尾言

文章都是手打原創,每天最淺顯的介紹C語言、C++,windows知識,喜歡我的文章就關注一波吧,可以看到最新更新和之前的文章哦。

《C語言51課視頻教程合集》

《C++45課視頻教程》

如果足下基礎比較差,不妨關注下人人都可以學習的視頻教程,通俗易懂,深入淺出,一個視頻只講一個知識點。視頻不深奧,不需要鑽研,在公交、在地鐵、在廁所都可以觀看,隨時隨地漲姿勢


分享到:


相關文章: