8

次の場合:

struct Foo
{
    int bar() const;
};

struct IsEqual : public std::unary_function<Foo*, bool>
{
    int val;
    IsEqual(int v) : val(v) {}

    bool operator()(const Foo* elem) const
    {
        return elem->bar() == val;
    }
};

私はのコンテナを持っており、Foo*を使用std::find_ifして、指定された値とは異なるものを返すstd::not1要素がコンテナ内にあるかどうかを調べます。bar()コードは次のようになります。

// Are all elements equal to '2'?
bool isAllEqual(const std::vector<Foo*> &vec)
{
    return find_if(vec.begin(), vec.end(), std::not1(IsEqual(2))) == vec.end();
}

将来に向けて早送りすると、今度はを含む別のコンテナができましたstd::tr1::shared_ptr<Foo>。のオーバーロードされたバージョンでファンクターを再利用したいだけですisAllEqual()。しかし、私はできません。 Foo*shared_ptr<Foo>は異なるタイプです。unary_functionそして、を使用できるように、から継承する必要がありますnot1。同じファンクターを2回書くのを避けられれば、もっとエレガントになるでしょう。

質問:

  • IsEqual生のポインターとスマートポインターの両方を使用できるように書く方法はありますか?
  • 使って手錠をかけましたstd::not1か?IsNotEqual代わりに書くべきですか?

制限:

  1. Boostライブラリの何も使用できません。
  2. 私たちのコンパイラは、C++0xラムダをサポートするのに十分なほどクールではありません。
4

5 に答える 5

8

どうですか:

template<typename T>
struct IsEqual : public std::unary_function<const T&, bool>
{
    int val;
    IsEqual(int v) : val(v) {}

    bool operator()(const T& elem) const
    {
        return elem->bar() == val;
    }
};

template<typename T>
IsEqual<T> DeduceEqualityComparer(int v, T) { return IsEqual<T>(v); }

// Are all elements equal to '2'?
template<typename TContainer>
bool isAllEqual(const TContainer& coll)
{
    using std::begin; // in C++0x, or else write this really simple function yourself
    using std::end;
    if (begin(coll) == end(coll)) return true;
    return find_if(begin(coll), end(coll), std::not1(DeduceEqualityComparer(2, *begin(coll)))) == end(coll);
}
于 2010-12-13T20:32:29.827 に答える
2

私のショットは次のようになります。

template<typename PtrToFoo>
struct IsEqual : public std::unary_function<PtrToFoo, bool>
{
    int val;
    IsEqual(int v) : val(v) {}

    bool operator()(PtrToFoo elem) const
    {
        return elem->bar() == val;
    }
};

で参照解除可能なすべてのものに対して異なるoperator()インスタンス化が行われる->ため、生のポインターとスマートポインターがあります。

于 2010-12-13T20:29:16.177 に答える
2
// --*-- C++ --*--

#include <vector>
#include <algorithm>
#include <iostream>

// Template unary function example.
template <typename T>
struct IsEqual : public std::unary_function<T, bool>
{
    int v;

    IsEqual (int v) : v (v) {}

    bool operator () (const T & elem) const
    {
        return elem ? elem->bar () == v : false;
    }
};

// Generic algorithm implementation example...
template <typename T1, typename T2>
bool isAllEqual (const T1 & c, T2 v)
{
    return find_if (
        c.begin (), c.end (),
        std::not1 (IsEqual <typename T1::value_type> (v))) == c.end ();
}

// Some arbitrary pointer wrapper implementation,
// provided just for an example, not to include any
// specific smart pointer implementation.
template <typename T>
class WrappedPtr
{
    const T *v;

public:
    typedef void (WrappedPtr<T>::*unspecified_boolean_type) () const;

    WrappedPtr (const T *v) : v (v) {}

    const T *operator -> () const { return v; }

    operator unspecified_boolean_type () const
    {
        return v != NULL ?
            &WrappedPtr<T>::unspecified_boolean_true : NULL;
    }

private:
    void unspecified_boolean_true () const {}
};

// Example of structure that could be used with our algorithm.
struct Foo
{
    int v;

    Foo (int v) : v (v) {}

    int bar () const
    {
        return v;
    }
};

// Usage examples...
int main ()
{
    Foo f1 (2), f2 (2);

    // Example of using raw pointers...
    {
        std::vector<Foo *> vec;
        vec.push_back (NULL);
        vec.push_back (&f1);
        vec.push_back (&f2);

        if (isAllEqual (vec, 2))
            std::cout << "All equal to 2" << std::endl;
        else
            std::cout << "Not all equal to 2" << std::endl;
    }

    // Example of using smart pointers...
    {
        std::vector< WrappedPtr<Foo> > vec;
        vec.push_back (NULL);
        vec.push_back (&f1);
        vec.push_back (&f2);

        if (isAllEqual (vec, 2))
            std::cout << "All equal to 2" << std::endl;
        else
            std::cout << "Not all equal to 2" << std::endl;
    }
}
于 2010-12-13T20:49:07.400 に答える
1

暗黙の変換で何かトリッキーなことをすることができます:

class IsEqualArg {
public:
  // Implicit conversion constructors!
  IsEqualArg(Foo* foo) : ptr(foo) {}
  IsEqualArg(const std::tr1::shared_ptr<Foo>& foo) : ptr(&*foo) {}
private:
  Foo* ptr;
  friend struct IsEqual;
};

struct IsEqualArg : public std::unary_function<IsEqualArg, bool> {
  bool operator()( const IsEqualArg& arg ) const;
  //...
};

しかし、私は実際にはむしろ単に書くことを望みますIsNotEqual

于 2010-12-13T20:37:14.080 に答える
0

ベンの答えは、c++03でできる唯一のことです。ただし、C ++ 0xでは、および/またはboost :: bindを使用すると、unary_functionから継承する必要はありません。これにより、templated()演算子を使用できます。通常、C ++ 03でも同じことができますが、技術的には正しくないと思います。

于 2010-12-13T20:40:23.907 に答える