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

知識點補充: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)文章
最新VScode C/C++ 環(huán)境配置的詳細教程
這篇文章主要介紹了最新VScode C/C++ 環(huán)境配置的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
深入C++拷貝構(gòu)造函數(shù)的總結(jié)詳解
本篇文章是對C++中拷貝構(gòu)造函數(shù)進行了總結(jié)與介紹。需要的朋友參考下2013-05-05

