I have written a class-based vector:
class A {
private:
string b;
string c;
public:
A(string n, string l) { b = l ;c = n; }
struct Finder {
Finder(std::string const& n) : name(n) { }
bool operator () ( const A & el) const { return el.b == name; }
private:
std::string name;
};
};
int main()
{
vector<A> a1;
a1.push_back(A("AA","aa"));
a1.push_back(A("BB","bb"));
a1.push_back(A("CC","cc"));
a1.push_back(A("DD","dd"));
vector<string>::iterator it;
it = find_if(a1.begin(), a1.end(), A::Finder("CC"));
if (it != a1.end()) {
auto pos = it - a1.begin();
cout << "CC is found at " << pos ;
}
}
Now, I want to search for a value in a1. Let's say I want to find the index of element in which "CC" happens.
I found these similar solutions:
Search a vector of objects by object attribute
How can I find an object in a vector based on class properties?
How to find an object with specific field values in a std::set?
As I implement all the comments in this section, I still get the errors! What did I miss? I guess the problem is in defining vector::iterator it;
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion)
and,
error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion)