STL Data Structures
STL Data Structures
🞇 p=make_pair(5,6);
🞇 p={5,6}
🞇 a=p.first;
🞇 b=p.second;
vector
🞇 vector<int> v; //empty vector
🞇 vector<int> v(10); // 10 elements with 0 val.
🞇 vector<int> v(10,1); // elements with 1 val.
🞇 v.push_back(element); //add element at back
of vector
🞇 K=v[i]; // access Ith element in vector
🞇 v.size() ;
🞇 v.resize(n); // resize vector to n element.
🞇 v.clear(); // remove all elements.
Iterate through elements of
vector
🞇 for(auto x:v)
🞇 cout<<x;
Functions
🞇 sort(v.begin(), v.end()); //ascending
🞇 sort(v.rbegin(), v.rend()); //descending
🞇 reverse(v.begin(), v.end());
🞇 int mn=*min_element(v.begin(),
v.end());
🞇 int mn=*max_element(v.begin(),
v.end());
Two dimensional vector
🞇 vector< vector<int> > Matrix;
ordered_set<int> os;
os.insert(6);
🞇 m[“8-A”]=23;
🞇 m.count(“8-B”); // return 0 if key is not in map
🞇 for(auto x:m)
🞇 {
🞇 cout<<x.first<<" "<<x.second<<endl;
🞇 }
🞇 m.erase(“8-A”); // remove from map
Vector of pair
🞇 vector < pair<int, int> > v;
🞇 v.push_back( {5,6} );
🞇 v.push_back( {1,2} );
🞇 sort(v.begin(), v.end());
Custom sort pair
🞇 Bool customsort(const pair<int,int>& a,
const pair<int, int>& b){
🞇 t=make_tuple(5,7,3);
🞇 a=get<0>(t); // a=5
🞇 b=get<1>(t); // b=7;
🞇 c=get<2>(t); // c=3;
🞇 v.push_back( make_tuple(5,6,7));
🞇 v.push_back( make_tuple(1,3,2) );
🞇 sort(v.begin(), v.end());
Custom sort tuple
🞇 Bool customsort(const tuple<int,int,int>& a,
const tuple<int, int,int>& b){