Last active
July 2, 2020 19:22
-
-
Save thepurpleowl/e7c3b2aa9a2242656be8524b1f074685 to your computer and use it in GitHub Desktop.
STL hands-on
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 <iostream> | |
#include <map> | |
#include <utility> | |
using namespace std; | |
/* | |
Internally map and set are almost always stored as red-black trees. We do not need to worry about the internal structure, | |
the thing to remember is that the elements of map and set are always sorted in ascending order | |
while traversing these containers. And that’s why it’s strongly not recommended to change the key value while traversing map or set: If you make the modification that breaks the order, it will lead to improper functionality of container’s algorithms, at least. | |
*/ | |
#define traverse(container, it)\ | |
for(typeof(container.begin()) it = container.begin();it!=container.end();it++) cout<<it->first<<":"<<it->second<<" ,";\ | |
cout<<endl; | |
#define traverse2(container, it)\ | |
for(auto it : container) cout<<it.first<<":"<<it.second<<" ,";\ | |
cout<<endl; | |
void f(const map< string, int >& M) { | |
// if(M["the meaning"] == 42) { // Error! Cannot use [] on const map objects! | |
// } | |
if(M.find("the meaning") != M.end() && M.find("the meaning")->second == 42) { // Correct | |
cout << "Don’t Panic!" << endl; | |
} | |
} | |
int main() { | |
map< string, int >M; | |
M["Top"] = 1; | |
M["Coder"] = 2; | |
M["SRM"] = 10; | |
traverse2(M,it); | |
int x = M["Top"] + M["Coder"]; | |
cout<<"sum : " <<x<<endl; | |
if(M.find("SRM") != M.end()) { | |
M.erase(M.find("SRM")); // or even M.erase("SRM") | |
} | |
cout<<M.at("Top")<<endl; | |
M.insert(make_pair("Codeforse",99)); | |
M["Top"] = 100; | |
traverse(M, it); | |
return 0; | |
} |
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 <iostream> | |
#include <vector> | |
#include <utility> | |
#include <string> | |
using namespace std; | |
vector<pair<string, pair<string, int> > > directory; | |
int main() { | |
int m=3; | |
for(int i=1;i<=m;i++){ | |
pair<string, int > ptemp = make_pair("D1",i); | |
directory.push_back(make_pair("E"+to_string(i),ptemp)); | |
} | |
vector<pair<string, pair<string, int> > >::iterator it; | |
/* | |
STL algorithms always use two iterators, called “begin” and “end.” | |
The end iterator is pointing not to the last object, | |
however, but to the first invalid object, or the object directly following the last one. | |
*/ | |
for(it=directory.begin();it!=directory.end();it++){ | |
pair<string, pair<string, int> > temp = *it; | |
cout<<(*it).first<< " works in "<<temp.second.first<<" with tag "<<temp.second.second<<endl; | |
} | |
return 0; | |
} |
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 <iostream> | |
#include <set> | |
using namespace std; | |
#define traverse(container, it)\ | |
for(typeof(container.begin()) it = container.begin();it!=container.end();it++) cout<<*it<<" ";\ | |
cout<<endl; | |
int main() { | |
set< int > seta; | |
/* | |
int data[5] = { 5, 1, 4, 2, 3 }; | |
set< int > S(data, data+5); | |
It gives us a simple way to get rid of duplicates in vector, and sort it: | |
vector< int > v; | |
set< int > s(all(v)); | |
vector< int > v2(all(s)) | |
*/ | |
int r = 0; | |
for(int i = 1; i <= 100; i++) { | |
seta.insert(i); // Insert 100 elements, [1…100] | |
} | |
seta.insert(42); // does nothing, 42 already exists in set | |
for(int i = 2; i <= 100; i += 2) { | |
seta.erase(i); // Erase even values | |
} | |
int n = int(seta.size()); | |
cout<<"size : "<<n<<endl; | |
cout<<"upper_bound of 55 : "<<*seta.upper_bound(55)<<"and lower_bound in set "<<*seta.lower_bound(55)<<endl; | |
traverse(seta,it); | |
cout<<"Last element : "<<*seta.end()<<endl; | |
if(seta.find(42) != seta.end()) { | |
// 42 presents in set | |
cout<<"Count of 42 : "<<seta.count(42); | |
} | |
else { | |
// 42 not presents in set | |
cout<<"Count of 42 : "<<seta.count(42); | |
} | |
return 0; | |
} |
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 <iostream> | |
#include <vector> | |
using namespace std; | |
void transpose_mat(vector<vector <int> > &mat){ | |
int row = mat.size(); | |
int column = mat[0].size(); | |
for(int i=0;i<row;i++){ | |
for(int j=0;j<column;j++){ | |
mat[j][i]=mat[i][j]; | |
} | |
} | |
} | |
void print_mat(vector < vector<int> >& mat, int m, int n){ | |
for(int i=0;i<m;i++){ | |
for(int j=0;j<n;j++){ | |
cout<<mat[i][j]<<" "; | |
} | |
cout<<endl; | |
} | |
} | |
int main() { | |
int m=3, n=3; | |
vector< vector<int> > mat(m, vector<int> (n,0)); | |
for(int i=0;i<m;i++){ | |
for(int j=0;j<n;j++){ | |
mat[i][j] = 1+i+j; | |
} | |
} | |
//pass by direct reference | |
//no need of dereferencing in called function | |
print_mat(mat,m,n); | |
return 0; | |
} |
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 <iostream> | |
#include <vector> | |
using namespace std; | |
#define traverse(container, it)\ | |
cout<<endl;\ | |
for(typeof(container.begin()) it = container.begin();it!=container.end();it++) cout<<*it<<" ";\ | |
cout<<endl; | |
int main() { | |
vector<int> arr ={1,2,3,4,5}; | |
vector<int> arr2 ={11,22,33,44,55}; | |
arr.swap(arr2); | |
cout<<"After swapping"<<endl; | |
/*Traverse using iterator*/ | |
for(vector<int>::iterator it= arr.begin();it!=arr.end();it++){ | |
cout<<*it<<" "; | |
} | |
cout<<endl; | |
/*Traverse using iterator, definition using typeof()*/ | |
for(typeof(arr2.begin()) it= arr2.begin();it!=arr2.end();it++){ | |
cout<<*it<<" "; | |
} | |
cout<<endl; | |
/*reverse vector*/ | |
int index = arr.size()-1; | |
for(vector<int>::reverse_iterator it=arr.rbegin();it!=arr.rbegin()+arr.size()/2;it++){ | |
// cout<<*it<<" "; | |
// cout<<*(arr.rend()-index); | |
// cout<<*(arr.rend()-1)<<endl; | |
std::swap(*it, *(arr.rbegin()+index)); | |
index--; | |
} | |
/*after reverse, traverse using reverse_iterator => output similar to traversing w/o reversing*/ | |
for(vector<int>::reverse_iterator it=arr.rbegin();it!=arr.rend();it++){ | |
cout<<*it<<" "; | |
} | |
traverse(arr, it); | |
cout<<"insert() and erase()"; | |
vector<int>:: iterator it = arr.begin(); | |
arr.insert(it+1,39); | |
arr.erase(arr.begin()+2); | |
traverse(arr,it); | |
arr.erase(arr.begin()+2,arr.begin()+3); | |
traverse(arr,it); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment