
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Algorithm Library - find_end() Function
Description
The C++ function std::algorithm::find_end() finds the last occurrence of the element. It uses operator = for comparison.
Declaration
Following is the declaration for std::algorithm::find_end() function form std::algorithm header.
C++98
template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
Parameters
first1 − Forward iterator to the initial position of the first sequence.
last1 − Forward iterator to the final position of the first sequence.
first2 − Forward iterator to the initial position of the second sequence.
last2 − Forward iterator to the final position of the second sequence.
Return value
Returns an iterator to the first element of the last occurrence of (first2,last2) in first1,last1.
Exceptions
Throws exception if either element comparison or an operation on an iterator throws exception.
Please note that invalid parameters cause undefined behavior.
Time complexity
Linear.
Example
The following example shows the usage of std::algorithm::find_end() function.
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(void) { vector<int> v1 = {1, 2, 1, 2, 1, 2}; vector<int> v2 = {1, 2}; auto result = find_end(v1.begin(), v1.end(), v2.begin(), v2.end()); if (result != v1.end()) cout << "Last sequence found at location " << distance(v1.begin(), result) << endl; v2 = {1, 3}; result = find_end(v1.begin(), v1.end(), v2.begin(), v2.end()); if (result == v1.end()) cout << "Sequence doesn't present in vector." << endl; return 0; }
Let us compile and run the above program, this will produce the following result −
Last sequence found at location 4 Sequence doesn't present in vector.