在类对象的向量中查找值

时间:2015-07-13 19:53:45

标签: c++ class object vector find

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

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”发生的元素索引。

我发现了这些类似的解决方案:

Search a vector of objects by object attribute

std::find Object by Member

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?

当我实现本节中的所有注释时,我仍然会收到错误!我错过了什么?我想问题是定义vector :: iterator it;

错误C2679:binary'=':找不到运算符,它采用'std :: _ Vector_iterator&lt; _Myvec&gt;'类型的右手操作数(或者没有可接受的转换)

错误C2678:binary'!=':找不到运算符,该运算符采用类型为'std :: _ Vector_iterator&lt; _Myvec&gt;'的左手操作数(或者没有可接受的转换)

3 个答案:

答案 0 :(得分:1)

  1. 要使用Predicate,您需要使用std::find_if,而不是std::find

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

    而不是

    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不应通过解除引用的迭代器应用任何非常量函数。

    许多编译器认为参数类型必须是值或const&

答案 1 :(得分:1)

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

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

考虑到内部clas应该被定义为

struct Finder {

    Finder(std::string const& n) : name(n) { }  

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

private:

    std::string name;

};

答案 2 :(得分:1)

我找到了两个解决方案:

  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 ++中的vector class / structure对象中实现std :: find_if(感谢来自莫斯科的@Vlad和@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;
     }