Summing struct members inside a vector

后端 未结 5 1094
闹比i
闹比i 2020-12-29 08:22

Consider the following:

typedef struct {
    int a;
    int b;
    int c;
    int d;
} ABCD;

typedef std::vector VecABCD;

Say

5条回答
  •  不知归路
    2020-12-29 09:19

    Let's add one more option, unfortunately an ugly one. It's possible to take the relative address from start of the struct ABCD to it's member by offsetof -function. Deliver return value to function and it can do counting using relative location from the start of each struct. If your types may differ from int, you may want to deliver also the sizeof information.

提交回复
热议问题