當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::string::insert()用法及代碼示例


insert()用於在指定位置的字符串中插入字符。它支持各種語法以促進相同,在這裏我們將對其進行描述。

語法1:從索引idx插入str的字符。

string& string::insert (size_type idx, const string& str)
idx:is the index number
str:is the string from which characters is to be picked to insert 
Returns *this.
Errors:
Throws out_of_range if idx > size().
Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for insert (size_type idx, const string& str) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str1, string str2) 
{ 
   
    // Inserts str2 in str1 starting  
    // from 6th index of str1 
    str1.insert(6, str2); 
    cout << "Using insert:"; 
    cout << str1; 
} 
  
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks "); 
   
    cout << "Original String:" << str1 << endl; 
    insertDemo(str1, str2); 
   
    return 0; 
}

輸出:


Original String:Hello World! 
Using insert:Hello GeeksforGeeks World! 

語法2:最多插入str的str_num個字符,從索引str_idx開始。

string& string::insert (size_type idx, const string& str, size_type str_idx,
                                                           size_type str_num)
idx:is the index number where insertion is to be made.
str:is the string from which characters are to be picked to insert.
str_idx:is the index number in str.
str_num:is the number of characters to be inserted from str.
返回 *this.
Errors:
Throws out_of_range if idx > size().
Throws out_of_range if str_idx > str.size().
Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for insert (size_type idx, const string& str,  
// size_type str_idx, size_type str_num) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str1, string str2) 
{ 
   
    // Inserts 6 characters from index number 
    // 8 of str2 at index number 6 of str1  
    str1.insert(6, str2, 8, 6); 
    cout << "Using insert:"; 
    cout << str1; 
} 
  
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks "); 
   
    cout << "Original String:" << str1 << endl; 
    insertDemo(str1, str2); 
   
    return 0; 
}

輸出:

Original String:Hello World! 
Using insert:Hello Geeks World! 

語法3:插入C-string cstr的字符,以便新字符以索引idx開頭。

string& string::insert (size_ type idx, const char* cstr)
idx:is the index number where insertion is to be made.
*cstr:is the pointer to the C-string which is to be inserted.
返回 *this.
Errors:
Throws out_of_range if idx > size().
Throws length_error if the resulting size exceeds the maximum number of characters.

注意:cstr可能不是空指針(NULL)。

// CPP code for insert(size_ type idx, const char* cstr) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str) 
{ 
   
    // Inserts " are " at 5th index of str 
    str.insert(5, " are "); 
    cout << "Using insert:"; 
    cout << str; 
} 
  
// Driver code 
int main() 
{ 
    string str("GeeksforGeeks "); 
   
    cout << "Original String:" << str << endl; 
    insertDemo(str); 
   
    return 0; 
}

輸出:

Original String:GeeksforGeeks 
Using insert:Geeks are forGeeks 

語法4:插入字符數組chars的chars_len字符,以便新字符以索引idx開頭。

string& string::insert (size_type idx, const char* chars, size_type chars_len)
idx:index number where insertion is to be made.
*chars:is the pointer to the array.
chars_len:is the number of characters to be inserted from character array.
返回:*this
Errors:
Throws out_of_range if idx > size().
Throws length_error if the resulting size exceeds the maximum number of characters.

注意:char必須至少包含chars_len字符。

// CPP code for insert (size_type idx, const char* chars,  
// size_type chars_len) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str) 
{ 
   
    // Inserts 10 characters from" are here " 
    // at 5th index of str 
    str.insert(5, " are here ", 10); 
    cout << "Using insert:"; 
    cout << str; 
} 
  
// Driver code 
int main() 
{ 
    string str("GeeksforGeeks "); 
   
    cout << "Original String:" << str << endl; 
    insertDemo(str); 
   
    return 0; 
}

輸出:

Original String:GeeksforGeeks 
Using insert:Geeks are here forGeeks 

語法5:在idx指定的位置插入字符c的出現次數。

string& string::insert (size_type idx, size_type num, char c)
idx:is the index number where insertion is to be made.
c:is the character to be inserted.
num:is the number of repetition of character c
返回:*this
Errors:
Throw out_of_range if idx > size().
Throw length_error if the resulting size exceeds the maximum number of characters.
// CPP code for:insert (size_type idx, size_type num, char c) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str) 
{ 
   
    // Inserts at 5th index, 
    // 5 occurrences of '$' 
    str.insert(5, 5, '$'); 
    cout << "Using insert:"; 
    cout << str; 
} 
  
// Driver code 
int main() 
{ 
    string str("**********"); 
   
    cout << "Original String:" << str << endl; 
    insertDemo(str); 
   
    return 0; 
}

輸出:


Original String:**********
Using insert:*****$$$$$*****

語法6:在迭代器pos指定的位置插入num個出現的字符c。

void string::insert (iterator pos, size_type num, char c)
pos:is the position of iterator.
c:is the character which is to be inserted.
返回:*this
Errors:
Throws out_of_range if pos > size().
Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for:insert (iterator pos, size_type num, char c) 
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str) 
{ 
   
    // Inserts 5 occurrences of '$' 
    // at position str.begin() + 5 
    str.insert(str.begin() + 5, 5, '$'); 
    cout << "Using insert:"; 
    cout << str; 
} 
  
// Driver code 
int main() 
{ 
    string str("**********"); 
   
    cout << "Original String:" << str << endl; 
    insertDemo(str); 
   
    return 0; 
}

輸出:

Original String:**********
Using insert:*****$$$$$*****

語法7:在迭代器pos所引用的字符之前插入字符c的副本。

iterator string::insert (iterator pos, char c )
pos:is the position of iterator.
c:is the character which is to be inserted.
返回: iterator pointing to the first character inserted.
Error:
Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for:insert (iterator pos, char c ) 
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str) 
{ 
     std::string::iterator pos; 
  
    // Inserts '$' at position 
    //  str.begin() + 5 
    pos = str.insert(str.begin()+5,'$'); 
    cout << "Using insert:"; 
    cout << str << endl; 
    cout << "Value at Iterator returned:" << *pos; 
      
} 
  
// Driver code 
int main() 
{ 
    string str("**********"); 
   
    cout << "Original String:" << str << endl; 
    insertDemo(str); 
   
    return 0; 
}

輸出:

Original String:**********
Using insert:*****$*****
Value at Iterator returned:$

語法8:在迭代器pos所引用的字符之前插入[beg,end)範圍內的所有字符。

void string::insert (iterator pos, InputIterator beg, InputIterator end )
pos:is the iterator position.
beg, end: Input iterators to the initial and final positions in a sequence.
Error:
Throws length_error if the resulting size exceeds the maximum number of characters.
// CPP code for insertinsert (iterator pos, InputIterator beg, 
// InputIterator end ) 
   
#include <iostream> 
#include <string> 
  
using namespace std; 
   
// Function to demonstrate insert 
void insertDemo(string str1, string str2) 
{ 
   
    // Inserts str2.begin() + 5 ,  str2.end() - 6 
    // at position str1.begin() + 6 
    str1.insert(str1.begin() + 6, str2.begin() + 5 ,  str2.end() - 6); 
    cout << "Using insert:"; 
    cout << str1; 
} 
  
// Driver code 
int main() 
{ 
    string str1("Hello  World! "); 
    string str2("GeeksforGeeks "); 
   
    cout << "Original String:" << str1 << endl; 
    insertDemo(str1, str2); 
   
    return 0; 
}

輸出:

Original String:Hello World! 
Using insert:Hello forWorld! 


相關用法


注:本文由純淨天空篩選整理自 std::string::insert() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。