forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Bhupesh-V#481 from dhornacek/find_end
Algorithms: Add find_end markdown and snippet
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)** | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |