jeudi 18 octobre 2018

How to find and update one nested structure in a vector in C++11

I have a requirement to find if a structure exists with a vector which contains a nested structure:

#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;
}

The only logic I have in SetDetails is to traverse through vector and then verify its details but is there a better way to exactly match and update that structure in the vector?

Aucun commentaire:

Enregistrer un commentaire