使用委托进行排序 [英] Make sort using delegates

查看:63
本文介绍了使用委托进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在C ++ / CLI中创建排序委托,但是,当我尝试编译时,我发现此错误:

I'm try to make a sort delegate in C++/CLI, but, when I try to compile I recive this erro:

>app.cpp(256): error C3374: can't take address of 'Program::AnonymousMethod1' unless creating delegate instance
>app.cpp(256): error C2664: 'void System::Collections::Generic::List<T>::Sort(System::Comparison<T> ^)' : cannot convert parameter 1 from 'System::Object ^(__clrcall *)(Program::teste1,Program::teste1)' to 'System::Comparison<T> ^'
>          with
>          [
>              T=Program::teste1 ^
>          ]
>          No user-defined-conversion operator available, or
>          There is no context in which this conversion is possible
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

以下是错误的代码示例:

here is the code sample of the error:

using namespace System;
using namespace System::Collections::Generic;

private ref class Program
{
private:
    enum class tokens
    {
        teste,
        lala,
        blabla,
    };

    ref struct teste1
    {
        int linha;
        tokens tk;
    };

private:
    static Object ^AnonymousMethod1(teste1 p1, teste1 p2)
    {
        return p1.tk.CompareTo(p2.tk);
    }

public:
    Program()
    {
        bool jump = false;
        List<teste1^>^ lstTest = gcnew List<teste1^>();
        Random ^rnd = gcnew Random();

        for (int i = 0; i < 20; i++)
        {
            teste1 ^tst = gcnew teste1();
            switch (rnd->Next(1,4))
            {
            case 1:
                tst->tk = tokens::teste;
                break;
            case 2:
                tst->tk = tokens::lala;
                break;
            case 3:
                tst->tk = tokens::blabla;
                break;
            }
            lstTest->Add(tst);
        }

        for each (teste1^ content in lstTest)
        {
            Console::WriteLine(content->tk.ToString());
        }

        lstTest->Sort(AnonymousMethod1);

        Console::WriteLine("==============================================================");

        for each (teste1^ content in lstTest)
        {
            Console::WriteLine(content->tk.ToString());
        }
    }
};

int main(array<String^> ^args)
{
    Program^ prg = gcnew Program();
    return 0;
}

我只是想先对带有令牌 lala的列表进行排序,如何我可以做到这一点,以及如何解决这个问题???

I just whant to sort the list with the token 'lala' first, how I can make this, and, How I can solve this ???

推荐答案

您的列表的类型为 List< test1 ^> (请注意 ^ 帽子)。因此,您想要的 Comparison< T> 委托人是 Comparison< teste1 ^> 。因此,您需要按以下方式更改AnonymousMethod1:

Your List is of type List<test1^> (notice the ^ hat). So the Comparison<T> delegate you want is Comparison<teste1^>. Thus you'll want to change your AnonymousMethod1 as follows:

static int AnonymousMethod1(teste1 ^ p1, teste1 ^ p2)
{
    return p1->tk.CompareTo(p2->tk);
}

在C ++ / CLI中,您需要显式创建委托:

And in C++/CLI you need to explicitly create your delegate:

    Comparison<teste1 ^> ^ comparisonDelegate = gcnew Comparison<teste1 ^>(&AnonymousMethod1);
    lstTest->Sort(comparisonDelegate);

这篇关于使用委托进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆