插入() 用于在字符串中的指定位置插入字符。它支持各种语法来促进相同的功能,这里我们将对它们进行描述。
null
语法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. Returns *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字符串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. Returns *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: 插入字符数组字符的字符,以便新字符以索引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. 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 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 Returns : *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指定的位置插入字符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. Returns : *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. Returns : 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!
本文由 萨希·提瓦里 .如果你喜欢极客(我们知道你喜欢!)如果你想投稿,你也可以用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END