Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

STL Data Structures

The document provides a comprehensive overview of various data structures in C++, including pairs, vectors, stacks, queues, deques, priority queues, sets, maps, and tuples. It includes syntax for creating, manipulating, and sorting these structures, as well as examples of custom sorting functions. Additionally, it covers advanced topics like ordered sets and vectors of pairs and tuples.

Uploaded by

khaydarov.anvar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

STL Data Structures

The document provides a comprehensive overview of various data structures in C++, including pairs, vectors, stacks, queues, deques, priority queues, sets, maps, and tuples. It includes syntax for creating, manipulating, and sorting these structures, as well as examples of custom sorting functions. Additionally, it covers advanced topics like ordered sets and vectors of pairs and tuples.

Uploaded by

khaydarov.anvar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

pair

🞇 pair <int, int> p;

🞇 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;

🞇 vector< vector<int> > Matrix(N,


vector<int> (M, -1)) // N*M metrix with
1 val.
stack
🞇 stack<int> st;
🞇 st.push(element);
🞇 k=st.top(); // returns a last element
🞇 st.pop(); //delete last element
🞇 st.empty(); // True or False
🞇 St.size(); // returns size
queue
🞇 queue<int> q;
🞇 q.push(element);
🞇 k=q.front(); // returns first element
🞇 q.pop(); //delete first element
🞇 q.empty(); // True or False
🞇 q.size(); // returns size
deque
🞇 deque<int> dq;
🞇 dq.push_front(element);
🞇 dq.push_back(element);
🞇 k=dq.front(); // returns first element
🞇 K=dq.back(); //return last element
🞇 dq.pop_front(); //delete first element
🞇 dq.pop_back();
🞇 dq.empty(); // True or False
🞇 dq.size(); // returns size
🞇 dq.clear();
priority_queue
🞇 queue<int> q;
🞇 q.push(element);
🞇 k=q.top(); // returns first element
🞇 q.pop(); //delete first element
🞇 q.empty(); // True or False
🞇 q.size(); // returns size
set
🞇 set<int> s;
🞇 s.insert(element); //add element to set
🞇 cout<<s.count(element); // 1 if exists,
else 0
🞇 s.erase(element); // remove element from
set
ordered_set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T,
null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;

ordered_set<int> os;
os.insert(6);

🞇 order_of_key (k) : Number of items strictly smaller than k .


🞇 find_by_order(k) : K-th element in a set (counting from
zero).
map
🞇 map<string, int> m;

🞇 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){

if (a.first > b. first) return true;


if( a.first < b.first ) return false;
return a.second>b.second;
}
Sort(v.begin(), v.end(), customsort);
Tuple
🞇 tuple<int,int,int> t;

🞇 t=make_tuple(5,7,3);
🞇 a=get<0>(t); // a=5
🞇 b=get<1>(t); // b=7;
🞇 c=get<2>(t); // c=3;

🞇 tie(a,b,c) = t; // a=5, b=7, c=3


Vector of tuple
🞇 vector < tuple<int, int,int> > v;

🞇 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){

if (get<0>(a) < get<0>(b)) return true;


if(get<0>(a) > get<0>(b) ) return false;
return get<1>(a)> get<1>(b)
}
Sort(v.begin(), v.end(), customsort);

You might also like