STL Container: Array Passing Array To Function Vector Deque (Doubly Ended Queue) List Stack Queue Priority Queue Set Map

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

STL Container.

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

MultiMap is a map which can contain dublicate Keys

1. Array

#include <array>
int main(){
array<int,4> a = {1,2,3,4};
// Static Array
int size = a.size();

for(int i = 0; i < size;i++){


cout<<a[i]<<endl;
}
printf("Welcome");
return 0;
}

2/9
STL Container.md 2/12/2022

Some Methods of array

// 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

a.fill(54) // fills the array with 54

2. Passing Array to function

// Array is passed by Referance,


//As this array name stores the starting pointer of the array.
void updateArray(int arr[],int i , int val){
arr[i] = val;
}

// Hence the actual array is updated,

In STL everything is a copy is the the actual object, everything is passed by reference

//Passed the array by reference.along with the size of the array


//The array is just the base address of the array
void print(int arr[],int i,int n){
for(int i = 0;i<n;i++){
cout<<arr[i]<<" ";
}
}

In the main it is actual object but in function it is only an address.

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

void updateArray(array<int,6> &xarr, int i, int val){


//This is passed by reference
//Now the changes are shown in real array.
//Here xarr is an alias for a
}

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)

v.capacity(); // capacity of the vector


v.push_back(1); // insert 1 element
v.size(); //size of the vector
v.at(3); // what is at 3rd postition
v.front(); // front of the vector
v.back(); // back of the vector
v.pop_back(); // removes the last element
v.clear(); // clears the vector
// But the capacity of the vector remains the same
v.begin(); //start iterator.

// different method
for (int i:a){
do what ever needed.;
}

// Erases the range or at each specific location


v.erase(v.begin(),v.begin()+3); //

// 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

Why is push_front not present?


As we do not have any additional space in the front to insert a element at front.

This doubling is a expensive opeartion -- Spending linear time to doube the size

so reserve :

v.reserve(100) //better if know the size of input.

4. Deque (Doubly ended queue)


insertion/ delection at both ends of the queue These are multiple fixed tactic array.not like array.

Dyanmic (Expand and contract on both end)

random access possible

#include <deque>
int main(){
// Ways to initialize a deque.
deque<int> d;
deque<int> second (4,100);
deque<int> third (second.begin(),second.end());

int myint[] = {1,2,3,4,5,6,7,8}


int n = sizeof(myint)/sizeof(int);
deque<int> = fifth (myint,myint + n)

d.push_back(1); // push at the end


d.puch_front(2); // push at the start

d.at(1); // get selected index

// Removing Elements
d.pop_back(); // pop back
d.pop_front(); // pop front
d.size();
d.font();
d.back();
d.empty();
d.begin();

// delete a range or single element


d.erase(d.begin(),d.begin()+1)

for (int i:d){


range
}
}
5/9
STL Container.md 2/12/2022

5. List
Used as Doubly Linked List, here we have two pointers, one for front and one for back.

Need to traverse to reach a element.

#include <list>
int main(){
list<int> l;

l.push_back(1);
l.push_front(2);

for (int i:l){


code ;
}
// O(n) for erase.
l.size();
l.begin();
l.end();
l.empty();
// Delete the element where the iterator is pointing.
l.erase(l.begin());

//copying to a new list


list<int> n(l);
list<int> n(5,100); // (no of element,value)

6. Stack
Keep books on a table, the last one put is picked up first.

LIFO : (Last In First Out)

#include <stack>
int main(){
stack<string> s;
s.push("Durgance");
s.push("Gaur");
s.push("Work");

s.top(); //Gives the element at the top "Work"


s.pop(); //Removes the top element.

6/9
STL Container.md 2/12/2022

s.size(); //Returns the number of elements


s.empty();

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

If MaxHeeperQueue : Returns the maximum element first in the queue.

#include <queue>
int main(){
// This is maxheep
priority_queue<int>maxi;

//min - heep
priority_queue<int,vector<int>,

//Min Heap of pair

priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>
q;

greater<int> > mini;

maxi.push(1);
maxi.push(4);
maxi.push(2);

7/9
STL Container.md 2/12/2022

maxi.push(0);

int n = maxi.size();

for (int i;i<n;i++){


cout<<maxi.top()<<" ";
maxi.pop();
}
// output : 3 2 As maxi.size is changing.
// new output : 4 2 1 0
maxi.empty();
}

Writing own Custon comparator:

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.

set is a bit slow in comparison to unordered set.


unordered set is random.

#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

s.count(5); // checks if the element is persent.

set<int>::iterator it2 = s.find(5);


// return the iterator of finds the element

for (auto it=it2; it!=it2.end();it++){


cout<<*it;
}
// Returns 5

insert ,find , erase, count all have log(n) complexity

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.count(13); // Checks if the key is present.

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

You might also like