[英]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.