当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。