複製鏈接
請複製以下鏈接發送給好友

insert函數

鎖定
insert函數是c語言c++)中字符串處理的一個函數,可以把一個字符串(或是某個部分)插入另一個字符串。
中文名
insert函數
所屬學科
計算機科學

目錄

insert函數官方文檔

string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
Inserts a copy of a substring of str at character position pos1. The substring is the portion of str that begins at the character position pos2 and takes up to n characters (it takes less than n if the end of str is reached before).

insert函數應用

例子(c++):
// inserting into a string
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}