Download C++ Standard Template Library (STL) - Data Structures and Algorithms and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!
C++ STL
Stack, Queue, Vector, Pair, Map
The C++ Standard Template Library (STL)
- (^) The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms and iterators. It is a generalized library and so, its components are parameterized.
- (^) STL has four components
- (^) Algorithms
- (^) Containers
- (^) Functions
- (^) Iterators
Algorithms ( #include )
- (^) The header algorithm defines a collection of functions especially designed to be
used on ranges of elements. They act on containers and provide means for various
operations for the contents of the containers.
- (^) Sorting :
- Sorting is one of the most basic functions applied to data. It means arranging the data in a particular
fashion, which can be increasing or decreasing. There is a builtin function in C++ STL by the name of sort().
- Internally this function is implemented as Quick-sort. The complexity of it is O(N*log(N)).
- (^) Searching :
- Binary search is a widely used searching algorithm that requires the array to be sorted before search is
applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until
the element is found, or all the elements are exhausted.
- (^) It works by comparing the middle item of the array with our target, if it matches, it returns true otherwise
if the middle term is greater than the target, the search is performed in the left sub-array.
- (^) If the middle term is less than target, the search is performed in the right sub-array.
Algorithms ( #include ) Sort in C++ STL #include #include using namespace std; void show( int a[]) { for ( int i = 0; i < 10; ++i) cout << a[i] << " "; } int main() { int a[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; cout << "\n The array before sorting is : "; show(a); sort(a, a+10); cout << "\n\n The array after sorting is : "; show(a); return 0; } sort (startAddress, endAddress)
- startAddress: the address of the first element of the array
- endAddress: the address of the last element of the array The output of this program is: The array before sorting is : 1 5 8 9 6 7 3 4 2 0 The array after sorting is : 0 1 2 3 4 5 6 7 8 9
Algorithms ( #include ) Binary Search in C++ STL
- (^) Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted.
- (^) It works by comparing the middle item of the array with our target, if it matches, it returns true otherwise if the middle term is greater than the target, the search is performed in the left sub-array.
- (^) If the middle term is less than target, the search is performed in the right sub- array.
Algorithms ( #include ) Binary Search in C++ STL cont. binary_search (startAddress, endAddress, valueToFind)
- startAddress: the address of the first element of the array.
- endAddress: the address of the last element of the array.
- valueToFind: the target value which we have to search for. #include #include using namespace std; void show(int a[], int arraysize) { for (int i = 0; i < arraysize; ++i) cout << a[i] << " "; } void main() { int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; int asize = sizeof(a) / sizeof(a[0]); cout << "\n The array is : "; show(a, asize); cout << "\n Let's say we want to search for 2 in the array"; cout << "\n So, we first sort the array"; sort(a, a + asize); cout << "\n The array after sorting is : "; show(a, asize); cout << "\n Now, we do the binary search"; if ( binary_search(a, a + 10, 2) ) cout << "\n Element found in the array"; else cout << "\n Element not found in the array"; cout << "\n Now, say we want to search for 10"; if ( binary_search(a, a + 10, 10) ) cout << "\n Element found in the array"; else cout << "\n Element not found in the array"; } The output of the program is: The array is : 1 5 8 9 0 6 7 3 4 2 0 Let's say we want to search for 2 in the array So, we first sort the array The array after sorting is : 0 1 2 3 4 5 6 7 8 9 Now, we do the binary search Element found in the array Now, say we want to search for 10
Containers
- Containers or container classes store objects and data. There are in total seven standard โfirst-classโ container classes and three container adaptor classes and only seven header files that provide access to these containers or container adaptors.
- Sequence Containers: implement data structures which can be accessed in a sequential manner.
- vector
- (^) list
- deque
- (^) arrays
- (^) forward_list( Introduced in C++11)
- (^) Container Adaptors : provide a different interface for sequential containers.
- (^) queue
- (^) priority_queue
- stack
- (^) Associative Containers : implement sorted data structures that can be quickly searched (O(log n)complexity).
- (^) set
- multiset
- (^) map
- multimap
Lists in C++ STL ( #include
)
- (^) 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. For implementing a singly linked list, we use forward list.
- (^) It will be covered in Data Structure-
Stack in C++ STL ( #include ) cont. #include #include using namespace std; void showstack(stack s) { while (!s.empty()) { cout << '\t' << s.top(); s.pop(); } cout << '\n'; } void main () { stack s; s.push(10); s.push(30); s.push(20); s.push(5); s.push(1); cout << "The stack is : "; showstack(s); cout << "\ns.size() : " << s.size(); cout << "\ns.top() : " << s.top(); cout << "\ns.pop() : "; s.pop(); showstack(s); } The output of this program is: The stack is : 1 5 20 30 10 s.size() : 5 s.top() : 1 s.pop() : 5 20 30 10
Queue in C++ STL ( #include )
- (^) 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.
Problem Solving (stack)โฆ
- (^) Solve 26B Regular Bracket Sequence in codeforces
- (^) https://codeforces.com/problemset/problem/26/B
- (^) Solution:
- (^) https://ideone.com/9Kcd
- (^) Solve 343B Alternating Current in codeforces
- (^) https://codeforces.com/contest/343/problem/B
Vectors in C++ STL ( #include )
- Vectors are the 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.
- (^) In vectors, data is inserted at the end. Inserting at the end takes differential time, as sometimes there may be a need of extending the array. O(1) if no size extension.
- (^) Removing the last element takes only constant time โO(1)โ because no resizing happens.
- (^) Inserting and erasing at the beginning or in the middle is linear in time โO(n)โ.
Vectors in C++ STL ( #include ) cont. Certain functions associated with the vector are:
vector Vect;
Iterators
- (^) begin() โ Returns an iterator pointing to the first element in the vector
- end() โ Returns an iterator pointing to the theoretical element that follows the last element in the vector - (^) Vect.end();
- (^) rbegin() โ Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from last to first element - (^) Vect.rbegin();
- (^) rend() โ Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end) - (^) Vect.rend();
vector Vect, Vect2;
Capacity
- (^) size() โ Returns the number of elements in the vector. cout << Vect.size();
- (^) resize(g) โ Resizes the container so that it contains โgโ elements. Vect.resize(100); Vect.resize(100, -1);
- (^) Element access:
- (^) front() โ Returns a reference to the first element in the vector cout<<Vect.front();
- (^) back() โ Returns a reference to the last element in the vector cout<<Vect.back();
- (^) Modifiers:
- (^) push_back() โ It push the elements into a vector from the back Vect.push_back(10);
- (^) pop_back() โ It is used to pop or remove elements from a vector from the back. Vect.pop_back();
- (^) insert() โ It inserts new elements before the element at the specified position Vect.insert(v.begin(),5);
- (^) erase() โ It is used to remove elements from a container from the specified position or range. Vect.erase(v.begin());
- (^) swap() โ It is used to swap the contents of one vector with another vector of same type and size. Vect.swap(Vect2);
- (^) clear() โ It is used to remove all the elements of the vector container Vect.clear(); Vectors in C++ STL ( #include ) cont. Certain functions associated with the vector are: