mardi 29 mai 2018

How to calc inner product of two vectors by stl container and algorithm libraries?

sure there are many ways:

assert(v1.size() == v2.size());
for (auto&& i = 0; i < v1.size(); ++i) {
    acc += v1[i]*v2[i];
}

but there is a zip operator in python, so python code would be easy as following codes:

acc = accumulate([ele1 * ele2 for ele1, ele2 in zip(v1, v2)])

my question is: are there any similar approaches to write some codes as styled as python code? especially by std::algorithm library.

Aucun commentaire:

Enregistrer un commentaire