Contents
Algorithms
STL provides implementation of common algorithms that we can use. They are template based.The below example shows how we can use the built in linear search and binary search.File: find1.cpp
#include <iostream> #include <algorithm> using namespace std ; int main() { vector<int> numbers = {10, 20, 30, 40, 50}; //Linear search example // Value to search for int searchValue1 = 30; int searchValue2 = 60; // Use find to search for searchValue1 vector<int>::iterator it1 = find(numbers.begin(), numbers.end(), searchValue1); // Check if the element was found if ( it1 != numbers.end()) { cout << "Element " << searchValue1 << " found at position: " << distance(numbers.begin(), it1) << endl; } else { cout << "Element " << searchValue1 << " not found." << endl; } it1 = find(numbers.begin(), numbers.end(), searchValue2 ); // Check if the element was found if ( it1 != numbers.end()) { cout << "Element " << searchValue2 << " found at position: " << distance(numbers.begin(), it1) << endl; } else { cout << "Element " << searchValue2 << " not found." << endl; } if (binary_search(numbers.begin(), numbers.end(), searchValue1)) { cout << "Binary search: " << searchValue1 << " found in the vector." << endl; } else { cout << "Binary search: " << searchValue1 << " not found in the vector." << endl; } return 0 ; }Exercise
The below program does not compile. Fix the code.
File: find_ex1.cpp
Functor
A functor is a class that has the overloaded operator"()" . It can be used as a function. Functors are used in STL algorithms that expect either a function or a Functor. A functor can retain state and because it is a class it can be passed as a template parameter.File: functor1.cpp
// C++ program to illustrate the begin and end iterator #include <iostream> #include <map> #include <string> using namespace std; class functor1 { public: void operator()( int& x1 ) { cout << "Inside operator() : " << x1 << endl ; x1++ ; } }; void function1( int& x1 ) { cout << "Inside function1() : " << x1 << endl ; x1++ ; } int main() { functor1 functor1Object ; int y1 = 10 ; functor1Object( y1 ) ; cout << "y1: " << y1 << endl ; function1( y1 ) ; cout << "y1: " << y1 << endl ; return 0; }
Sort
File: sort1.cpp
#include <iostream> // cout #include <algorithm> // sort #include <vector> // vector using namespace std; bool myfunction (int i,int j) { return (i<j); } class myclass { public: bool operator() (int i,int j) { return (i<j);} } ; int main () { int myints[] = {32,71,12,45,26,80,53,33}; vector<int> myvector (myints, myints+8); myclass myobject ; // using default comparison (operator <): sort (myvector.begin(), myvector.begin()+4); //sort the first 4 elements only //(12 32 45 71)26 80 53 33 cout << "myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; // using function as comp sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) cout << "myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; // using object as comp sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: cout << "myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; cout << "myobject:" << myobject( 12, 5 ) << endl ; return 0; } $ g++ sort1.cpp ; ./a.exe myvector contains: 12 32 45 71 26 80 53 33 myvector contains: 12 32 45 71 26 33 53 80 myvector contains: 12 26 32 33 45 53 71 80 myobject:0Complete the TODO parts in the following exercise. A student is less than another student if a student's id is less than the other student's id.
File: sort_ex1.cpp
#include <iostream> #include <vector> #include <algorithm> using namespace std ; class student { public: int id ; student( ) { id=0 ; } student( int idP ) { id = idP ; } student( const student& obj1 ) { id = obj1.id ; } ~student( ) { } }; bool studentCompareFunction (student obj1, student obj2 ) { //TO DO Complete the code } class studentCompareClass { public: bool ascend = true ; bool operator() (student obj1, student obj2 ) { //TO DO Complete the code //If ascend is true the sorted order is ascending //else it is descending. } ; int main() { student studentObject1(1) ; student studentObject2(2) ; student studentObject3(3) ; vector< student > vec ; vec.push_back( studentObject1 ) ; vec.push_back( studentObject3 ) ; vec.push_back( studentObject2 ) ; studentCompareClass studentCompareClassObj1 ; studentCompareClassObj1.ascend = false ; sort( vec.begin() , vec.end() , studentCompareClassObj1 ) ; for( student x1 : vec ) cout << x1.id << " " ; cout << endl ; vec.clear() ; vec.push_back( studentObject1 ) ; vec.push_back( studentObject3 ) ; vec.push_back( studentObject2 ) ; sort( vec.begin() , vec.end() , studentCompareFunction ) ; for( student x1 : vec ) cout << x1.id << " " ; cout << endl ; return 0 ; } $ g++ sort_ex1s.cpp ; ./a.exe 3 2 1 1 2 3The "for_each" function allows us to call a function for a range of elements in a container.
For Each
File: for1.cpp#include <iostream> #include <vector> #include <algorithm> using namespace std ; // A regular function to be used with for_each void print_element(int n) { cout << n << " "; } int main() { vector<int> numbers = {1, 2, 3, 4, 5}; // Using for_each with a function pointer for_each( numbers.begin(), numbers.end(), print_element ); cout << endl; return 0; }
Exercise
File: for_ex1.cpp#include <iostream> #include <vector> #include <algorithm> using namespace std ; // A regular function to be used with for_each //Change the below function to use templates void print_element(int n) { cout << n << " "; } int main() { vector<int> numbers = {1, 2, 3, 4, 5}; // Using for_each with a function pointer for_each(numbers.begin(), numbers.end(), print_element<int> ); cout << endl; vector<string> strings = { "One", "Two", "Three" }; for_each(strings.begin(), strings.end(), print_element<string> ); cout << endl; return 0; }Transform applies a function to a range of elements in a container and
Transform
File: transform1.cpp#include <algorithm> #include <vector> #include <iostream> using namespace std ; // Free function to double an integer int double_value(int i) { return i * 2; } int main() { vector<int> input = {1, 2, 3, 4, 5}; vector<int> output(input.size()); // Apply double_value to each element of input and store in output transform(input.begin(), input.end(), output.begin(), double_value); for (int val : output) { cout << val << " "; // Output: 2 4 6 8 10 } cout << endl; return 0; }
Exercise
1)File: algo_ex1.cpp
Solutions
File: vector_ex1_s.cpp
File: sort_ex1s.cpp
File: for_ex1s.cpp
File: algo_ex1_s.cpp