How to copy an array of struct to an equal one?

Asked

Viewed 323 times

2

I have 2 arrays made of a struct:

struct events {
    string Kind;
    int Time;
    int DurVal;
    int Chan;
};

events e1[100]
events e2[100];

How can I make a full copy of the array e1 for e2 in a single command?

  • 1

    The answer of that question seems to be what you seek

1 answer

5


Since you are using code that you should only use in C and not in C++, you could use a C function:

memcpy(e1, e2, sizeof(events) * 100);

Documentation.

Or you could use a C function++:

copy(begin(e1), end(e1), begin(e2));

Documentation.

Or in the hand:

for (auto i = 0; i < 100; i++)  e2[i] = e1[i];

I put in the Github for future reference.

but I would use a vector or array which is more the face of C++ and is safer and faster in many cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.