Thursday, June 25, 2020

STL

Vector:
--Declaration
--> vector <data type> name;
--Insert Element
--> to push an element : name.push_back(element);
--To check Size
--> to check size : name.size();
--Erase Element
--> to erase an element : name.erase(name.begin() + index of the element);
--> to erase several elements : name.erase(name.begin() + index of the 1st element , name.begin() + index of the last element + 1 );
--Clear All Element
--> to erase all elements : name.clear();
--Check data Empty  or not
--> true if the container size is 0, false otherwise: name.empty();

Stack:
--Declaration
--> stack < data type > name
--Insert Element
--> to push an element : name.push(data);
--Access top Element:
--> to access the top element : name.top();
--Remove top Element:
--> to remove the top element : name.pop();
--To check Size
--> to check size : name.size();
--Check data Empty  or not
--> true if the underlying container's size is 0, false otherwise: name.empty();
--Swap between Elemen
-->xchanges the contents of the container adaptor: foo.swap(bar)

Queue:
--Declaration
--> queue < data type > name
--Insert Element
--> to push an element : name.push(data);
--Access top Element:
--> to access the top element : name.front();
--Remove top Element:
--> to remove the top element : name.pop();
--To check Size
--> to check size : name.size();
--Check data Empty  or not
--> true if the underlying container's size is 0, false otherwise: name.empty();
--Swap between Element
-->xchanges the contents of the container adaptor: foo.swap(bar)

Deque:
--Declaration
--> declare : deque < data type > name;
--Insert Element
--> to insert an element from left end : name.push_front(value);
--> to insert an element from right end : name.push_back(value);
--Access Element
--> to access :name.front() and name.back();
--Remove top Element:
--> to remove the front element : name.pop_front();
--> to remove the back element : name.pop_back();
--To check Size
--> to check size : name.size();
--Check data Empty  or not
--> true if the underlying container's size is 0, false otherwise: name.empty();

Priority_queue:
--Declaration
-- declare : priority_queue < data type > name;
--Insert Element
-- to insert an element : name.push(data);
--Access Element
-- to access the top element : name.top();
--Erase Element
-- to erase the top element : name.pop();
--Max Heap
int arr[] = {5 , 6 , 4 , 10 , 9 , -9 , -1};
-- for(int i = 0 ; i<n ; i++) pqq.push(arr[i]);
--Min Heap
int arr[] = {5 , 6 , 4 , 10 , 9 , -9 , -1};
-- for(int i = 0 ; i<n ; i++) pqq.push(-arr[i]);
while(!pqq.empty()){

        printf("%d\n",-pqq.top());
        pqq.pop();
    }

Set:
--Declaration
-- declare : set < data type > name;
--Insert Element
-- to insert an element : name.insert();
--Declare Iterator
-- to declare an iterator : set < data type > :: iterator name;
--Access Element
-- access element using iterator
for(it = st.begin() ; it != st.end() ; it++){

        printf("%d\n",*it);

    }
--Erase Element
-- to erase : name.erase(value);
--Clear Elment
-- to clear all elements : name.clear();

No comments:

Post a Comment