在类对象的向量中找到一个值

find a value in a vector of class objects

本文关键字:一个 对象 向量      更新时间:2023-10-16

我写了一个基于类的向量:

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 ;
    }
}

现在,我想在a1中搜索一个值。假设我想找到包含"CC"的元素的索引。

我找到了这些类似的解决方案:

根据对象属性搜索对象向量

std::find Object by Member

我如何找到一个基于类属性的矢量对象?

如何在std::set中找到具有特定字段值的对象?

当我实现本节中的所有注释时,我仍然得到错误!我错过了什么?我猜问题出在定义vector::iterator

错误C2679: binary '=':找不到右操作数类型为'std::_Vector_iterator<_Myvec>'的操作符(或者没有可接受的转换)

,

error C2678: binary '!=':找不到左操作数类型为'std::_Vector_iterator<_Myvec>'的操作符(或者没有可接受的转换)

必须使用标准算法std::find_if

it = find_if(a1.begin(), a1.end(), A::Finder("CC"));

考虑内部类应该像

那样定义
struct Finder {
    Finder(std::string const& n) : name(n) { }  
    bool operator () ( const A & el) const  { return el.b == name; }
private:
    std::string name;
};
  1. 要使用Predicate,您需要使用std::find_if,而不是std::find

    it = std::find_if(a1.begin(), a1.end(), A::Finder("CC"));
    
  2. Finder::operator()的参数类型中使用const&

    不是

    bool operator () (A & el) { return el.b == name; }
    
    使用

    bool operator () (A const& el) { return el.b == name; }
    

    UnaryPredicate的要求之一是(来自http://en.cppreference.com/w/cpp/concept/Predicate)

    函数对象pred不能通过解引用迭代器应用任何非常数函数。

    许多编译器将其理解为实参类型必须是value或const&

我找到了两个解决方案:

  1. 你可以在c++中实现std::find:

    class A {
        private:
           string b;
           string c;
        public:
        A(string i) : b(i) {}
        A(string n, string l) { b = n ;c = l; }
        string Getb(){ return b; }
        string Getc(){ return c; }
        bool operator==(const A & obj2) const
        {
           return (this->b.compare(obj2.b) == 0); 
        }
    };
    
    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"));
       auto it = find(a1.begin(), a1.end(), A("CC"));
       if (it != a1.end()) {
          auto idx = distance(a1.begin(), it);
          cout << "b= " << it->Getb() << " c= " << it->Getc() << endl;
          cout << "Index= " << idx << endl;
        } else
          cout << "CC is not found" << endl;
    return 0;
    }
    
  2. 你可以在c++中实现std::find_if在基于向量类/结构的对象中(感谢@Vlad from Moscow和@R Sahu):

    class A {
        private:
            string b;
            string c;
        public:
            A(string n, string l) { b = n ;c = l; }
            string Getb(){ return b; }
            string Getc(){ return c; }
            struct Finder {
                Finder(string const & n) : name(n) { }  
                bool operator () (const A & el) const { 
                     return el.Pos == name; 
                 }
                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<A>::iterator it;
         it = find_if(a1.begin(), a1.end(), A::Finder ("CC"));
         if (it != a1.end()) {
             auto idx = distance(a1.begin(), it);
             cout << "b= " << it->Getb() << " c= " << it->Getc() << endl;
             cout << "Index= " << idx << endl;
         } else
             cout << "CC is not found" << endl;
      return 0;
     }
    
相关文章: