C ++中的自适应排序功能

时间:2018-07-15 09:21:17

标签: c++ algorithm templates

我想为自适应气泡排序具有不同类型的不同集合提供功能。代码示例:

// GET TABLE QUERY
table = md.retrieveTableName();
// ITERATE EACH TABLE
for (int a = 0; a < n_tables; a++) {                
    colName = md.retrieveColumnName(table.get(a));
    ps = conn.prepareStatement("select * from " + database + "." + table.get(a));
    // ITERATE EACH COLUMN IN TABLE A
    for (int b = 0; b < colName.size(); b++) {
        colType = md.retrieveColumnType(table.get(a), colName.get(b));
        rlSet = ps.executeQuery();
        // ACCESS NEXT DATA
        while (rlSet.next()) {
            Element1.add(rlSet.getString(colName.get(b)));
            // ITERATE ALL EXISTING TABLES
            for (int d = 0; d < n_tables; d++) {
                if (a != d) {
                    ps2 = conn2.prepareStatement("select * from " + database + "." + table.get(d));
                    colName2 = md.retrieveColumnName(table.get(d));
                    // ITERATE EACH COLUMN FROM TABLE
                    for (int c = 0; c < colName2.size(); c++) {
                        rlSet2 = ps2.executeQuery();

                        while (rlSet2.next()) {
                            Element2.add(rlSet2.getString(colName2.get(c)));

                            if (Element1.get(Element1.size()-1).equals(Element2.get(Element2.size() - 1))) {
                                System.out.println("Same value");
                                        // DO SOMETHING HERE

                                    } ......

但是我遇到以下问题:

#include <iostream>
#include <vector>
#include <functional>

template <typename CollectionType, typename ValueType>
void bubbleSort(CollectionType& ar, size_t size, std::function<bool(ValueType, ValueType)> comparator)
{
    bool isSorting = false;
    for (size_t i = 0; i < size - 1; i++)
    {
        isSorting = true;
        for (size_t j = 0; j < size; j++)
        {
            if (comparator(ar[j], ar[j + 1]))
            {
                std::swap(ar[j], ar[j + 1]);
                isSorting = false;
            }
        }
        if (isSorting)
        {
            return;
        }
    }
}

int main()
{
    std::vector<int> vector = {7, 9, 1, 5, 8, 1, 8, 3, 7, 3};
    bubbleSort(vector, vector.size(), [](int a, int b) { return a > b; });

    for(const auto& val : vector)
    {
        std::cout << val << " ";
    }

    return EXIT_SUCCESS;
}

如果我设置功能模板的参数:

/home/vova/Desktop/Temp/main.cpp:37:73: error: no matching function for call to ‘bubbleSort(std::vector<int>&, std::vector<int>::size_type, main()::<lambda(int, int)>)’ bubbleSort(vector, vector.size(), [](int a, int b) { return a > b; });

/home/vova/Desktop/Temp/main.cpp:6:6: note: candidate: template<class CollectionType, class ValueType> void bubbleSort(CollectionType&, size_t, std::function<bool(ValueType, ValueType)>) void bubbleSort(CollectionType& ar, size_t size, std::function<bool(ValueType, ValueType)> comparator)

/home/vova/Desktop/Temp/main.cpp:6:6: note:   template argument deduction/substitution failed:

/home/vova/Desktop/Temp/main.cpp:37:73: note:   ‘main()::<lambda(int, int)>’ is not derived from ‘std::function<bool(ValueType, ValueType)>’ bubbleSort(vector, vector.size(), [](int a, int b) { return a > b; });

然后它可以工作,但对我来说看起来并不漂亮。如何纠正?

1 个答案:

答案 0 :(得分:3)

您的lambda不是std :: function,并且无法从lambda的参数推导出std :: function的模板参数。

但是,您无需将std :: function用作比较器,则可以使用模板“ Comparator”参数。这是标准库对有序容器中的比较器所做的工作。 例如

template <typename CollectionType, typename Comparitor>
void bubbleSort(CollectionType& ar, size_t size, Comparitor comparator)
{
    bool isSorting = false;
    for (size_t i = 0; i < size - 1; i++)
    {
        isSorting = true;
        for (size_t j = 0; j < size; j++)
        {
            if (comparator(ar[j], ar[j + 1]))
            {
                std::swap(ar[j], ar[j + 1]);
                isSorting = false;
            }
        }
        if (isSorting)
        {
            return;
        }
    }
}