3

このクラスに問題があります。
目標は、メイン関数を適切に機能させることです。コードが機能するように、「And」関数オブジェクトを実装することになっていました。私たちのソリューションの何が問題なのかわかりません。
(ソリューションの開始と終了は、「メイン」関数の前のコード内のコメントでマークされています)
助けていただけますか?
ありがとう

#include <iostream>
#include <algorithm>

using namespace std;

class NotNull
{
    public:
    bool operator()(const char* str) {return str != NULL;}
};

class BeginsWith
{
    char c;
    public:
    BeginsWith(char c) : c(c) {}
    bool operator()(const char* str) {return str[0] == c;}
};

class DividesBy {
    int mod;
    public:
    DividesBy(int mod) : mod(mod) {}
    bool operator()(int n) {return n%mod == 0;}
};

//***** This is where my sulotion starts ******

template <typename Function1, typename Function2, typename T>
class AndFunction
{
    Function1 f1;
    Function2 f2;
    public:
    AndFunction(Function1 g1, Function2 g2) : f1(g1), f2(g2) {}
    bool operator()(T t)
    {
        return (f1(t) && f2(t));
    }
};

template <typename Function1, typename Function2, typename T>
AndFunction <Function1, Function2, T>

bool And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2, T>(f1, f2);
}

//***** This is where my sulotion ends ******

int main(int argc, char** argv)
{
    int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    char* strings[4] = {"aba", NULL, "air", "boom"};
    cout << count_if(array,array+10,And(DividesBy(2),DividesBy(4))) << endl;
    // prints 2, since 4 and 8 are the only numbers which can be divided by
    // both 2 and 4.
    cout << count_if(strings,strings+4,And(NotNull(),BeginsWith('a'))) <<endl;
    // prints 2, since only "aba" and "air" are both not NULL and begin
    // with the character 'a'.
    return 0;
}
4

4 に答える 4

3

T明らかに、ファンクターを作成するときのパラメーターはわかりません。T実際の呼び出し (operator()メンバー テンプレートの作成)の導入を遅らせることを検討しましたか?

于 2010-07-22T11:26:17.583 に答える
2

()ここでオブジェクトを作成するときに 、オーバーロードされた演算子を呼び出さないでください: (の前にreturn AndFunction<Function1, Function2, T>(f1, f2);a が必要です) このコードは、実際には bool ではなくオブジェクトを返すため、コンパイルすることさえできません。();


EDIT:指摘したように、関数(bool And(Function1 f1, Function2 f2) )は返してはいけませんが、オーバーロードされた演算子を介して呼び出すboolための関数オブジェクトですcount_if()

于 2010-07-22T11:26:29.933 に答える
1

技術的に言えば、 STL アルゴリズムに適切に配置したい場合は、unary_functionおよびクラスを親として使用する必要があります。binary_functionここ:

template<typename Func1, typename Func2,typename T>
struct AndFunction : public unary_function<T,bool>{
    AndFunction(Func1 _func1, Func2 _func2) 
        : _myFunc1(_func1),
        _myFunc2(_func2){}

    bool operator()(T _t){
        return _myFunc1(_t) && _myFunc2(_2);
    }

private:
    Func1 _myFunc1;
    Func2 _myFunc2;
};

あなたの場合、あなたがする必要があります

template<typename Func1, typename Func2, typename T> 
AndFunction<Func1, Func2, T> And(Func1 _func1, Func2 _func2){
    return AndFunction<Func1,Func2,T>(_func1,_func2);
};

オペレーターをオブジェクト作成と混同しないようにし、関数命令を受け取る方法を指定するようにします。

反対に、あなたのmain働き方はあなたが本当に望んでいると思います

struct And : public binary_function<bool, bool, bool>{
    bool operator()(bool _1, bool _2){
        return _1 && _2;
    }
};

それが役立つことを願っています。

于 2010-07-22T12:02:03.630 に答える
0

テンプレート パラメーターTは推測できません。明示的に指定する必要があります。

template <typename T, typename Function1, typename Function2>
AndFunction <Function1, Function2, T>
And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2, T>(f1, f2);
}

//***** This is where my sulotion ends ******

int main(int argc, char** argv)
{
    int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    char* strings[4] = {"aba", NULL, "air", "boom"};
    cout << count_if(array,array+10,And<int>(DividesBy(2),DividesBy(4))) << endl;
    // prints 2, since 4 and 8 are the only numbers which can be divided by
    // both 2 and 4.
    cout << count_if(strings,strings+4,And<const char*>(NotNull(),BeginsWith('a'))) <<endl;
    // prints 2, since only "aba" and "air" are both not NULL and begin
    // with the character 'a'.
    return 0;
}

jpalecek のソリューションの方が優れており、次のように機能します。

//***** This is where my sulotion starts ******

template <typename Function1, typename Function2>
class AndFunction
{
    Function1 f1;
    Function2 f2;
    public:
    AndFunction(Function1 g1, Function2 g2) : f1(g1), f2(g2) {}
  template<typename T> bool operator()(T)
    {
        return (f1(t) && f2(t));
    }
};

template <typename Function1, typename Function2>
AndFunction <Function1, Function2>
And(Function1 f1, Function2 f2)
{
    return AndFunction<Function1, Function2>(f1, f2);
}

//***** This is where my sulotion ends ******
于 2010-07-22T11:33:20.753 に答える