在下面的代码中,我试图搜索传递给特定字符串的模板函数的字符串数组,但我收到错误“没有匹配函数调用 `arraySearch”。前两个函数调用 int 数组和 double 数组工作正常,似乎我只是缺少处理字符串数组的细节,我不知道它是什么。无论如何,它必须是一个数组(没有向量)。任何帮助将不胜感激!
#include<iostream>
#include<string>
using namespace std;
template<typename T>
bool arraySearch(T array[], int size, T thing)
{
for(int i = 0; i < size; i++)
{
if(array[i] == thing)
return true;
}
return false;
}
int main()
{
const int SIZE = 12;
int intArray[] = {14, 3, 6, 76, 34, 22, 21, 54, 33, 23, 76, 234};
cout << "The element was found: " << arraySearch(intArray, SIZE, 23) << endl;
double doubleArray[] = {34.5, 65.56, 11.1, 45.4, 87.5, 98.3, 23.6, 15.5, 3.3, 5.44, 54.3, 99.9};
cout << "The element was found: " << arraySearch(doubleArray, SIZE, 23.6) << endl;
string stringArray[] = {"cool", "bug", "master", "katze", "republic", "randolph", "watermelon", "igloo", "sardine", "cream", "yellow", "rubber"};
cout << "The element was found: " << arraySearch(stringArray, SIZE, "cool") << endl;
system("pause");
return 0;
}