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

    using namespace std;

    class A
    {
          public :
          void getData(vector< A > &);
          void putData(vector< A > &);
      
          private :
                  int x;
                  char name[90];
    };
    vector< A > v;

    void A :: getData(vector< A > &Aref)
    {
         cout << "id = ";
         cin >> x;
         cout << "\nname = ";
         cin >> name;
 
    }

    void A :: putData(vector< A > &Aref)
    {
         cout << "size of vector is " << Aref.size()  << endl;
         for(int i=0; i < Aref.size(); i++)
         cout << Aref[i].x << " : " << Aref[i].name << endl;
         
         cin.ignore(numeric_limits< streamsize >::max(), '\n'); 
         cin.get();
    }
     

    int main()
    {
        for(int i=0; i < 3; i++)
        {
        v.push_back(A());
        v[0].getData(v);
        v[0].putData(v);
        }
        vector< A >::iterator beg = v.begin(), en = v.end();
        //v.erase( find(beg, en, v[1]) ); -->DOESN'T WORK
        return 0;   
    }

I have used this
v.erase( find(beg, en, v[1]) ); (Line 48)
to search and delete an object from the array
vector< A > v; (A is the name of class)

Does find accepts the object as its 3rd if not what ways can a follow to delete an object.In fact I want to search the particular attribute(such as id, name) value from the array of objects and then delete that object.

please advice?

Dani AI

Generated

was right about why the compiler vomited template errors: std::find needs a way to test equality for your A objects. There are three practical, modern choices (and a few style fixes in your posted loop that will avoid confusion).

First, simplest: search by attribute with a predicate instead of relying on operator==. With C++11+ you can use std::find_if (or std::remove_if + erase to delete all matches). Example — find one element by id and erase it:

std::vector<A>::iterator it =
    std::find_if(v.begin(), v.end(),
                 [&](const A& a){ return a.getId() == targetId; });

if (it != v.end())
    v.erase(it);

To delete every element that matches a condition, use the erase–remove idiom:

v.erase(std::remove_if(v.begin(), v.end(),
                       [&](const A& a){ return a.getId() == targetId; }),
        v.end());

If you must support older compilers without lambdas, write a small functor with operator() and pass that to find_if or remove_if. Alternatively, defining A::operator== is fine when equality semantics are unambiguous — but for “find by id” a predicate is clearer.

Other practical notes based on the code shown: the loop pushes back then always calls v[0].getData(...) — that means you only ever edit the first element; use v[i] or v.back() after push_back. Prefer std::string over char[], add int getId() const / const std::string& getName() const accessors, and keep I/O code out of the data class (separate concerns). Finally, always check it != v.end() before erasing, be mindful that erase invalidates iterators to removed elements, and consider using a modern compiler with C++11 support to take advantage of lambdas and safer idioms.

Recommended Answers

All 4 Replies

Did you find any error?

Did you find any error?

well a new tab opened when i compiled it.( stl_algo.h opened) and

got many lines of error i don't understand

C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h In function `_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<A*, std::vector<A, std::allocator<A> > >, _Tp = A]':

314 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h instantiated from `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = __gnu_cxx::__normal_iterator<A*, std::vector<A, std::allocator<A> > >, _Tp = A]'


and many more lines, I dont get it
compiler (DevC++ version )

If you look at this page http://www.cplusplus.com/reference/algorithm/find/ you'll see that find() requires the == operator to be defined on your class, and it's not.

Something like this probably:

bool A :: operator ==(const A &t) const
{
	return ((x == t.x) && (strcmp(name, t.name) == 0));
}

On a side note, why did you define putData() as a member-function of A even though it doesn't do anything with the A it's called from?

And why does getData() take an argument which it doesn't use?

If you look at this page http://www.cplusplus.com/reference/algorithm/find/ you'll see that find() requires the == operator to be defined on your class, and it's not.

Something like this probably:

bool A :: operator ==(const A &t) const
{
	return ((x == t.x) && (strcmp(name, t.name) == 0));
}

On a side note, why did you define putData() as a member-function of A even though it doesn't do anything with the A it's called from?

And why does getData() take an argument which it doesn't use?

Thanks, I got this

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.