在类对象的向量中找到一个值
find a value in a vector of class objects
我写了一个基于类的向量:
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;
};
-
要使用
Predicate
,您需要使用std::find_if
,而不是std::find
。it = std::find_if(a1.begin(), a1.end(), A::Finder("CC"));
-
在
不是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&
。
我找到了两个解决方案:
-
你可以在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; }
-
你可以在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; }
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 包装一个对象并假装它是一个 int
- 为什么我可以在不重载 "=" 运算符的情况下将一个对象分配给另一个对象?
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- 如何使用函数将一个对象的输入复制到另一个对象中
- 选择一个元素而不是一个对象的数组的原因
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 打印对象的映射,其中另一个对象作为键
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 绘制一个对象,比较模具缓冲区的两个不同值