📜  C++中的std :: 字符串:: insert()

📅  最后修改于: 2021-05-30 14:51:59             🧑  作者: Mango

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 
#include 
  
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 
#include 
  
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 
#include 
  
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.
Returns : *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 
#include 
  
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指定的位置插入num个出现的字符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 
#include 
  
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.
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 
#include 
  
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 
#include 
  
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:将范围为[beg,end)的所有字符插入迭代器pos所引用的字符之前。

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 
#include 
  
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! 
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”