Home >>Cpp Programs >C++ Find

C++ Find

C++ Find

This is the function in C++ that is basically used to find the element that are in the given range of numbers. C++ find also used to return an iterator to the first element in the range [first,last] that basically compares the equal to val. In case there is no such element is found then the function returns the last.

C++ search generally returns an iterator back to the first element in the range [first,last] that basically compares equal to val. Last is returned by the function in case there is no such element is found.

The CPP find function basically uses the operator== in order to compare the individual elements to val.

Here is an example that will explain about the C++ find and the working of it, most important of all, it will let you understand the application aspect of it:

#include<bits/stdc++.h> 
int main () 
{ 
	std::vector<int> vect { 10, 11, 12, 13 }; 
	
	//store the position using iterator of searches element 
	std::vector<int>::iterator it; 
	
	// Original Vector print 
	std::cout << "Original vector :"; 
	for (int i=0; i<vect.size(); i++) 
		std::cout << " " << vect[i]; 
		
	std::cout << "\n"; 
	
	//Need  to searche element 12 
	int search = 12; 
	
	it = std::find (vect.begin(), vect.end(), search); 
	if (it != vect.end()) 
	{ 
	std::cout << "Element " << search <<" found at position : " ; 
	std:: cout << it - vect.begin() + 1 << "\n" ; 
	} 
	else
	{
		std::cout << "Element not found.\n\n"; 
	}	
	return 0; 
}
Output :
Original vector : 10 11 12 13
Element 12 found at position : 3

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++