c++ - Searching std::vector of class objects by class attribute (eg. name) -


is there difference performance/safe-wise inspecting vector elements using

  1. for loop iterators

vs.

  1. 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

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -