English 中文(简体)
在法典体内确定校长
原标题:Defining functor within the body of a code
  • 时间:2012-01-13 00:20:17
  •  标签:
  • c++

能否在一个功能体内在当地界定校长?

我认为,对当地定义的校长(特别是如果我们想使用STL算法)的一种可能使用就是说。

我们有两个矢量<代码>std:vector<int> s/code>和b,然后我们可以以多种方式予以同等考虑。 i.e. a[i] = b[i] (mod loop_counter),在这种地方,假冒者保持变化,我们测试在每处休息时实现平等。

for(int i=0 ; i<=10 ; ++i)
{
//Define binary predicate functor my_local_predicate

//Test for equality using functor local to loop
std::equal(a.begin(), a.end(), b.begin, my_local_predicate)

 // Do something if they are equal OR unequal

}

如果答案没有,那么在平等条件与每一种变化之间如何改变的情况下,谁会这样做?

<>光> 我试图将ctor子界定为(此处为>>,但该方案未能汇编成册。

#include <algorithm>
#include <iostream>
#include <list>



int main() {

     class EvenOddFunctor 
  {
    int even_;
    int odd_;
    public:
    EvenOddFunctor() : even_(0), odd_(0) {}
    void operator()(int x) {
        if (x%2 == 0) even_ += x;
        else odd_ += x;
    }
    int even_sum() const { return even_; }
    int odd_sum() const { return odd_; }
  };

    EvenOddFunctor evenodd;

    int my_list[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    evenodd = std::for_each(my_list,
                  my_list+sizeof(my_list)/sizeof(my_list[0]),
                  evenodd);

    std::cout << "Sum of evens: " << evenodd.even_sum() << "
";
    std::cout << "Sum of odds: " << evenodd.odd_sum() << std::endl;

    // output:
    // Sum of evens: 30
    // Sum of odds: 25
}

关于将博士定义移至<代码>main()”的问题,该守则进行了精确汇编,并正确地执行。

因此,即使在一个职能机构内确定一位胜利者似乎是不可能的,我也希望一些小ice像改变每一个代人的平等条件。

最佳回答

在你提问的第一部分,我看不出如何需要当地确定的职能。 你们所需要的只是选择不同职能的一种方式,这非常简单。 例如:

std::function<bool(int,int)> my_functions[10];
my_functions[0] = std::equal_to<int>();
my_functions[1] = both_divisible_by_5;
...etc

for (int i=0; i<10; ++i)
{
    if (std::equal(a.begin(), a.end(), b.begin, my_functions[i]))
    {
        ...
    }
}
问题回答

如果你能够使用C++11,这只会奏效。 在C++03中,你不能将当地班级作为模板参数。 您可使用<代码>std:accumulate,代之以当地班级固定功能(以及非当地结构储存结果):

struct EvenOdd
{
    int even;
    int odd;
    static EvenOdd empty() { EvenOdd result= { }; return result; }
};

int main()
{
    struct EvenOddFunctor
    {
        static EvenOdd function(const EvenOdd &old_result, int x)
        {
            EvenOdd result= old_result;
            if (x%2 == 0) result.even += x;         
            else result.odd += x;
            return result;
        }
    };

    EvenOdd evenOdd= std::accumulate(..., EvenOdd::empty(), EvenOddFunctor::function);
}

(假设你在C++03中 st)

您的校长可以继承最高范围界定的模范基级,然后只提及基级(例如,使用m_fun/bind1st)。

template <typename T>
struct val_functor {
    virtual ~val_functor() {}
    virtual void operator()(T x) = 0;
};

int main()
{
    class EvenOddFunctor : public val_functor<int>
    ...
    std::for_each(my_list, my_list+sizeof(my_list)/sizeof(my_list[0]),
        std::bind1st(
             std::mem_fun(&val_functor<int>::operator()),
            (val_functor<int>*)&evenodd)
    );
    ...
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签