mardi 28 février 2023

How can I fill a vector with another smaller vector in c++?

I want to fill a std::vector with another vector . For example:

std::vector<int> src = {1, 2, 3};
std::vector<int> dst(9);

I want to fill dst to make it become: 1, 2, 3, 1, 2, 3, 1, 2, 3

Is there an efficient method to do this?

I have two methods now:

The first is two loop:

for (int i = 0; i < dst.size() / src.size(); i++) {
    for (int val : src) {
        dst.emplace_back(val);
    }
}

Or use the std::copy in one loop.

Maybe exist a more efficient method?

Aucun commentaire:

Enregistrer un commentaire