String insert() 插入方法

本文详细解析了C++中basic_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 可调整位置
   cout << "The string with a C-string inserted at position 0 is: "
        << str1a << "." << endl;

   // 结果 str1a = away.
-----------------------------------------------------------------

basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0, 
   const value_type* _Ptr,
   size_type _Count
);

   // 在 string 字符后插入指定长度的 char 类型字符
   // 其中参数4代表原数据的长度
   
   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;

   // 结果 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 可调整位置
   cout << "The string with a string inserted at position 0 is: "
        << str3a << "." << endl;
    
   // 结果 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 类型字符 , 中间参数为过该字符串长度 
   // 起始点 并以该参数后的 第一个字符作为起始点 。插入第三个参数指定参数长度。
   // 其中参数 5 代表原数据的长度
   
   
   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;

    // 结果 str2a = Good Baby.
-----------------------------------------------------------------

basic_string<CharType, Traits, Allocator>& insert(
   size_type _P0,     // 在原字符串后插入新字符串 po 代表参数 如:15
   size_type _Count, // 在原字符串后插入新字符个数,且内容 为下面 _Ch 参数。
   value_type _Ch     // 在原字符串后面插内容参数 且 value_type 为 char
);

   // 在指定位置 后插入指定个数的字符串。
   
   string str5 ( "The number is: ." );
   str5.insert ( 15 , 3 , '3' );
   cout << "The string with characters inserted is: "
        << str5 << endl;

    // 结果 The number is: 333.
-----------------------------------------------------------------

iterator insert(
   iterator _It
);

iterator insert(
   iterator _It, 
      value_type _Ch
)l

void insert(
   iterator _It,     // 指定原字符串内容 和 位置. 可参看 ( str6.begin ( ) + 4 ) 。
   size_type _Count, // 指定新插入内容的个数
   value_type _Ch    // 新插入内容 参数。
);
   
   // 在指定位置中插入 新字符 有个数参数
   
   string str6 ( "ABCDFG" );
   basic_string <char>::iterator str6_Iter = ( str6.begin ( ) + 4 ); // 在 string 字符串中第4位 插入 Ch 内容。且后面内容照旧。
   str6.insert ( str6_Iter , 'e' );
   cout << "The string with a character inserted is: "
        << str6 << endl;

    // 结果 ABCDeFG
-----------------------------------------------------------------    


template<class InputIterator> // template模板
   void insert(
      iterator _It, // 在迭代器后面字符插入内容。
      InputIterator _First, // 输入迭代器, const_pointer ,或const_iterator处理中的第一个元素的来源范围内插入。
      InputIterator _Last    // 输入迭代器, const_pointer ,或const_iterator解决的立场,一个超越过去因素的来源范围内插入。
   );

=================================================


void insert(
      iterator _It,        // 指定原字符串内容 和 位置. 可参看 (str7a.begin ( ) + 4 )
     const_pointer _First, // 从要插入字符串的第一个元素往后数,到指定位置。 可参看 str7b.begin ( ) + 4
     const_pointer _Last   // 从要插入字符串的最后一个元素往前数,到指定位置。 可参看 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;
        
   // 结果 ABCDefgHIJ
-----------------------------------------------------------------


void insert(
      iterator _It,         // 指定原字符串内容 和 第一个元素往后数,到指定位置。. 可参看 (str7a.begin ( ) + 4 )
     const_iterator _First, // 指定新插入内容的个数
     const_iterator _Last   // 新插入内容 参数。
);

   // 在在指定位置中插入新字符。有个数参数
   
   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;

   // 结果 ABCDeeeHIJ
=======================================================================

参数解释补充

_P0
该指数的作用是在最后点插入新的字符。

_Ptr
这架C -字串将全部或部分插入到字符串。

_Count
的字符数待补

_Str
该字符串将全部或部分插入到目标字符串。

_Off
该指数对部分供应源字符串的字符被附加。

_Ch
价值的性质的内容插入。

_It
一个迭代器处理的立场后面的字符是插入。

_First
输入迭代器, const_pointer ,或const_iterator处理中的第一个元素的来源范围内插入。

_Last
输入迭代器, const_pointer ,或const_iterator解决的立场,一个超越过去因素的来源范围内插入。

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### C++ 中 `std::string` 类的字符插入操作 在 C++ 的标准库中,`std::string` 提供了多种方式来进行字符串的操作,其中包括插入字符或子串到指定位置的功能。 #### 使用 insert 方法 `insert` 是 `std::string` 类提供的成员函数之一,允许向现有字符串中的特定位置插入另一个字符串或单个字符。此方法非常灵活并支持多种形式的调用: - 插入一个单独的字符: ```cpp #include <iostream> #include <string> int main() { std::string s = "world"; s.insert(0, 1, 'h'); // 在索引0处插入'h' std::cout << s << std::endl; } ``` 这段代码会在原有字符串 `"world"` 开头的位置插入字母 `'h'`,最终输出结果为 `"hworld"`[^1]。 - 插入一段字符串: ```cpp #include <iostream> #include <string> int main() { std::string s = "Hello "; s.insert(s.size(), "World!"); // 在当前字符串结尾追加"World!" std::cout << s << std::endl; } ``` 上述例子展示了如何利用 `s.size()` 获取当前位置作为起始点,在原字符串后面附加新的内容,得到完整的问候语句 `"Hello World!"`[^2]。 除了这些基本形式外,还可以通过传递迭代器范围或者直接给出要复制的具体字节数等方式来执行更复杂的插入动作。值得注意的是,所有的插入操作都会返回修改后的字符串本身的一个引用,这使得链式调用成为可能[^3]。 对于想要连续进行多次插入的情况,可以充分利用这一点简化语法结构而不必重复变量名。例如: ```cpp #include <iostream> #include <string> int main() { std::string greeting = "Hi".insert(2, ", ").insert(4, "there"); std::cout << greeting << std::endl; // 输出 Hi, there } ``` 在这个案例里,先是在索引2之后加入了逗号和空格组成的短语`, `,紧接着在同一表达式的下一部分继续于新形成的第四个位置(即原来的第三个位置加上新增部分)加入单词`there`,从而构建出了友好的打招呼信息 `"Hi, there"`[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值