Professional Documents
Culture Documents
STL Container: Array Passing Array To Function Vector Deque (Doubly Ended Queue) List Stack Queue Priority Queue Set Map
STL Container: Array Passing Array To Function Vector Deque (Doubly Ended Queue) List Stack Queue Priority Queue Set Map
STL Container: Array Passing Array To Function Vector Deque (Doubly Ended Queue) List Stack Queue Priority Queue Set Map
md 2/12/2022
STL Container
1. Array
2. Passing Array to function
3. Vector
4. Deque (Doubly ended queue)
5. List
6. Stack
7. Queue
8. Priority Queue
9. Set
10. Map
1/9
STL Container.md 2/12/2022
Container : https://en.cppreference.com/w/cpp/container
Algorithms : https://en.cppreference.com/w/cpp/algorithm
1. Array
#include <array>
int main(){
array<int,4> a = {1,2,3,4};
// Static Array
int size = a.size();
2/9
STL Container.md 2/12/2022
// Better Way
//O(1)
a.at(2) // what is at 2nd position?
a.empty() // Is the array empty?
a.front() // Gives the first element.
a.back() // Gives the last element
In STL everything is a copy is the the actual object, everything is passed by reference
3/9
STL Container.md 2/12/2022
Updating STL Container Objects. All stl containers are passed by values.
When passsing an array in function a copy is created, we need to use pass by referance
If need to create a print method, as we are pssing the whole object we can use all the function of the object.
3. Vector
So Vector is a dynamic array, create a double size vector when vector is filled. Ability to resize when an
element is inserted.
#include <vector>
int main() {
vector<int> v;
vector<int> a(5,1) // (size,value)
// different method
for (int i:a){
do what ever needed.;
}
// Copy v to a
vector<int> last(a);
// This is "last" vector which has all the element of a.
}
4/9
STL Container.md 2/12/2022
This doubling is a expensive opeartion -- Spending linear time to doube the size
so reserve :
#include <deque>
int main(){
// Ways to initialize a deque.
deque<int> d;
deque<int> second (4,100);
deque<int> third (second.begin(),second.end());
// Removing Elements
d.pop_back(); // pop back
d.pop_front(); // pop front
d.size();
d.font();
d.back();
d.empty();
d.begin();
5. List
Used as Doubly Linked List, here we have two pointers, one for front and one for back.
#include <list>
int main(){
list<int> l;
l.push_back(1);
l.push_front(2);
6. Stack
Keep books on a table, the last one put is picked up first.
#include <stack>
int main(){
stack<string> s;
s.push("Durgance");
s.push("Gaur");
s.push("Work");
6/9
STL Container.md 2/12/2022
7. Queue
FIFO : (First In First Out)
#include <queue>
int main(){
queue<string> q;
q.push("Durgance");
q.push("Gaur");
q.push("Work");
q.front(); // Durgance
q.pop(); //Removes Durgance
q.size();
8. Priority Queue
Like .. max heep A Data Structure in which we have two ways to take out data. This is a heap Data Structure
#include <queue>
int main(){
// This is maxheep
priority_queue<int>maxi;
//min - heep
priority_queue<int,vector<int>,
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>
q;
maxi.push(1);
maxi.push(4);
maxi.push(2);
7/9
STL Container.md 2/12/2022
maxi.push(0);
int n = maxi.size();
class Compare{
public:
bool operator()(int a, int b){
return a > b;
}
};
int main(){
priority_queue<int,vector<int>,Compare> heap;
}
9. Set
All unique elements Can not modify inserted element, only deletion avilable, return in sorted order.
#include<set>
int main(){
set<int> s;
s.insert(5);
s.insert(3);
s.insert(6);
s.insert(0); //O(log(n))
for(auto i :s){
count<<i<<" ";
}
//output : 0 3 6 5
8/9
STL Container.md 2/12/2022
set<int>::iterator it = s.begin();
it++;
s.erase(it);
// deletes 3
10. Map
Stores in the form of Key Value. This has complexity in search C(logn) In ordered map. In unordered map, is
made using hashmaps, there time complexity is O(1)
#include <map>
int main(){
map<int,string> m;
m[1]="Durgance";
m[13]="PG";
m[2]="Gaur";
m.insert({5,"bheem"})
for(auto i :m){
cout<<i.first<<":"<<i.second;
}// Output : 1 : Durgance 2 13
m.erase(13); // Erases
// Returns iterator
auto it = m.find(5)
for (auto i = it;i!=m.end();i++){
cout<<(*i).first<<endl;
}
// used for Getting the last key of the map, Good when having sorted map;
prev(m.end())->first;
}
9/9