(c++) str.erase() & str.find_first(last)_not_of()用法

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
const int maxn = 5e4 + 100;
#define TLE ios::sync_with_stdio(0),cin.tie(0)
const int INF = 0x3f3f3f3f;
#define ll long long
string Delete_Space(string str) {
	cout << str.find_first_not_of(' ') << endl;;
	str.erase(0, str.find_first_not_of(' '));
	cout << str.find_last_not_of(' ') + 1 << endl;
	str.erase(str.find_last_not_of(' ') + 1);
	return str;
}
int main() {
	TLE;
	string str;
	getline(cin, str);
	cout << Delete_Space(str) << endl;
}

样例:
在这里插入图片描述

  1. .find_first_not_of(str,num)函数:

    从字符串第num+1个字符开始查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从num 开始。如果没找到就返回string::nops

  2. str.erase(iter1,iter2)函数:

    删除[iter1,iter2)包含的字符

  3. getline(cin,str)函数所在头文件:

    包装了std 的C++头文件,是新的string 类,其分两种对应的是char和wchar_t,其对应的命名空间分别是using std::string;using std::wstring

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值