#include <algorithm>
#include <vector>
vector<int> vec;
//can have other data types instead of int but must same datatype as item
std::find(vec.begin(), vec.end(), item) != vec.end()
auto it = find(vec.begin(),vec,end(), item)!
if(it != vec.end()){
int index = it - vec.begin();
}
#include <vector> // vector
#include <algorithm> // find
#include <iostream> // cout
using namespace std;
int main()
{
vector<int> nums = {1,2,3,4,5,6,7,8,9};
bool isSorted = is_sorted(nums.begin(), nums.end());
if(isSorted){
cout << "Using binary search: " << endl;
if(binary_search(nums.begin(), nums.end(), 9))
cout << "found it" << endl;
else
cout << "not here" << endl;
}
else{
cout << "Using std::find";
if(std::find(nums.begin(), nums.end(), 9) != nums.end())
cout << "found it" << endl;
else
cout << "not here" << endl;
}
}
it = find (vec.begin(), vec.end(), ser);
if (it != vec.end())
{
cout << "Element " << ser <<" found at position : " ;
cout << it - vec.begin() << " (counting from zero)
" ;
}
else{
cout << "Element not found.
";
}
Code Example |
---|
Cpp :: iterate through list c++ |
Cpp :: classes constructor in c++ |
Cpp :: stack implementation through linked list |
Cpp :: c++ squaroot |
Cpp :: how to make Dijkstra in c++ |
Cpp :: creare array con c++ |
Cpp :: struct c++ |
Cpp :: convert kelvin to Fahrenheit |
Cpp :: C++ New Lines |
Cpp :: quick sort |
Cpp :: c++ doubly linked list |
Cpp :: how to initialize 2d array with values c++ |
Cpp :: pointer cpp |
Cpp :: c++ syntax |
Cpp :: cpp #include "" < |
Cpp :: c++ average vector |
Cpp :: template c++ |
Cpp :: lambda function in c++ |
Cpp :: exponent of x using c c++ |
Cpp :: cknuth hash |
Cpp :: c++ fill two dimensional array |
Cpp :: use of strstr in c++ |
Cpp :: c++ count vector elements |
Cpp :: converting char to integer c++ |
Cpp :: remove something from stringstream |
Cpp :: Character cin(userInput) in c++ |
Cpp :: maximum subarray leetcode c++ |
Cpp :: executing an opencv c++ code |
Cpp :: new in c++ |
Cpp :: c++ copy constructor |