TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm find_end() function

C++ algorithm find_end() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Function find_end ()

C++ Algorithm find_end()function searches for the last occurrence of a pattern in the container, or say the last occurrence of a small portion of the sequence in the container. It basically searches the range specified by [first1,last1)for the occurrence of sequence which is defined by [first2,last2). If the occurrence is found, an iterator to the first element is returned, otherwise the last1 is returned.

Syntax

template<class ForwardIterator1, classForwardIterator2>
ForwardIterator1 find_end(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);

template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end(ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);

Parameter

first1: It is a forward iterator to the first element in the range [first1, last1) where the element itself is included in the range.

last1: It is a forward iterator to the last element in the range [first1, last1) where the element itself is not included in the range.

first2: It is a forward iterator to the first element in the range [first2, last2) where the element itself is included in the range.

last2: It is a forward iterator to the last element in the range [first2, last2) where the element itself is not included in the range.

pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function.

Return value

The function returns an iterator to the first element of the last occurrence of [first2,last2)in the range [first1,last1).In case the sequence is not found then the function returns last1 value.

Example 1

#include <iostream>     
#include <algorithm>    
#include <vector>       
bool newfunction (int m, int n) 
{
  return (m==n);
}
int main () 
{
  int newints[] = {1,2,3,4,5,1,2,3,4,5};
  std::vector<int> haystack (newints,newints+10);
  int patt1[] = {1,2,3};
  std::vector<int>::iterator ti;
  ti = std::find_end (haystack.begin(), haystack.end(), patt1, patt1+3);
  if (ti!=haystack.end())
  std::cout << "patt1 last found at position " << (ti-haystack.begin()) << '\n';
  int patt2[] = {4,5,1};
  ti = std::find_end (haystack.begin(), haystack.end(), patt2, patt2+3, newfunction);
  if (ti!=haystack.end())
  std::cout << "patt2 last found at position " << (ti-haystack.begin()) << '\n';
  return 0;
}

Output:

patt1 is last found at the position 5
patt2 is last found at the position 3

Example 2

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	vector<int>u= {1,3,10,3,10,1,3,3,10,7,8,1,3,10};
	vector<int>u1={1,3,10};
	vector<int>::iterator ti;
	ti=std::find_end(u.begin(),u.end(),u1.begin(),u1.end());
	cout<<(ti-u.begin())<<"\n";
	return 0;
} 

Output:

11

Complexity

The complexity of the function is specified by count2*(1+count1-count2. Here countX specifies the distance between the firstX and lastX.

Data races

Objects in both ranges are accessed.

Exceptions

The function throws an exception if any of the argument throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf