搜索具有匹配字符串的struct的结构体的c ++ std向量 [英] Searching c++ std vector of structs for struct with matching string
本文介绍了搜索具有匹配字符串的struct的结构体的c ++ std向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确定我比这更难。
我有一个向量...
vector<Joints> mJointsVector;
...由以下结构图案组成:
...comprised of structs patterned after the following:
struct Joints
{
string name;
float origUpperLimit;
float origLowerLimit;
};
我正在尝试使用std :: find搜索mJointsVector,以查找其中的单个关节字符串名称 - 到目前为止没有运气,但下面的例子至少在概念上有所帮助:
I'm trying to search mJointsVector with "std::find" to locate an individual joint by its string name - no luck so far, but the examples from the following have helped, at least conceptually:
任何人都可以进一步指向正确的方向? / p>
Can anyone point me further in the right direction?
推荐答案
一个直接的方法:
struct FindByName {
const std::string name;
FindByName(const std::string& name) : name(name) {}
bool operator()(const Joints& j) const {
return j.name == name;
}
};
std::vector<Joints>::iterator it = std::find_if(m_jointsVector.begin(),
m_jointsVector.end(),
FindByName("foo"));
if(it != m_jointsVector.end()) {
// ...
}
或者,您可能想查看一些类似 Boost.Bind 以减少代码量。
Alternatively you might want to look into something like Boost.Bind to reduce the amount of code.
这篇关于搜索具有匹配字符串的struct的结构体的c ++ std向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文