#include <iostream> 
    #include <vector> 
    #include <algorithm> 
  
    using namespace std; 
  
class A 
    { 
          public : 
          void getData(); 
          void putData(); 
          bool operator==(const A &) const;

          private : 
                  int x; 
                  char name[90]; 
    }; 
    vector< A > v; 
  
    void A :: getData() 
    { 
         cout << "id = "; 
         cin >> x; 
         cout << "\nname = "; 
         cin >> name; 
  
    } 
  
    void A :: putData() 
    { 
     cout << "size of vector is " << Aref.size()  << endl; 
     cout << x << " : " << name << endl;         
    } 
     
    bool A :: operator==(const A &t) const
    {
     return ( x == t.x && (strcmp(name, t.name) == 0) ); 
    } 
  
  
    int main() 
    { 
        for(int i=0; i < 3; i++) 
        { 
        v.push_back(A()); 
        v[i].getData(); 
        v[i].putData(); 
        } 
      vector< A >::iterator beg = v.begin(), en = v.end(); 
      v.erase( find(beg, en, v[1]) );  
      //Deletes the second object, but I want to search the whole object 
      //array by its attribute such as name or x(employee number) and then 
      //delete the object, if match found

    cin.ignore(numeric_limits< streamsize >::max(), '\n');
    cin.get();
      return 0;    
    }

I want to search the array of objects in terms of the objects attributes (such as x, name)
when I find the particular value for any object(such as x or name) i delete that object

if possible how do I use remove, find_if or functors?

Recommended Answers

All 5 Replies

std::find_if certainly will do what you want. However, realize that it only finds the first occurrence over the range you provide. If you want to delete multiple values you will have to search until there is no match.

Here is an example of finding and deleting a single value:

#include <iostream>
#include <vector>
#include <algorithm>

struct Foo {
    int val_;
    Foo(int v) : val_(v) {}
};

struct Pred {
    int want_;
    bool operator() (const Foo& f) { return want_ == f.val_; }
    Pred (int x) : want_(x) {}
};

int main () {
    std::vector< Foo > v;
    for (int i = 0; i < 5; ++i)
        v.push_back (Foo(i));
    std::cout << "length of vector: " << v.size () << std::endl;
    std::vector< Foo >::iterator vit;
    vit = std::find_if (v.begin (), v.end (), Pred(3));
    if (vit != v.end ()) {
        std::cout << "Found item to delete..." << std::endl;
        v.erase (vit);
    }
    std::cout << "length of vector: " << v.size () << std::endl;
    return 0;
}

As L7Sqr said. And, of course, since find_if/find only finds the first occurrence, it is a trivial matter to wrap it in a while loop to find all the occurrences:

int main() {
  std::vector<int> v;
  //populate v. Now, delete all 0 values:
  std::vector<int>::iterator vit = v.begin();
  do {
    vit = std::find(vit, v.end(), 0);
  } while ((vit != v.end()) && ((vit = v.erase(vit)) != v.end()));
};

Additionally, you can define a generic predicate to check a particular data member:

#include <iostream>
#include <vector>
#include <algorithm>

struct Foo {
    int val_;
    Foo(int v) : val_(v) {}
};

template <typename ClassType,typename DataType>
struct check_data_member {
    DataType want_;
    DataType ClassType::*pmember;
    bool operator() (const Foo& f) { return want_ == f.*pmember; }
    Pred (const DataType& x, DataType ClassType::*aPMember) : want_(x), pmember(aPMemeber) {}
};

template <typename ClassType,typename DataType>
check_data_member<ClassType,DataType> make_check(const DataType& val, DataType ClassType::*pmember) {
  return check_data_member<ClassType,DataType>(val,pmember);
};

int main () {
    std::vector< Foo > v;
    for (int i = 0; i < 5; ++i)
        v.push_back (Foo(i));
    std::cout << "length of vector: " << v.size () << std::endl;
    std::vector< Foo >::iterator vit;
    vit = std::find_if (v.begin (), v.end (), make_check(3,&Foo::val_));
    if (vit != v.end ()) {
        std::cout << "Found item to delete..." << std::endl;
        v.erase (vit);
    }
    std::cout << "length of vector: " << v.size () << std::endl;
    return 0;
}

However, if you want your look-ups to be fast, I suggest you use Boost.MultiIndex library to create efficient look-ups via different data members or methods. The library also includes standard generic predicates for accessing data members and such, like the one I did above.

commented: Thanks, i get it +1

As L7Sqr said. And, of course, since find_if/find only finds the first occurrence, it is a trivial matter to wrap it in a while loop to find all the occurrences:

int main() {
  std::vector<int> v;
  //populate v. Now, delete all 0 values:
  std::vector<int>::iterator vit = v.begin();
  do {
    vit = std::find(vit, v.end(), 0);
  } while ((vit != v.end()) && ((vit = v.erase(vit)) != v.end()));
};

Additionally, you can define a generic predicate to check a particular data member:

#include <iostream>
#include <vector>
#include <algorithm>

struct Foo {
    int val_;
    Foo(int v) : val_(v) {}
};

template <typename ClassType,typename DataType>
struct check_data_member {
    DataType want_;
    DataType ClassType::*pmember;
    bool operator() (const Foo& f) { return want_ == f.*pmember; }
    Pred (const DataType& x, DataType ClassType::*aPMember) : want_(x), pmember(aPMemeber) {}
};

template <typename ClassType,typename DataType>
check_data_member<ClassType,DataType> make_check(const DataType& val, DataType ClassType::*pmember) {
  return check_data_member<ClassType,DataType>(val,pmember);
};

int main () {
    std::vector< Foo > v;
    for (int i = 0; i < 5; ++i)
        v.push_back (Foo(i));
    std::cout << "length of vector: " << v.size () << std::endl;
    std::vector< Foo >::iterator vit;
    vit = std::find_if (v.begin (), v.end (), make_check(3,&Foo::val_));
    if (vit != v.end ()) {
        std::cout << "Found item to delete..." << std::endl;
        v.erase (vit);
    }
    std::cout << "length of vector: " << v.size () << std::endl;
    return 0;
}

However, if you want your look-ups to be fast, I suggest you use Boost.MultiIndex library to create efficient look-ups via different data members or methods. The library also includes standard generic predicates for accessing data members and such, like the one I did above.

Your code does not compiles, gives an error
15 C:\Documents and Settings\munish\My Documents\Dev myprograms\DEV C++\vector_obj4.cpp `aPMemeber' undeclared (first use this function)

aPMember seems declared but why this error now

Ohh sorry mikael for the message, I got this its O.K

there was a spelling mistake

solved now

Sorry, it was just a couple of typos in the writing of check_data_member. Here is a replacement:

template <typename ClassType,typename DataType>
struct check_data_member {
    DataType want_;
    DataType ClassType::*pmember;
    bool operator() (const Foo& f) { return want_ == f.*pmember; }
    check_data_member(const DataType& x, DataType ClassType::*aPMember) : want_(x), pmember(aPMember) {}
};

Notice the constructor's name ('check_data_member' and not 'Pred') and the fixed typo with 'aPMember'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.