首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用stable_partiton出错

使用stable_partiton出错
EN

Stack Overflow用户
提问于 2015-07-11 17:24:11
回答 2查看 30关注 0票数 0

我试图解决topcoder问题,除非字符串只包含元音,否则必须删除字符串中的所有元音。我正在尝试使用stable_partition和函数isVowel对字符串进行分区。`

代码语言:javascript
复制
// {{{ 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;
    }
};

但是在编译时,我得到了以下错误消息:

代码语言:javascript
复制
(3 of 458): error: cannot convert ‘VowelEncryptor::isVowel’ from type ‘bool (VowelEncryptor::)(char)’ to type ‘bool (VowelEncryptor::*)(char)’
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-11 17:28:35

试着使isVowel保持静态。

如果没有要调用的实际对象实例,则不能使用指向成员函数的指针,而如果没有帮助,stable_partition就无法使用该实例。要么让函数static,要么使用std::bind

票数 2
EN

Stack Overflow用户

发布于 2015-07-11 17:29:03

isVowel函数在任何方面都不依赖于VowelEncryptor类对象的任何状态。它应该是一个自由函数,或者一个静态成员函数。顺便说一句,这应该可以修复您的错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31359832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档