【问题标题】:error: no matching function for call to ‘recherche(std::vector >&, std::vector >::iterator, std::vector >::iterator, const char [10])’错误:没有匹配函数调用“recherche(std::vector >&, std::vector >::iterator, std::vector >::iterator, const char [10])”
【发布时间】:2022-01-19 02:26:10
【问题描述】:

我收到了这些错误:

error: no matching function for call to ‘recherche(std::vector >&, std::vector >::iterator, std::vector >::iterator, const char [10])’
error: no matching function for call to ‘recherche(std::__cxx11::list&, std::__cxx11::list::iterator, std::__cxx11::list::iterator, int)’
error: no matching function for call to ‘recherche(std::array&, std::array::iterator, std::array::iterator, double)’
#include<iostream>
#include<vector>
#include<array>
#include<list>
#include<algorithm>
#include<iterator>
#include<set>

using namespace std;

template <template<typename> class C, class InputIterator, typename A>
bool recherche(C<A> s, InputIterator debut, InputIterator fin, A n)
{
    InputIterator itr;
    for (itr = debut; itr != fin; itr++) {
        if(*itr == n){
            return 1;
        }}
    return 0;
}
int main(){
    vector<string> strVec = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
    list<int> my_list = { 12, 5, 10, 9, 4, 90 };
    array<float, 5> arr = { 10.56, 20.99, 30.34, 40.12, 10.1 };
    
    cout << recherche(strVec, strVec.begin(), strVec.end(), "Wednesday") << endl;
    cout << recherche(my_list, my_list.begin(), my_list.end(), 90) << endl;
    cout << recherche(arr, arr.begin(), arr.end(), 30.34) << endl;
    
    return 0;
    }
}

【问题讨论】:

  • 在此处查看接受的答案:stackoverflow.com/questions/16925041/…。另外std::vector 是一个具有两种模板参数类型的模板,而不是一种,所以这可能是您收到错误的原因
  • 另一件会咬你的事:你搜索30.34,这使得A 成为double。它不等于您存储在arr 中的30.34f
  • 将容器和迭代器都传递给你的recherche函数是没有用的。
  • 您的 recherche() 函数正在复制标准 std::find() 算法已经完成的工作。当标准算法可以完成相同的工作时,编写自己的函数几乎没有什么好处。 template &lt;class InputIterator, class T&gt; bool recherche(InputIterator debut, InputIterator fin, T n) { return std::find(debut, fin, n) != fin; }

标签: c++ arrays c++11 vector


【解决方案1】:

std::vector 接受两个模板参数,一个用于元素,一个用于分配器。解决此问题的一种方法是在函数中同时接受它们:

template <template<typename, typename> class C, class InputIterator, typename Elem, typename Alloc>
bool recherche(C<Elem, Alloc> s, InputIterator debut, InputIterator fin, Elem n)
{
    InputIterator itr;
    for (itr = debut; itr != fin; itr++) {
        if(*itr == n){
            return 1;
        }}
    return 0;
}

不过,您也可以利用所有 STL 容器实现的 value_type 来获取元素值类型:

template<class Vec, class Elem = Vec::value_type>
void foo(Vec v, Elem e) {
    // the element type is extracted in compile time. The only constraint is
    // that your container must implement value_type and all stl containers do so
    // Of course Vec is not necessarily a vector, it can be a list, an array, an unordered_map etc.
}

【讨论】:

  • 您应该能够在 C++11 及更高版本中省略 Alloc 参数:template &lt;template&lt;typename...&gt; class C, class InputIterator, typename Elem&gt; bool recherche(C&lt;Elem&gt; s, InputIterator debut, InputIterator fin, Elem n)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 2017-07-03
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多