Reputation: 21
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;
}
Upvotes: 1
Views: 18176
Reputation: 596417
std::vector
does not have a find()
member method. You need to use the std::find()
algorithm instead:
#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. 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. You have two choices to fix that:
resize cost_of_person
before entering the loop:
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:
vector<int> cost_of_person;
REP(i,0,n){
int cost;
cin >> cost;
cost_of_person.push_back(cost);
people.push_back(i+1);
}
Upvotes: 2
Reputation: 238351
The problem is here:
if(people.find(people.begin(),people.end(),x)!=people.end())
You are using member function of object people
named find
. However, it turns out that people
is defined as:
vector<int> people;
It is a vector. And that type doesn't have a member function named find
. Because you call a non-existing member function, the program is ill-formed. As a consequence, you get a diagnostic message from the compiler:
fatal error: no member named 'find' in 'std::vector >'
To fix it, don't call functions that don't exist.
Upvotes: 0