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

#include
#include
#include
#include
#include
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

你可能感兴趣的:((c++) str.erase() & str.find_first(last)_not_of()用法)