C++string中的insert()插入函數(shù)詳解
下面通過代碼給大家介紹c++ string insert() 函數(shù),具體內(nèi)容如下:
basic_string& insert (size_type pos, const basic_string& str);
在原串下標(biāo)為pos的字符前插入字符串str
basic_string& insert (size_type pos, const basic_string& str, size_type pos1, size_type n);
str從下標(biāo)為pos1開始數(shù)的n個字符插在原串下標(biāo)為pos的字符前
basic_string& insert (size_type pos, size_type n, char c);
在原串下標(biāo)為pos的字符前插入n個字符c
代碼:
#include<iostream>
using namespace std;
int main()
{
string str="hello";
string s="Hahah";
str.insert(1,s);//在原串下標(biāo)為1的字符e前插入字符串s
cout<<str<<endl;
string str1="hello";
char c='w';
str1.insert(4,5,c);//在原串下標(biāo)為4的字符o前插入5個字符c
cout<<str1<<endl;
string str2="hello";
string s2="weakhaha";
str2.insert(0,s2,1,3);//將字符串s2從下標(biāo)為1的e開始數(shù)3個字符,分別是eak,插入原串的下標(biāo)為0的字符h前
cout<<str2<<endl;
return 0;
}
運(yùn)行結(jié)果:

知識點補(bǔ)充:C++ string類insert函數(shù)
string的成員函數(shù)insert有以下多種重載:
string &insert(int p0, const char *s);——在p0位置插入字符串s
string &insert(int p0, const char *s, int n);——在p0位置插入字符串s的前n個字符
string &insert(int p0,const string &s);——在p0位置插入字符串s
string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字符串s從pos開始的連續(xù)n個字符
string &insert(int p0, int n, char c);//在p0處插入n個字符c
iterator insert(iterator it, char c);//在it處插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iteratorlast);//在it處插入從first開始至last-1的所有字符
void insert(iterator it, int n, char c);//在it處插入n個字符c
總結(jié)
到此這篇關(guān)于C++string中的insert()插入函數(shù)的文章就介紹到這了,更多相關(guān)c++ string insert()插入函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 的 format 和 vformat 函數(shù)示例詳解
傳統(tǒng)C庫的printf系列函數(shù)存在安全問題,而C++推薦的基于流格式化輸入輸出雖然解決了安全性問題,但在易用性方面仍顯不足,C++11引入了新的C風(fēng)格字符串格式化函數(shù),但類型安全問題依舊存在,下面通過本文介紹C++ 的 format 和 vformat 函數(shù)示例,感興趣的朋友一起看看吧2025-02-02
C語言中邏輯運(yùn)算符與條件運(yùn)算符的學(xué)習(xí)教程
這篇文章主要介紹了C語言中邏輯運(yùn)算符與條件運(yùn)算符的學(xué)習(xí)教程,條件運(yùn)算符問號即三目運(yùn)算符使用起來十分方便,需要的朋友可以參考下2016-04-04
C++ 實現(xiàn)一個復(fù)數(shù)類的實例代碼
這篇文章主要介紹了C++ 實現(xiàn)一個復(fù)數(shù)類的實例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

