简体   繁体   English

使用 lambdas 对 c++ 中的指针数组进行排序

[英]sorting array of pointers in c++ using lambdas

I am trying to write sorting template function to make it work with custom classes.我正在尝试编写排序模板 function 以使其与自定义类一起使用。

My code is:我的代码是:

#include <iostream>
#include <vector>

struct test
{
    int value;
    test(int a) : value(a){};
    void print() { printf("the value is : %d", value); };
};

template <class T>
void bubblesort(T *m_data, size_t size, bool (*cmp)(const T &l, const T &r))
{
    for (uint32_t i = 0; i < size; i++)

        for (uint32_t j = 1; j < size - i; j++)

            if (cmp(m_data[j - 1], m_data[j]))
            {
                T temp = m_data[j];
                m_data[j] = m_data[j - 1];
                m_data[j - 1] = temp;
            }
}

int main()
{
    std::vector<test> arr;
    for (size_t i = 0; i < 10; i++)
        arr.emplace_back(i);

    std::vector<test *> arr1;
    for (auto &i : arr)
        arr1.emplace_back(&i);

    bubblesort<test>(&arr[0], arr.size(), [](const test &l, const test &r) { return l.value < r.value; });

    // bubblesort<test*>(&arr1[0], arr1.size(), [](const test *&l, const test *&r) { return l->value < r->value; });

    for (auto i : arr)
        printf("%d\n", i.value);
}

My question is how do you sort arr1 using the bubblesort function above?我的问题是如何使用上面的bubblesort function 对 arr1 进行sort What kind of modification do I have to make in my code to be able to do so?我必须在我的代码中进行什么样的修改才能这样做?

uncommenting the bubblesort line gives error取消注释 bubblesort 行会出错

 error: invalid user-defined conversion from 'main()::<lambda(const test*&, const test*&)>' to 'bool (*)(test* const&, test* const&)' [-fpermissive]
[build]    48 |     bubblesort<test *>(&arr1[0], arr1.size(), [](const test *&l, const test *&r) { return l->value < r->value; });

Your function has the wrong type;你的function类型错误; T is test* , so you need test* const& - "reference to const pointer to test " - as the error message says. Ttest* ,所以你需要test* const& - “reference to const pointer to test ” - 正如错误消息所说。
( const test*& is "reference to pointer to const test .) const test*&是“对指向 const test的指针的引用。)

Your solution cannot work with templates...it cannot deduce the parameters type.您的解决方案无法使用模板...它无法推断出参数类型。

Consider modifying your code as follow:考虑修改您的代码如下:

struct test
{
    int value;
    explicit test(int a) : value(a) {};
    void print() { printf("the value is : %d", value); };
};

template <class T, class _cmp>
void bubblesort(T *m_data, size_t size, _cmp cmp)
{
    for (uint32_t i = 0; i < size; i++)

        for (uint32_t j = 1; j < size - i; j++)

            if (cmp(m_data[j - 1], m_data[j]))
            {
                T temp = m_data[j];
                m_data[j] = m_data[j - 1];
                m_data[j - 1] = temp;
            }
}


int main()
{
    
    std::vector<test> arr;
    for (int i = 0; i < 10; i++)
        arr.emplace_back(i);

    
    std::vector<test *> arr1;
    for (auto i : arr)
        arr1.emplace_back(&i);

    bubblesort<test>(&arr[0], arr.size(), [](const test &l, const test &r) -> bool { return l.value < r.value; });
    bubblesort<test*>(&arr1[0], arr1.size(), [](const test* l, const test* r) -> bool { return l->value < r->value; });

    for (auto i : arr)
        printf("%d\n", i.value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM