5

Is there any difference performance/safe-wise inspecting vector elements using

  1. for loop with iterators

vs.

  1. std:find_if(...)?

1. for loop

// 1. for loop  
for (llvm::SmallVectorImpl<myClass>::const_iterator it = v.begin();
     it != v.end();
     ++it) {
    if (it->getName() == Name) {
        // found element
        // do smth...
        break;
    }
}

vs.

2. std:find_if

// 2. find if
llvm::SmallVectorImpl<myClass>::const_iterator it
    = std::find_if(v.begin(),
                   v.end(),
                   StringCheck<llvm::StringRef>(Name));
if (it != v.end()) {
    // found element
    // do 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_;
};
J_T
  • 71
  • 1
  • 5
  • Nothing different "safe-wise". For performance you'll have to benchmark. – StoryTeller - Unslander Monica Aug 08 '16 at 14:03
  • Ultimately, `std::find_if` will iterate the vector, so I guess no performance difference. – DimChtz Aug 08 '16 at 14:06
  • There [*may* be a performance difference either way.](http://stackoverflow.com/a/36988833/4892076) Any such difference is small enough to be a premature optimization unless you've actually identified the loop as a bottleneck though, and in that case you'll want to do the profiling yourself. – jaggedSpire Aug 08 '16 at 14:07
  • 2
    Really the only difference I can see is the iterator loop will find all cases unless you use a break where `find_if` will implicitly stop at the first occurrence. – NathanOliver Aug 08 '16 at 14:07
  • How do you think `std::find_if` is implemented - black magic? Actually, you don't need to guess - you can look at the implementation, it came with your compiler. You'll find a loop with iterators there. – Igor Tandetnik Aug 08 '16 at 14:08
  • According to Sean Parent's C++ seasoning, you should avoid using "raw for loops", and favor generic algorithms. The first major advantage of using an algorithm is readability: it's much easier to understand that you are searching for an object when you read `find_if` in the code rather than `for(...) { ... if( x == Y ) { ... } }`. – KABoissonneault Aug 08 '16 at 14:19
  • @KABoissonneault, easier said than done in pre-c++11. Otherwise you are correct. – StoryTeller - Unslander Monica Aug 08 '16 at 15:22
  • @NathanOliver: I added a break; in the for loop, since my use case was to find just first match. Currently I am limited to c++03 usage but notes for c++11 are also interesting for me. – J_T Aug 08 '16 at 16:47
  • @J_T Then there should be no difference except for the people who feel that seeing `break` in a loop is code smell. – NathanOliver Aug 08 '16 at 16:51
  • In case I need multiple matches what would be the correct way using ? Or simple for is the best shot then? – J_T Aug 08 '16 at 17:23

1 Answers1

0

Your for-loop continues iterating after a match is found. This could be a good thing if multiple matches are possible and you want to run the code for each match, or a bad thing if you wanted to stop after a match is found and the match is found early in a large container.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • In my use case I want to finish iterating after first match, so I added break; in the for loop. – J_T Aug 08 '16 at 16:42