简体   繁体   English

没有匹配的 function 来调用。 C++ STL

[英]No Matching function to call. C++ STL

I'm new to C++ STL.我是 C++ STL 的新手。 I'm writing a function to check whether one vector is an subset of another(Duplicate elements also count) and print 'Yes' or 'No'.我正在编写 function 来检查一个向量是否是另一个向量的子集(重复元素也算在内)并打印“是”或“否”。 I have come up with the following code:我想出了以下代码:

void checkMagazine(vector<string> magazine, vector<string> note) {
    vector<string>::iterator a;
    for(int i=0;i<note.size();i++)
    {
        a=find(magazine.begin(),magazine.end(),note[i]);
        if(a==magazine.end())
        {
            printf("No");
            return;
        }else magazine.erase(a-magazine.begin());
    }
    printf("Yes");
}

But,I'm getting the following compilation error:但是,我收到以下编译错误:

error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char>
>::erase(__gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*,
std::vector<std::__cxx11::basic_string<char> > >::difference_type)’
         }else magazine.erase(a-magazine.begin());

I'm trying to use erase to delete the found element in magazine.Is there some problem with the type of declaration of the iterator?我正在尝试使用擦除删除杂志中找到的元素。迭代器的声明类型有问题吗? Why is this happening?为什么会这样? And also if there are alternate methods/logic/inbuilt-functions to get the required job done using STL, Please let me know since I'm new to this and trying to learn it.而且,如果有替代方法/逻辑/内置功能可以使用 STL 完成所需的工作,请告诉我,因为我是新手并试图学习它。

erase takes an iterator. erase需要一个迭代器。 You have that iterator, it's called a .你有那个迭代器,它叫做a But you then convert the iterator a into an offset relative to begin() .但是您随后将迭代器a转换为相对于begin()的偏移量。 erase does not have an overload which takes an offset. erase没有带偏移量的过载。

The Standard Library has alternatives if the vectors would be sorted, but we don't see the calling code so we can't assume that.如果对向量进行排序,标准库有替代方案,但我们看不到调用代码,所以我们不能假设。

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

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