//search for x find(vect.begin(), vect.end(),x); //note // find() returns iterator to last address if element not present find(vect.begin(), vect.end(),5) != vect.end()? cout << "\nElement found": cout << "\nElement not found"; --------------------------------------------------------------------------------------------------- // for binary search in containers like vector (let target element=6) binary_search(v.begin(), v.end(), 6); // return 1 or 0 as present or not --------------------------------------------------------------------------------------------------- //binary search with lower and upper bound int x, ans; //x is passing value, ans is a value you want, v --> vector or any container ans = lower_bound(v.begin(), v.end(), x) - v.begin(); //>= x (equal number or first number after x) ans = upper_bound(v.begin(), v.end(), x) - v.begin(); // > x (first number after x) --------------------------------------------------------------------------------------------------- //Example on Binary Search() , lower_bound() and upper_bound #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // Initializing vector with array values int arr[] = {5, 10, 15, 20, 20, 23, 42, 45}; int n = sizeof(arr)/sizeof(arr[0]); vector<int> vect(arr, arr+n); // Sort the array to make sure that lower_bound() // and upper_bound() work. sort(vect.begin(), vect.end()); // Returns the first occurrence of 20 auto q = lower_bound(vect.begin(), vect.end(), 20); //3 // Returns the last occurrence of 20 auto p = upper_bound(vect.begin(), vect.end(), 20); //5 //The lower bound is at index cout << q-vect.begin() << endl; // O/P : 3 // upper bound is at index cout << p-vect.begin() << endl; // O/P : 5 return 0; } -------------------------------------------------------------------------------------------------- // swap two numbers //Time Complexity: O(1) swap(a,b); --------------------------------------------------------------------------------------------------- // rotate containers like vector, strings by n position rotate(v.begin(), v.begin()+n, v.end()); --------------------------------------------------------------------------------------------------- // sort arrays of size n sort(arr, arr+n); --------------------------------------------------------------------------------------------------- // sort containers like vector, strings(based on intro sort) sort(v.begin(), v.end()); --------------------------------------------------------------------------------------------------- // reverse containers like vectors, strings reverse(v.begin(), v.end()); --------------------------------------------------------------------------------------------------- //find the maximum element of a vector. *max_element(v.begin(), v.end()); --------------------------------------------------------------------------------------------------- //find the minimum element of a vector. *min_element(v.begin(), v.end()); --------------------------------------------------------------------------------------------------- //Does the summation of vector elements accumulate(vect.begin(), vect.end(), 0); --------------------------------------------------------------------------------------------------- //Example on sort(), reverse(), *max_element(), *min_element() and accumulate() #include <algorithm> #include <iostream> #include <vector> #include <numeric> //For accumulate operation using namespace std; int main() { // Initializing vector with array values int arr[] = {10, 20, 5, 23 ,42 , 15}; int n = sizeof(arr)/sizeof(arr[0]); vector<int> vect(arr, arr+n); //print Vector for (int i=0; i<n; i++) cout << vect[i] << " "; // O/P: 10 20 5 23 42 15 // Sorting the Vector in Ascending order sort(vect.begin(), vect.end()); // Print Vector after sorting for (int i=0; i<n; i++) cout << vect[i] << " "; // O/P: 10 20 5 23 42 15 // Reversing the Vector reverse(vect.begin(), vect.end()); // Print Vector after reversing for (int i=0; i<n; i++) cout << vect[i] << " "; // O/P : 42 23 20 15 10 5 //Maximum element of vector cout << *max_element(vect.begin(), vect.end()); //42 //Minimum element of vector cout << *min_element(vect.begin(), vect.end()); //5 // Starting the summation from 0 //The summation of vector elements cout << accumulate(vect.begin(), vect.end(), 0); //115 return 0; } --------------------------------------------------------------------------------------------------- // Count from first to end count (begin(v),end(v),true); // true or any boolean --------------------------------------------------------------------------------------------------- // Remove all elments v.clear(); --------------------------------------------------------------------------------------------------- //erase() in vector //Time Complexity: O(N) //erases selected element in vector and shifts and resizes the vector elements accordingly. v.erase(); // Example 1: working of erase() function #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector{ 1, 2, 3, 4, 5 }; vector<int>::iterator it; it = myvector.begin(); myvector.erase(it); // Printing the Vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; // O/P : 2 3 4 5 return 0; } ----------------------------------------- //Example 2: removing elements within a range #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector{ 1, 2, 3, 4, 5 }; vector<int>::iterator it1, it2; it1 = myvector.begin(); it2 = myvector.end(); it2--; it2--; myvector.erase(it1, it2); // Printing the Vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; //4 5 return 0; } ----------------------------------------- // Example 3: remove all the even elements from the vector and print the vector. #include <iostream> #include <vector> using namespace std; int main() { vector<int> myvector{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (auto i = myvector.begin(); i != myvector.end(); ++i) { if (*i % 2 == 0) { myvector.erase(i); i--; } } // Printing the vector for (auto it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it; // O/P : 1 3 5 7 9 return 0; } --------------------------------------------------------------------------------------------------- // erase in map #include<iostream> #include<map> // for map operations using namespace std; int main() { // declaring map // of char and int map< char, int > mp; // declaring iterators map<char, int>::iterator it ; map<char, int>::iterator it1; map<char, int>::iterator it2; // inserting values mp['a']=5; mp['b']=10; mp['c']=15; mp['d']=20; mp['e']=30; it = mp.begin(); // erasing element using iterator // erases 2nd element // 'b' ++it; mp.erase(it); // printing map elements after deletion for (it1 = mp.begin(); it1!=mp.end(); ++it1) cout << "(" << it1->first << " : " << it1->second <<") "; // O/P : (a : 5) (c : 15) (d : 20) (e : 30) // erasing element using value int c = mp.erase('c'); // printing map elements after deletion for (it1 = mp.begin(); it1!=mp.end(); ++it1) cout << it1->first << "->" << it1->second << endl; // O/P : (a : 5) (d : 20) (e : 30) //The number of elements deleted in 2nd deletion are cout << c << endl; // O/P : 1 // erasing element using not present value int d = mp.erase('w'); // printing map elements after deletion for (it1 = mp.begin(); it1!=mp.end(); ++it1) cout << it1->first << "->" << it1->second << endl; // O/P : (a : 5) (d : 20) (e : 30) //The number of elements deleted in 3rd deletion are cout << d << endl; // O/P : 0 ++it; ++it; // erasing element using range iterator // deletes "d" and "e" keys mp.erase(it, mp.end()); // printing map elements 4th deletion for (it1 = mp.begin(); it1!=mp.end(); ++it1) cout << it1->first << "->" << it1->second << endl; // O/P : (a : 5) cout << endl; } --------------------------------------------------------------------------------------------------- //unique //erase (uniqe()) //next_permutation() // prev_permutation() // distance()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter