Created
November 9, 2018 09:17
-
-
Save Kishy-nivas/e997ef1b2b2db9fa7d3b9ce91a58f2a9 to your computer and use it in GitHub Desktop.
STL and templates quick reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
// function template examples | |
template <typename T> | |
void print_vector(const vector<T>& temp){ | |
cout<<"{"; | |
for(int i=0;i<temp.size();i++){ | |
cout<<temp[i]; | |
if(i!= temp.size()-1) | |
cout<<","; | |
} | |
cout<<"}"<<"\n"; | |
} | |
struct student{ | |
int id; | |
string name; | |
int rank; | |
/* | |
bool operator<(const student& other){ //overload the "<" operator to support built-in sort, but what if we need different flavours | |
return id < other.id; | |
} | |
*/ | |
/* | |
ostream& operator<<(ostream& os){ | |
os<<"ID: "<< id<<"\n"; | |
os<<"Name: "<<name<<"\n"; | |
os<<"Rank: "<<rank<<"\n"; | |
return os; | |
} | |
*/ | |
}; | |
bool cmp_by_rank(const student& a, const student& b){ | |
return a.rank < b.rank; | |
} | |
bool cmp_by_id(const student& a, const student& b){ | |
return a.id < b.id; | |
} | |
template<typename T> | |
void swap_v(T& a, T& b){ | |
T temp; | |
temp = a; | |
a= b; | |
b = temp; | |
} | |
template<typename T> | |
T triple(T value){ | |
return value + value + value; | |
} | |
template<typename T> | |
T abs_max(T a, T b){ | |
return abs(a) > abs(b) ? a : b; | |
} | |
template<typename T> | |
struct Mychoices{ | |
T a, b,c,d; | |
}; | |
template<typename T> | |
ostream& operator<<(ostream& os,const Mychoices<T>& t){ // overload how the object of type Mychoices, should be represented | |
os<<"A :"<< t.a<<"\n"; | |
os<<"B: "<<t.b<<"\n"; | |
os<<"C: "<<t.c<<"\n"; | |
os<<"D: "<<t.d<<"\n"; | |
return os; | |
} | |
ostream& operator<<(ostream& os, const student& s){ // same as above, but now for our student object | |
os<<"Name: "<<s.name<<"\n"; | |
os<<"ID: "<<s.id<<"\n"; | |
os<<"Rank: "<<s.rank<<"\n"; | |
return os; | |
} | |
int main(){ | |
//templates | |
Mychoices<int> choice1{1,2,3,4}; | |
cout<<choice1<<"\n"; | |
Mychoices<string> choice2{"megan fox", "kiera knighly" , "Gal gadot", "anne hathaway"}; | |
cout<<choice2<<"\n"; | |
vector<student> student_list; | |
student_list.push_back(student{4,"Priya ",1}); | |
student_list.push_back(student{5,"Kavitha",4}); | |
student_list.push_back(student{3,"kashyap",5}); | |
student_list.push_back(student{1,"kishore",2}); | |
student_list.push_back(student{2,"magesh",3}); | |
sort(student_list.begin(),student_list.end(),cmp_by_rank); // sort by rank | |
print_vector(student_list); | |
sort(student_list.begin(),student_list.end(),cmp_by_id); // sort by id | |
print_vector(student_list); | |
vector<int> arr = {1,2,3,4,5,6,7}; | |
arr.push_back(11); | |
arr.push_back(9); | |
arr.push_back(3); | |
arr.push_back(3); | |
arr.push_back(3); | |
cout<<"before sorting :"; | |
print_vector(arr); | |
cout<<"after sorting : "; | |
sort(arr.begin(),arr.end()); | |
print_vector(arr); | |
int search_val = 10; | |
if(binary_search(arr.begin(),arr.end(), search_val )){ //return 0, if not found | |
cout<<"The value " <<search_val<<" is present"<<"\n"; | |
} | |
else{ | |
cout<<"The value is not found"<<"\n"; | |
} | |
// upper bound and lower bound, the place where it can be inserted | |
cout<<"upper bound for 10 " << upper_bound(arr.begin(),arr.end(), 10)- arr.begin()<<"\n"; // subtract it with begin iterator to get the index spot | |
cout<<"lower bound for 3"<<lower_bound(arr.begin(),arr.end(),3)- arr.begin()<<"\n"; | |
// list - use it when you want to remove elements in constant time, | |
list<int> li = {4,5,6,7,6}; | |
li.remove(6); // removes all occurences of 6 | |
for(auto i : li) cout<<i<<"\n"; | |
//set- like an always sorted array, will not contain duplicates | |
set<int> s = {5,4,3,2,1}; //initializer | |
set<int>::iterator st; | |
cout<<"array sorted by set"<<"\n"; | |
for(st = s.begin(); st!= s.end();st++){ // similary we can just use auto to traverse | |
cout<<*st<<"\n"; | |
} | |
// for(auto i: s) cout<<i<<"\n"; | |
s.insert(-1); | |
cout<<"new smallest element: "<<*s.begin()<<"\n"; | |
s.insert(7); | |
cout<<"new largest element: "<<*s.end()<<"\n"; | |
s.insert(8); | |
cout<<*s.lower_bound(6)<<"\n"; // smallest key greater than the value | |
// map | |
map<string,int> mp; | |
vector<string> words ={"hello", "world","i" , "love" ,"apple","i" , "love","world", "i","love","kiera" }; | |
for(string i :words) | |
mp[i]++; | |
for(auto i : mp){ // it returns a pair | |
cout<<i.first<<" "<<i.second<<"\n"; | |
} | |
// find most repeated elements | |
int max_count= INT_MIN; | |
for(auto i: mp){ | |
max_count = max(max_count, i.second); | |
} | |
vector<string> most_repeated; | |
for(auto i : mp){ | |
if(i.second == max_count){ | |
most_repeated.push_back(i.first); | |
} | |
} | |
print_vector(most_repeated); | |
int a{triple(3)}; | |
cout<<a<<"\n"; | |
string name{triple(string{"abc"})}; | |
cout<<name<<"\n"; | |
cout<<abs_max(5,4)<<"\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xzvzx