1

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Abhishek Shinde
  • 21
  • 1
  • 1
  • 2
  • 4
    Well, that's because [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) class doesn't have any member function called `find()`. – Yksisarvinen May 19 '20 at 15:16
  • Does this answer your question? [Erasing elements from a vector](https://stackoverflow.com/questions/347441/erasing-elements-from-a-vector) – Yksisarvinen May 19 '20 at 15:17
  • Generally, you can also use ```std::find``` in conjunction with a vector, http://www.cplusplus.com/reference/algorithm/find/ – ctenar May 19 '20 at 15:19
  • All containers in the std only have the absolute minimum of member functions that are required to control their contents. Everything else is done with free functions, that provide a container independent interface using iterators. – t.niese May 19 '20 at 15:25

2 Answers2

2

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);
    }
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326