Skip to content

Commit

Permalink
Merge pull request Bhupesh-V#481 from dhornacek/find_end
Browse files Browse the repository at this point in the history
Algorithms: Add find_end markdown and snippet
  • Loading branch information
Bhupesh-V authored Oct 5, 2020
2 parents 635aa66 + 9086115 commit 92fd8ae
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions algorithm/find_end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# find_end

**Description :** Return an iterator which points to the first element of the last occurrence of the sequence [s_first, s_last) in the range target [first, last). If list is empty or sequence isn't found, returns last.

**Example** :

```cpp
std::vector<int> sequence{ 13, 5, 2, };
std::vector<int> target{ 18, 11, 7, 13, 5, 2, 12, 5, 13, 5, 2 };

// find the first element of the last occurrence of the given sequence
std::vector<int>::iterator result = std::find_end(target.begin(), target.end(), sequence.begin(), sequence.end());

if(result != target.end()){
// list wasn't empty and sequence occurred
std::cout << "The result is: " << std::distance(target.begin(), result) << "." << std::endl;
} else{
std::cout << "No such pattern found or lists are empty." << std::endl;
}
```
**[See Sample code](snippets/vector/find_end.cpp)**<br>
**[Run Code](https://rextester.com/UYNGGS38128)**
26 changes: 26 additions & 0 deletions snippets/algorithm/find_end.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Author : Derek Hornacek
Date : 16/03/2020
Time : 05:24pm
Description : This is a code snippet demonstrating the find_end algorithm from the C++ STL.
*/

#include <iostream>
#include <algorithm>
#include <vector>

int main(){
std::vector<int> sequence{ 13, 5, 2, };
std::vector<int> target{ 18, 11, 7, 13, 5, 2, 12, 5, 13, 5, 2 };

// find the first element of the last occurrence of the given sequence
std::vector<int>::iterator result = std::find_end(target.begin(), target.end(), sequence.begin(), sequence.end());

if(result != target.end()){
// list wasn't empty and sequence occurred
std::cout << "The result is: " << std::distance(target.begin(), result) << "." << std::endl;
} else{
std::cout << "No such pattern found or lists are empty." << std::endl;
}
return 0;
}

0 comments on commit 92fd8ae

Please sign in to comment.