mardi 27 décembre 2016

How to compare and assign between std::vector

Here are two different type of std::vector, as an example:

std::vector<std::reference_wrapper<MyClass>> rv;
std::vector<MyClass> v;

A possible way to assign between them is:

for (const auto& mc : rv) {
    v.push_back(mc.get());
}

It works. But ugly and maybe slow. The same as comparison:

bool is_same(std::vector<MyClass>& v, std::vector<std::reference_wrapper<MyClass>>& rv) {
    if (v.size()!=rv.size()) {
        return false;
    }
    for (size_t i = 0; i < v.size(); v++) {
        if (v[i]!=rv[i].get()) {
            return false;
        }
    }
    return true;
}

Is there any better way to do this work? Smart and quick.

Aucun commentaire:

Enregistrer un commentaire