#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()
#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 :: string iterator in c++ |
Cpp :: decltype in c++ |
Cpp :: header file for unordered_map in c++ |
Cpp :: memcpy library cpp |
Cpp :: c++ get char of string |
Cpp :: iterate over 2 vectors c++ |
Cpp :: how to get the first element of a map in c++ |
Cpp :: c++ vector extend vector |
Cpp :: int to hex arduino |
Cpp :: c++ clear char array |
Cpp :: update variable in const function C++ |
Cpp :: matrix in vector c++ |
Cpp :: sort index c++ |
Cpp :: clear qlayout |
Cpp :: c++ pause linux |
Cpp :: for loop c++ |
Cpp :: char size length c++ |
Cpp :: c++ initialize vector of vector with size |
Cpp :: How to create files in C++ |
Cpp :: c++ ternary operator |
Cpp :: unique_ptr syntax |
Cpp :: last character of std::string |
Cpp :: new c++ |
Cpp :: C++, for-loop over an array array |
Cpp :: c++ looping through a vector |
Cpp :: c++ thread |
Cpp :: input full line as input in cpp |
Cpp :: c++ range based for loop |
Cpp :: c++ average vector |
Cpp :: prime or not in cpp |