c++ - Searching std::vector of class objects by class attribute (eg. name) -
is there difference performance/safe-wise inspecting vector elements using
- for loop iterators
vs.
- std:find_if(...)?
1. loop
// 1. loop (llvm::smallvectorimpl<myclass>::const_iterator = v.begin(); != v.end(); ++it) { if (it->getname() == name) { // found element // smth... break; } }
vs.
2. std:find_if
// 2. find if llvm::smallvectorimpl<myclass>::const_iterator = std::find_if(v.begin(), v.end(), stringcheck<llvm::stringref>(name)); if (it != v.end()) { // found element // smth... } // stringcheck defined in header... template <class t> struct stringcheck{ stringcheck(const t &s) : s_(s) {} bool operator()(const myclass &obj) const { return obj.getname() == s_; } private: const t &s_; };
your for-loop continues iterating after match found. thing if multiple matches possible , want run code each match, or bad thing if wanted stop after match found , match found in large container.
Comments
Post a Comment