64,683
社区成员
发帖
与我相关
我的任务
分享
#include <string>
#include <iostream>
using namespace std;
template<typename T>
void rtrim(basic_string<T>& s,T c)
{
if(s.empty())
return;
typename basic_string<T>::iterator p;
for(p=s.end();p!=s.begin()&&*--p==c;);
if(*p!=c)
p++;
s.erase(p,s.end());
}
int main()
{
string s="Great!!!!!";
wstring ws=L"super!!!!";
rtrim(s,'!');
rtrim(ws,L'!');//单引号就可以了
cout<<s<<'\n';
wcout<<ws<<L'\n';
return 0;
}
#include <string>
#include <iostream>
using namespace std;
template<typename T>
void rtrim(basic_string<T>& s, T c)
{
if (s.empty())
return;
typename basic_string<T>::iterator p;
for (p = s.end(); p != s.begin() && *--p == c;);
if (*p != c)
p++;
s.erase(p, s.end());
}
int main()
{
string s = "Great!!!!!";
wstring ws = L"super!!!!";
rtrim<char>(s, '!');
rtrim<wchar_t>(ws, L'!');
cout << s << '\n';
wcout << ws << L'\n';
}