Friday 6 July 2018

C++ STL

Containers or container classes store objects and data. 
  • Sequence Containers:  implement data structures which can be accessed in a sequential manner.
    • vector
    Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
  • Modifiers:


    • list
Lists are sequence containers that allow non-contiguous memory allocation. As compared to vector, list has slow traversal, but once a position has been found, insertion and deletion are quick. Normally, when we say a List, we talk about doubly linked list. 
Functions used with List :
front() – Returns the value of the first element in the list
back() – Returns the value of the last element in the list
push_front(g) – Adds a new element ‘g’ at the beginning of the list
push_back(g) – Adds a new element ‘g’ at the end of the list
pop_front() – Removes the first element of the list, and reduces size of the list by 1
pop_back() – Removes the last element of the list, and reduces size of the list by 1
begin() – Returns an iterator pointing to the first element of the list
end() – Returns an iterator pointing to the theoretical last element which follows the last element
empty() – Returns whether the list is empty(1) or not(0)
insert() – Inserts new elements in the list before the element at a specified position
erase() – Removes a single element or a range of elements from the list
assign() – Assigns new elements to list by replacing current elements and resizes the list
remove() – Removes all the elements from the list, which are equal to given element
reverse() – Reverses the list
size() – Returns the number of elements in the list
sort() – Sorts the list in increasing order


    • deque
hey are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.
The functions for deque are same as vector, with an addition of push and pop operations for both front and back.

    • arrays
The introduction of array class from C++11 has offered a better alternative for C-style arrays. The advantages of array class over C-style array are :-
  • Array classes knows its own size, whereas C-style arrays lack this property. So when passing to functions, we don’t need to pass size of Array as a separate parameter.
  • Array classes are generally more efficient, light-weight and reliable than C-style arrays.
at() :- This function is used to access the elements of array.

// Initializing the array elements
    array<int,6> ar = {1, 2, 3, 4, 5, 6};
 
    // Printing array elements using at()
    cout << "The array elemets are (using at()) : ";
    for ( int i=0; i<6 code="" i="">
    cout << ar.at(i) << " ";
    cout << endl;

fill() :- This function is used to fill the entire array with a particular value.

 front() :- This returns the first element of array.
 back() :- This returns the last element of array.

    • forward_list( Introduced in C++11)(Single Link list)
It differs from list by the fact that forward list keeps track of location of only next element while list keeps track to both next and previous elements, thus increasing the storage space required to store each element. The drawback of forward list is that it cannot be iterated backwards and its individual elements cannot be accessed directly.

  • Container Adaptors :  provide a different interface for sequential containers.
    • queue

    Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.
    The functions supported by queue are :
    empty() – Returns whether the queue is empty
    size() – Returns the size of the queue
    front() – Returns a reference to the first element of the queue
    back() – Returns a reference to the last element of the queue
    push(g) – Adds the element ‘g’ at the end of the queue
    pop() – Deletes the first element of the queue

  • priority_queue
Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non decreasing order(hence we can see that each element of the queue has a priority{fixed order}).


void showpq(priority_queue <int> gq)
{
    priority_queue <int> g = gq;
    while (!g.empty())
    {
        cout << '\t' << g.top();
        g.pop();
    }
    cout << '\n';
}
 
int main ()
{
    priority_queue <int> gquiz;
    gquiz.push(10);
    gquiz.push(30);
    gquiz.push(20);
    gquiz.push(5);
    gquiz.push(1);
 
    cout << "The priority queue gquiz is : ";
    showpq(gquiz);
 
    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.top() : " << gquiz.top();
 
 
    cout << "\ngquiz.pop() : ";
    gquiz.pop();
    showpq(gquiz);
 
    return 0;
}

The output of the above programs is :
The priority queue gquiz is :     30    20    10    5    1

gquiz.size() : 5
gquiz.top() : 30
gquiz.pop() :     20    10    5    1

    • stack
Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.


  • Associative Containers :  implement sorted data structures that can be quickly searched (O(log n) complexity).
    • set

    Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it.

  • find(const g) – Returns an iterator to the element ‘g’ in the set if found, else returns the iterator to end

multiset
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.
map




  • Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.
pair insert(keyvalue,mapvalue) – Adds a new element to the map.






  • find(const g) – Returns an iterator to the element with key value ‘g’ in the map if found, else returns the iterator to end

    • multimap
  • Multimap is similar to mapwith an addition that multiple elements can have same keys. Rather than each element being unique, the key value and mapped value pair has to be unique in this case.









No comments:

Post a Comment