Contents
Set
A set is a collection of unique elements. Searching, insertion and deletion are efficient operations on a set. We have the normal set that is ordered and also the "unordered_set" whose elements are not sorted.File: set1.cpp
#include <iostream> #include <set> using namespace std ; int main() { set<char> set1 ; set1.insert('G'); set1.insert('F'); set1.insert('G'); for (char ch1 : set1) { cout << ch1 << ' '; } cout << '\n'; if ( set1.count( 'G' ) > 0 ) cout << "G found in set. " << endl ; //another way if (set1.find('G') != set1.end() ) cout << "G found in set. " << endl ; set1.erase( 'G' ) ; if (set1.find('G') == set1.end() ) cout << "G not found in set. " << endl ; return 0; } $ g++ set1.cpp ; ./a.exe F G G found in set. G found in set. G not found in set.A set is ordered so if we are inserting custom class objects then we need to make sure that the operator "<" is implemented. STL will determine if a set equals a custom object by the rule that for 2 objects ( obj1 and obj2 ) the objects are equal if both obj1 < obj2 and obj2 < obj1 are false.
File: set2.cpp
#include <iostream> #include <set> using namespace std ; class student { public: int id ; student( int idP ) { id = idP ; } bool operator <(const student& obj1 ) const { return id < obj1.id ; } }; int main() { set< student > set1 ; student obj1(1) ; student obj2(2) ; student obj3(1) ; set1.insert( obj1 ) ; set1.insert( obj2 ) ; set1.insert( obj3 ) ; for (student st1 : set1 ) { std::cout << st1.id << ' '; } std::cout << '\n'; return 0; }
File: set4.cpp
#include <iostream> #include <set> #include <vector> #include <list> #include <chrono> #include <thread> #include <algorithm> using namespace std ; class Timer { public: chrono::steady_clock::time_point start_time ; chrono::steady_clock::time_point end_time ; void startTime() { start_time = chrono::steady_clock::now() ; } void endTime() { end_time = chrono::steady_clock::now() ; } void printTime() { chrono::milliseconds elapsed_ms = chrono::duration_cast<chrono::milliseconds>(end_time - start_time); cout << "Execution time: " << elapsed_ms.count() << " milliseconds" << endl; } }; //-------------------------------------------------------- //Assume vector can only contain positive numbers void removeDuplicates1( vector<int>& v1 ) { set<int> set1 ; int left = 0 ; for( int x1 : v1 ) { //not seen before if ( set1.count( x1 ) == 0 ) { v1[left++] = x1 ; } else { } set1.insert( x1 ) ; } //for v1.resize( left ) ; cout << "v1:size: " << v1.size() << endl ; } void removeDuplicates2( vector<int>& v1 ) { int left = 1 ; // cout << "v1.size():" << v1.size() << endl ; for( int i1=1 ; i1 < v1.size() ; i1++ ) { //not seen before //cout << "i1: " << i1 << " " << "v1[i1]:" << v1[i1] << endl ; vector<int>::iterator it = v1.begin() ; vector<int>::iterator it1 = v1.begin() + i1 ; int value = v1[i1] ; vector<int>::iterator it2 = find( it , it1, value ) ; //Did not find it in the left hand side of the //current position //cout << "*it2:" << *it2 << endl ; if ( it2 == it1 ) { // cout << "Did not find." << endl << endl ; v1[left++] = v1[i1] ; } } //for v1.resize( left ) ; cout << "v1:size: " << v1.size() << endl ; } int main() { //Assume vector can only contain positive numbers vector<int> v1 ; int noElements = 100000 ; //int noElements = 2 ; Timer timerObject1 ; for( int i1=1 ; i1<=noElements ; i1++ ) { v1.push_back( i1 ) ; v1.push_back( i1 ) ; } //for timerObject1.startTime() ; removeDuplicates1( v1 ) ; timerObject1.endTime() ; timerObject1.printTime() ; v1.clear() ; for( int i1=1 ; i1<=noElements ; i1++ ) { v1.push_back( i1 ) ; v1.push_back( i1 ) ; } //for timerObject1.startTime() ; removeDuplicates2( v1 ) ; timerObject1.endTime() ; timerObject1.printTime() ; //----------------------------------------------- v1.clear() ; for( int i1=1 ; i1<=noElements ; i1++ ) { v1.push_back( i1 ) ; } //for for( int i1=1 ; i1<=noElements ; i1++ ) { v1.push_back( i1 ) ; } //for timerObject1.startTime() ; removeDuplicates1( v1 ) ; timerObject1.endTime() ; timerObject1.printTime() ; v1.clear() ; for( int i1=1 ; i1<=noElements ; i1++ ) { v1.push_back( i1 ) ; } //for for( int i1=1 ; i1<=noElements ; i1++ ) { v1.push_back( i1 ) ; } //for timerObject1.startTime() ; removeDuplicates2( v1 ) ; timerObject1.endTime() ; timerObject1.printTime() ; return 0; } $ g++ set4.cpp ; ./a.exe v1:size: 100000 Execution time: 63 milliseconds v1:size: 100000 Execution time: 44202 milliseconds v1:size: 100000 Execution time: 61 milliseconds v1:size: 100000 Execution time: 29377 milliseconds
Exercise
1)File: set_ex1.cpp
#include <iostream> #include <set> #include <vector> using namespace std ; int main() { vector<int> v1 ; set<int> set1 ; v1.push_back( 4 ) ; v1.push_back( 2 ) ; v1.push_back( 4 ) ; v1.push_back( 1 ) ; v1.push_back( 2 ) ; for( int x1 : v1 ) cout << x1 << " " ; cout << endl ; //Remove duplicates in v1 . Keep the order the same. //Do not use any other data structures like vector // other than v1 and set1 defined above. //Do not use nested loops. Only 1 loop for( int x1 : v1 ) cout << x1 << " " ; cout << endl ; return 0; }
File: set_ex2.cpp
#include <iostream> #include <set> #include <vector> using namespace std ; /* Use a single loop to determine if any 2 elements in the vector equal a sum value */ //Assume vector can only contain positive numbers bool doesSumExistFor2Elements( vector<int>& v1 , int sum ) { return false ; } int main() { //Assume vector can only contain positive numbers vector<int> v1 = { 3, 1, 5, 7 , 4 , 8 }; cout << doesSumExistFor2Elements( v1, 10 ) << endl ; cout << doesSumExistFor2Elements( v1, 19 ) << endl ; vector<int> v2 = { 3, 1, 5, 7 , 4 , 2 }; cout << doesSumExistFor2Elements( v2, 3 ) << endl ; cout << doesSumExistFor2Elements( v2, 12 ) << endl ; return 0; }
File: set_ex1s.cpp
File: set_ex2s.cpp