编程技巧:
auto可以自定义类型,适用于不知道数据类型的数据定义。
巧妙使用s.find(str);s.rfind(str);s.find_fisrt_of(str);s.find_last_of;s.find_first_not_of(str);s.find_last_not_of(str)等函数,其中后四个都是查找的str包含的任何一个字符。
#include <iostream>
#include <string>
using namespace std;
//末尾可能存在空格版本
int main(){
string str;
getline(cin,str);
auto pos=str.find_last_not_of(" "); //找到最后一个字母位置
str=str.substr(0,pos+1); //截断到最后一个字母
pos=str.rfind(" ");
str=str.substr(pos+1);
cout << str.size() << endl;
}