使用 stable_partiton 时出错
Error using stable_partiton
我正在尝试解决一个 topcoder 问题,我必须从字符串中删除所有元音,除非该字符串仅包含元音。我正在尝试使用 stable_partition 和函数 isVowel 对字符串进行分区。
`
// {{{ VimCoder 0.3.6 <-----------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
// }}}
class VowelEncryptor
{
public:
bool isVowel(char letter)
{
return (letter == 'a' || letter == 'e' || letter == 'o' || letter == 'o' || letter == 'u');
}
vector <string> encrypt(vector <string> text)
{
int nElements = text.size();
for (int i = 0; i < nElements; i++)
{
string::iterator bound = stable_partition(text[i].begin(), text[i].end(), isVowel);
if (bound != text.end())
{
text[i].erase(text[i].begin(), bound);
}
}
return text;
}
};
但是在编译时我收到了这个错误信息:
(3 of 458): error: cannot convert ‘VowelEncryptor::isVowel’ from type ‘bool (VowelEncryptor::)(char)’ to type ‘bool (VowelEncryptor::*)(char)’
尝试使 isVowel
静态化。
如果没有要调用的实际对象实例,则不能使用指向成员函数的指针,这 stable_partition
没有帮助也做不到。创建函数 static
或使用 std::bind
.
您的 isVowel
函数不以任何方式依赖于 VowelEncryptor
class 对象的任何状态。它应该是一个自由函数,或者静态成员函数。顺便说一句,这应该可以解决您的错误。
我正在尝试解决一个 topcoder 问题,我必须从字符串中删除所有元音,除非该字符串仅包含元音。我正在尝试使用 stable_partition 和函数 isVowel 对字符串进行分区。 `
// {{{ VimCoder 0.3.6 <-----------------------------------------------------
// vim:filetype=cpp:foldmethod=marker:foldmarker={{{,}}}
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
// }}}
class VowelEncryptor
{
public:
bool isVowel(char letter)
{
return (letter == 'a' || letter == 'e' || letter == 'o' || letter == 'o' || letter == 'u');
}
vector <string> encrypt(vector <string> text)
{
int nElements = text.size();
for (int i = 0; i < nElements; i++)
{
string::iterator bound = stable_partition(text[i].begin(), text[i].end(), isVowel);
if (bound != text.end())
{
text[i].erase(text[i].begin(), bound);
}
}
return text;
}
};
但是在编译时我收到了这个错误信息:
(3 of 458): error: cannot convert ‘VowelEncryptor::isVowel’ from type ‘bool (VowelEncryptor::)(char)’ to type ‘bool (VowelEncryptor::*)(char)’
尝试使 isVowel
静态化。
如果没有要调用的实际对象实例,则不能使用指向成员函数的指针,这 stable_partition
没有帮助也做不到。创建函数 static
或使用 std::bind
.
您的 isVowel
函数不以任何方式依赖于 VowelEncryptor
class 对象的任何状态。它应该是一个自由函数,或者静态成员函数。顺便说一句,这应该可以解决您的错误。