简体   繁体   English

如何从 c++ 中的模板 class 调用 function

[英]How to call a function from template class in c++

this template function is new to me as I'm learning.这个模板 function 对我来说是新的,因为我正在学习。 I need to pass the compare from BinarySearch() to Comparer() to show how many elements the comparison performs.我需要将比较从 BinarySearch() 传递给 Comparer() 以显示比较执行了多少元素。 How can I call the Comparer() inside the BinarySearch()?如何在 BinarySearch() 中调用 Comparer()? This is to find if a string is in the array.这是为了查找字符串是否在数组中。

#ifndef INTCOMPARER_H
#define INTCOMPARER_H

#include "Comparer.h"

class IntComparer : public Comparer<int> {
public:
    int Compare(const int& a, const int& b) override {
        if (a < b) {
            return -1;
        }
        else if (a > b) {
            return 1;
        }
        return 0;
    }
};
#endif
#ifndef COMPARER_H
#define COMPARER_H

template <typename T>
class Comparer {
public:
    virtual int Compare(const T& a, const T& b) = 0;
};

#endif
#ifndef SEARCHER_H
#define SEARCHER_H

#include "Comparer.h"

template <typename T>
class Searcher {
public:
    // I need to call the Comparer() inside this BinarySearch().
    static int BinarySearch(T* array, int arraySize, const T& key,
        Comparer<T>& comparer) 
    {
        int low = 0;
        int high = arraySize - 1;
        int mid = 0;
        int test;
      
        while (high >= low) {
            mid = (high + low) / 2;
            if (array[mid] < key) {
                low = mid + 1;               
            }
            else if (array[mid] > key) {
                high = mid - 1;                
            }
            else
            {
                return mid;
            }
            return array.template Comparer<int>(1, 2);
        }
        return -1;
    }
};
#endif

BinarySearch() takes in a reference to an object that implements Comparer<T> . BinarySearch()接受对实现Comparer<T>的 object 的引用。 It can call the Compare() method on that object when needed, eg:它可以在需要时调用该 object 上的Compare()方法,例如:

#ifndef SEARCHER_H
#define SEARCHER_H

#include "Comparer.h"

template <typename T>
class Searcher {
public:
    static int BinarySearch(T* array, int arraySize, const T& key,
        Comparer<T>& comparer) 
    {
        int low = 0;
        int high = arraySize - 1;
        int mid = 0;
        int test;
      
        while (high >= low) {
            mid = (high + low) / 2;
            test = comparer.Compare(array[mid], key); // <-- here
            if (test < 0) {
                low = mid + 1;               
            }
            else if (test > 0) {
                high = mid - 1;                
            }
            else {
                return mid;
            }
        }
        return -1;
    }
};
#include "searcher.h"
#include "intcomparer.h"

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int key = ...;
    IntComparer comp;

    cout << "found at " << Searcher<int>::BinarySearch(arr, 5, key, comp);
}

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

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