Advantages of std::for_each over for loop

后端 未结 21 712
遥遥无期
遥遥无期 2020-11-29 15:23

Are there any advantages of std::for_each over for loop? To me, std::for_each only seems to hinder the readability of code. Why do then some coding

21条回答
  •  既然无缘
    2020-11-29 15:50

    its very subjective, some will say that using for_each will make the code more readable, as it allows to treat different collections with the same conventions. for_each itslef is implemented as a loop

    template
      Function for_each(InputIterator first, InputIterator last, Function f)
      {
        for ( ; first!=last; ++first ) f(*first);
        return f;
      }
    

    so its up to you to choose what is right for you.

提交回复
热议问题