我确信我正在使这比它需要的更难。
我有一个 vector ...
vector<Joints> mJointsVector;
...由以下结构组成:
struct Joints
{
string name;
float origUpperLimit;
float origLowerLimit;
};
我正在尝试使用“std::find”搜索 mJointsVector 以通过其字符串名称定位单个关节 - 到目前为止运气不佳,但以下示例至少在概念上有所帮助:
Vectors, structs and std::find
谁能进一步指出我正确的方向?
最佳答案
直接的方法:
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 的内容减少代码量。
关于c++ - 在结构的 c++ std vector 中搜索具有匹配字符串的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2025938/