String insert()总结
文件頭格式
#include "stdafx.h"
#include <string>
#include <iostream>
int main( )
{
?? using namespace std;
}
-----------------------------------------------------------------
basic_string<CharType, Traits, Allocator>& insert(
?? size_type _P0,?
?? const value_type* _Ptr
);
?? // 在 string 字符前(位置可以指定) 插入 char 類型字符串
?? basic_string <char> str1a ( "way" );
?? const char *cstr1a = "a";
?? str1a.insert ( 0, cstr1a ); // 0 可調(diào)整位置
?? cout << "The string with a C-string inserted at position 0 is: "
??????? << str1a << "." << endl;
?? // 結(jié)果 str1a = away.
-----------------------------------------------------------------
basic_string<CharType, Traits, Allocator>& insert(
?? size_type _P0,?
?? const value_type* _Ptr,
?? size_type _Count
);
?? // 在 string 字符后插入指定長度的 char 類型字符
?? // 其中參數(shù)4代表原數(shù)據(jù)的長度
???
?? basic_string <char> str2a ( "Good" );
?? const char *cstr2a = "Bye Bye Baby";
?? str2a.insert ( 4, cstr2a ,3 ); // Good為4 , Bye為3
?? cout << "The string with a C-string inserted at the end is: "
??????? << str2a << "." << endl;
?? // 結(jié)果 str2a = GoodBye.
-----------------------------------------------------------------
basic_string<CharType, Traits, Allocator>& insert(
?? size_type _P0,
?? const basic_string<CharType, Traits, Allocator>& _Str
);
?? // 在 string 字符前(位置可以指定) 插入指定長度的 string 類型字符
???
?? basic_string <char> str3a ( "Bye" );
?? string str3b ( "Good" );
?? str3a.insert ( 0, str3b ); // 0 可調(diào)整位置
?? cout << "The string with a string inserted at position 0 is: "
??????? << str3a << "." << endl;
????
?? // 結(jié)果 str2a = GoodBye.
-----------------------------------------------------------------
basic_string<CharType, Traits, Allocator>& insert(
?? size_type _P0,
?? const basic_string<CharType, Traits, Allocator>& _Str,?
?? size_type _Off,?
?? size_type _Count
);
?? // 在 string 字符后插入指定長度的 char 類型字符 , 中間參數(shù)為過該字符串長度?
?? // 起始點 并以該參數(shù)后的 第一個字符作為起始點 。插入第三個參數(shù)指定參數(shù)長度。
?? // 其中參數(shù) 5 代表原數(shù)據(jù)的長度
???
???
?? basic_string <char> str4a ( "Good " );
?? string str4b ( "Bye Bye Baby" );
?? str4a.insert ( 5, str4b , 8 , 4 ); // Bye Bye 為8 , Baby為4
?? cout << "The string with part of a string inserted at position 4 is: "
??????? << str4a << "." << endl;
??? // 結(jié)果 str2a = Good Baby.
-----------------------------------------------------------------
basic_string<CharType, Traits, Allocator>& insert(
?? size_type _P0,???? // 在原字符串后插入新字符串 po 代表參數(shù) 如:15
?? size_type _Count, // 在原字符串后插入新字符個數(shù),且內(nèi)容 為下面 _Ch 參數(shù)。
?? value_type _Ch???? // 在原字符串后面插內(nèi)容參數(shù) 且 value_type 為 char
);
?? // 在指定位置 后插入指定個數(shù)的字符串。
???
?? string str5 ( "The number is: ." );
?? str5.insert ( 15 , 3 , '3' );
?? cout << "The string with characters inserted is: "
??????? << str5 << endl;
??? // 結(jié)果 The number is: 333.
-----------------------------------------------------------------
iterator insert(
?? iterator _It
);
iterator insert(
?? iterator _It,?
????? value_type _Ch
)l
void insert(
?? iterator _It,???? // 指定原字符串內(nèi)容 和 位置. 可參看 ( str6.begin ( ) + 4 ) 。
?? size_type _Count, // 指定新插入內(nèi)容的個數(shù)
?? value_type _Ch??? // 新插入內(nèi)容 參數(shù)。
);
???
?? // 在指定位置中插入 新字符 有個數(shù)參數(shù)
???
?? string str6 ( "ABCDFG" );
?? basic_string <char>::iterator str6_Iter = ( str6.begin ( ) + 4 ); // 在 string 字符串中第4位 插入 Ch 內(nèi)容。且后面內(nèi)容照舊。
?? str6.insert ( str6_Iter , 'e' );
?? cout << "The string with a character inserted is: "
??????? << str6 << endl;
??? // 結(jié)果 ABCDeFG
-----------------------------------------------------------------????
template<class InputIterator> // template模板
?? void insert(
????? iterator _It, // 在迭代器后面字符插入內(nèi)容。
????? InputIterator _First, // 輸入迭代器, const_pointer ,或const_iterator處理中的第一個元素的來源范圍內(nèi)插入。
????? InputIterator _Last??? // 輸入迭代器, const_pointer ,或const_iterator解決的立場,一個超越過去因素的來源范圍內(nèi)插入。
?? );
=================================================
void insert(
????? iterator _It,??????? // 指定原字符串內(nèi)容 和 位置. 可參看 (str7a.begin ( ) + 4 )
???? const_pointer _First, // 從要插入字符串的第一個元素往后數(shù),到指定位置。 可參看 str7b.begin ( ) + 4
???? const_pointer _Last?? // 從要插入字符串的最后一個元素往前數(shù),到指定位置。 可參看 str7b.end ( ) -1
);
?? // 在指定位置中插入新字符串。且指定范圍。
???
?? string str7a ( "ABCDHIJ" );
?? string str7b ( "abcdefgh" );
?? basic_string <char>::iterator str7a_Iter = (str7a.begin ( ) + 4 );
?? str7a.insert ( str7a_Iter , str7b.begin ( ) + 4 , str7b.end ( ) -1 );
?? cout << "The string with a character inserted from a range is: "
??????? << str7a << endl;
????????
?? // 結(jié)果 ABCDefgHIJ
-----------------------------------------------------------------
void insert(
????? iterator _It,???????? // 指定原字符串內(nèi)容 和 第一個元素往后數(shù),到指定位置。. 可參看 (str7a.begin ( ) + 4 )
???? const_iterator _First, // 指定新插入內(nèi)容的個數(shù)
???? const_iterator _Last?? // 新插入內(nèi)容 參數(shù)。
);
?? // 在在指定位置中插入新字符。有個數(shù)參數(shù)
???
?? string str8 ( "ABCDHIJ" );
?? basic_string <char>::iterator str8_Iter = ( str8.begin ( ) + 4 );
?? str8.insert ( str8_Iter , 3 , 'e' );
?? cout << "The string with a character inserted from a range is: "
??????? << str8 << endl;
?? // 結(jié)果 ABCDeeeHIJ
=======================================================================
參數(shù)解釋補充
_P0
該指數(shù)的作用是在最后點插入新的字符。
_Ptr
這架C -字串將全部或部分插入到字符串。
_Count
的字符數(shù)待補
_Str
該字符串將全部或部分插入到目標(biāo)字符串。
_Off
該指數(shù)對部分供應(yīng)源字符串的字符被附加。
_Ch
價值的性質(zhì)的內(nèi)容插入。
_It
一個迭代器處理的立場后面的字符是插入。
_First
輸入迭代器, const_pointer ,或const_iterator處理中的第一個元素的來源范圍內(nèi)插入。
_Last
輸入迭代器, const_pointer ,或const_iterator解決的立場,一個超越過去因素的來源范圍內(nèi)插入。
總結(jié)
以上是生活随笔為你收集整理的String insert()总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: string 类型详解
- 下一篇: CCTYPE函数系列