mercredi 1 juillet 2015

Any better ways to do calculations using each 2 elements in a vector

I have a very time consuming function, which needs to so some calculation using each 2 elements in a std::vector. The way I am doing now is,

std::vector<int> vec;
for (auto it = vec.begin(); it != vec.end(); ++ it)
  for (auto it2 = vec.begin(); it2 != vec.end(); ++ it2)
    if (it2 != it)
      f(*it, *it2) // the function

I am wondering if there are any other better ways to do this, because this process cost too much time.

Besides, I have tried to use OpenMP to parallelize the outer loop, it works fine when I use std::vector, but if I do similar things with std::map, it returns a segmentation fault.

Thanks.

Aucun commentaire:

Enregistrer un commentaire