如何在 C++11 中查找和更新向量中的一个嵌套结构
How to find and update one nested structure in a vector in C++11
我有一个需要查找结构是否存在包含嵌套结构的向量:
#include <iostream>
#include <algorithm>
#include <vector>
typedef struct Obj {
int id[128];
int len;
} ID;
typedef struct Details {
ID id;
int fwid;
int respfmt;
} IDDetails;
std::vector<IDDetails> details =
{
{ { { 2, 2, 1 }, 3 }, 0, 1 },
{ { { 2, 2, 2 }, 3 }, 0, 2 }
};
class A {
public:
int SetDetails(std::vector<IDDetails>& ids, ID &id)
{}
};
int main()
{
A a;
ID d = { { 2, 2, 1 }, 3 };
a.SetDetails(details, d);
return 0;
}
我在SetDetails中唯一的逻辑是遍历矢量,然后验证其详细信息,但是有没有更好的方法来精确匹配和更新矢量中的结构?
#include <cstring>
#include <algorithm>
// ...
bool operator==(ID const &lhs, ID const &rhs)
{
return lhs.len == rhs.len && std::memcmp(lhs.id, rhs.id, lhs.len) == 0;
}
// ...
auto f = std::find_if(ids.begin(), ids.end(), [&](IDDetails const &i) { return i.id == id; });
?
相关文章:
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?
- 重构模板类,该类将其嵌套类用作另一个类的模板参数
- 另一个类中嵌套模板的外部实例化
- 访问另一个类中的模板化嵌套类函数和对象
- 嵌套的类成员变量不可访问.链表设置.V、下一个和新的问题
- 使用另一个模板类的嵌套名称说明符专用化模板类
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
- 如何将3个嵌套循环重写为一个
- 初始化时 num 的哪个值会给这个嵌套循环一个"true"输出?
- 如何在C++中的另一个模板函数中使用属于模板化类的嵌套类型?
- 如何在另一个CDialog中显示嵌套的CDialog
- 复制一个三重嵌套容器
- 为什么嵌套类不能有一个类型为封闭类的成员?
- typedef是一个专门的嵌套模板
- 如何定义一个模板类函数,该函数在类外的签名中具有嵌套类型的模板参数
- 为了简洁起见,在另一个文件中定义前向声明的嵌套结构/类/枚举
- 嵌套绑定到成员,需要一个指针,得到了一个引用.做什么