简体   繁体   English

致命错误:“std::vector”中没有名为“find”的成员<int, std::allocator<int> &gt;'</int,>

[英]fatal error: no member named 'find' in 'std::vector<int, std::allocator<int> >'

Can someone tell why am I getting this error有人可以告诉我为什么会收到此错误

fatal error: no member named 'find' in 'std::vector<int, std::allocator<int> >'
         if(people.find(people.begin(),people.end(),x)!=people.end())
#include<iostream>
#include<vector>
#define REP(i,a,b) for(int i=a ; i<b ; i++)
using namespace std;
int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>> friends;
    vector<int> people;
    vector<int> cost_of_person;
    REP(i,0,n){
        cin >> cost_of_person[i];
        people.push_back(i+1);
    }
    REP(i,0,m){
        int x,y;
        cin >> x >> y;
        if(people.find(people.begin(),people.end(),x)!=people.end()) // error here
            people.erase(x);
        if(people.find(people.begin(),people.end(),y)!=people.end())
            people.erase(y);
        bool inserted = false;
        REP(j,0,friends.size())
        .
        .
        .
        .


    return 0;
}

std::vector does not have a find() member method. std::vector没有find()成员方法。 You need to use the std::find() algorithm instead:您需要改用std::find()算法:

#include <algorithm>

if (std::find(people.begin(), people.end(), x) != people.end())
    people.erase(x);
if (std::find(people.begin(), people.end(), y) != people.end())
    people.erase(y);

However, you will then get a new error, because std::vector::erase() does not take an element value as input, it takes an iterator instead.但是,您将收到一个新错误,因为std::vector::erase()不将元素值作为输入,而是使用迭代器 So you need to fix that, too:所以你也需要解决这个问题:

vector<int>::iterator iter = std::find(people.begin(), people.end(), x);
if (iter != people.end())
    people.erase(iter);
iter = std::find(people.begin(), people.end(), y);
if (iter != people.end())
    people.erase(iter);

Another problem I see with your code is you are not adding elements to your cost_of_person vector correctly, corrupting memory.我在您的代码中看到的另一个问题是您没有正确地将元素添加到您的cost_of_person向量中,从而破坏了 memory。 You have two choices to fix that:你有两个选择来解决这个问题:

  • resize cost_of_person before entering the loop:在进入循环之前调整cost_of_person的大小:

     vector<int> cost_of_person(n); REP(i,0,n){... }

    Or或者

    vector<int> cost_of_person; cost_of_person.resize(n); REP(i,0,n){... }
  • use cost_of_person.push_back() inside the loop:在循环内使用cost_of_person.push_back()

     vector<int> cost_of_person; REP(i,0,n){ int cost; cin >> cost; cost_of_person.push_back(cost); people.push_back(i+1); }

The problem is here:问题在这里:

 if(people.find(people.begin(),people.end(),x).=people.end())

You are using member function of object people named find .您正在使用名为find的 object people的成员 function 。 However, it turns out that people is defined as:然而,事实证明, people被定义为:

 vector<int> people;

It is a vector.它是一个向量。 And that type doesn't have a member function named find .并且该类型没有名为find的成员 function 。 Because you call a non-existing member function, the program is ill-formed.因为你调用了一个不存在的成员 function,所以程序是非良构的。 As a consequence, you get a diagnostic message from the compiler:因此,您会从编译器收到一条诊断消息:

fatal error: no member named 'find' in 'std::vector >'致命错误:“std::vector >”中没有名为“find”的成员

To fix it, don't call functions that don't exist.要修复它,请不要调用不存在的函数。

暂无
暂无

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

相关问题 std :: vector <int,std :: allocator <char >>是否有效? - Is std::vector<int, std::allocator<char> > valid? C ++模板错误:调用std :: vector没有匹配功能 <int, std::allocator<int> &gt; - C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> > 错误:'类 std::vector <route, std::allocator<route> &gt;' 没有名为“exitPoint”的成员</route,> - error: ‘class std::vector<route, std::allocator<route> >’ has no member named ‘exitPoint’ g++:'类标准::map <int, std::vector<std::vector<int> &gt; &gt;' 没有名为 'insert_or_assign' 的成员</int,> - g++: 'class std::map<int, std::vector<std::vector<int> > >' has no member named 'insert_or_assign' 反向迭代器错误:&#39;rcit!= std :: vector &lt;_Tp,_Alloc&gt; :: rend()中的&#39;operator!=&#39;与_Tp = int,_Alloc = std :: allocator&#39;不匹配 - reverse iterator error : no match for 'operator!=' in 'rcit != std::vector<_Tp, _Alloc>::rend() with _Tp = int, _Alloc = std::allocator' 运行时错误:引用绑定到类型为“std::vector”的空指针<int, std::allocator<int> &gt;&#39; (stl_vector.h) - runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h) 从不兼容类型&#39;value_type&#39;(aka&#39;std :: __ 1 :: vector)分配给&#39;int *&#39; <int, std::__1::allocator<int> &gt;&#39;) - Assigning to 'int *' from incompatible type 'value_type' (aka 'std::__1::vector<int, std::__1::allocator<int> >') 合并间隔/第 1034 行:字符 9:运行时错误:引用绑定到类型为“std::vector”的空指针<int, std::allocator<int> &gt;&#39; - Merge Intervals/Line 1034: Char 9: runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' 在 std::map &lt; int , std::vector 中查找 vector 中元素的值<int > &gt; - Find a value of an element in vector in a std::map < int , std::vector <int >> 没有来自“lambda []()-&gt; 的合适的用户定义转换<error-type> &quot; 到 &quot; 常量 std::vector <int, std::allocator<int> &gt;&quot; - no suitable user-defined conversion from "lambda []()-><error-type>" to "const std::vector<int, std::allocator<int>>"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM