string类insert函数
发布日期:2021-04-30 21:01:43
浏览次数:100
分类:精选文章
本文共 944 字,大约阅读时间需要 3 分钟。
C++ string类提供了多种插入操作的方法,用于在字符串中插入不同类型的字符或子字符串。这些方法能够帮助开发者灵活地构建和修改字符串内容。以下是常见的插入方法及其功能说明:
string& insert(int p0, const char* s);
在p0位置插入字符串s。例如,字符串str = "hello",通过str.insert(2, "wor")可以得到"helworlo"。string& insert(int p0, const char* s, int n);
在p0位置插入字符串s的前n个字符。同样,使用str.insert(2, "wor", 3)可以插入"wor"的前3个字符,结果为"helworlo"。string& insert(int p0, const string& s);
在p0位置插入字符串s。如str.insert(5, s)会将s中的所有字符插入到位置5,结果字符串长度增加s.size()。string& insert(int p0, const string& s, int pos, int n);
在p0位置插入字符串s从pos开始的n个字符。例如,若s = "abcdef",pos=2,n=3,则插入"bcd",结果字符串变为"hbcdefg"。string& insert(int p0, int n, char c);
在p0位置插入n个字符c。如str.insert(3, 5, 'a')将插入5个'a',结果字符串长度增加5。iterator insert(iterator it, char c);
在it处插入字符c,返回插入后迭代器的位置。例如,str.begin()插入后,新字符的位置即为插入点。void insert(iterator it, const_iterator first, const_iterator last);
在it处插入从first到last-1的所有字符。该方法适用于插入子字符串的所有字符。void insert(iterator it, int n, char c);
在it处插入n个字符c。这种方式适用于批量插入相同字符的情况,效率较高。发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2026年05月27日 03时22分50秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PHP 浮点型精度运算相关问题
2023-02-28
php 浮点型计算精度问题
2023-02-28
php 特定时间段统计,jpgraph某个时间段的数据统计
2023-02-28
php 生成csv mac下乱码
2023-02-28
php 生成证书 签名及验签
2023-02-28
PHP 的标准输入与输出
2023-02-28
php 笔记 (早前的,很乱)
2023-02-28
PHP 第一天
2023-02-28
Redis使用量暴增,快速定位有哪些大key在作怪
2023-02-28
PHP 统计数据功能 有感
2023-02-28
SpringBoot处理JSON数据
2023-02-28
PHP 输入输出流合集
2023-02-28
php--防止sql注入的方法
2023-02-28
php-兔子问题,斐波那契数列
2023-02-28
php-有序数组合并后仍有序
2023-02-28
php-约瑟夫问题
2023-02-28
php.ini中常见的配置信息选项
2023-02-28
php.ini配置中有10处设置不当,会使网站存在安全问题
2023-02-28
PHP7 新特性
2023-02-28