string 容器函数

本文介绍了C++中的`insert`函数,包括在指定位置插入字符串和插入指定数量字符的方法,以及`substr`函数的用法,特别提到substr在赋值时会清空目标容器的内容。
摘要由CSDN通过智能技术生成

1.insert

1)string &insert(int p0, const char *s)
功能:在原字符串下标为pos的字符插入字符串str
返回值:插入字符串后的结果

(2)string &insert(int p0, const char *s, int n);
功能:在p0位置插入字符串s的n个字符
返回值:插入字符串后的结果

 

 

2.substr函数

一种构造string的方法
形式 : s.substr(pos, len)
返回值: string,包含s中从pos开始的len个字符的拷贝。pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s。

xingsi

  • substr函数用于赋值时,会先清空另一个容器的元素
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string a = "bad";
	string s="kill";
	cout << s << endl;
	s = a.substr(0, 2);
	cout << s << endl;
	return 0;
}

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值