Download lecture notes for OOdp and more Study notes Object Oriented Programming in PDF only on Docsity!
UNIT -V
18CSC202J - OBJECT ORIENTED DESIGN AND
PROGRAMMING
Standard Template Library
What is 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.
- (^) A working knowledge of template classes is a prerequisite for working with STL.
The C++ Standard Template Libraries
- (^) In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C+
- with a library of class and function templates which has come to be known as the STL.
- (^) In 1994, STL was adopted as part of ANSI/ISO Standard C++.
The C++ Standard Template Libraries
- (^) STL had three basic components:
- (^) Containers Generic class templates for storing collection of data.
- (^) Algorithms Generic function templates for operating on containers.
- (^) Iterators Generalized ‘smart’ pointers that facilitate use of containers. They provide an interface that is needed for STL algorithms to operate on STL containers.
- (^) String abstraction was added during standardization.
STL Containers
Sequence and Associative Containers
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
- (^) Container Adaptors
- (^) Associative Containers
- (^) Unordered Associative Containers
Topic : Sequence Container: Vector List
Sequence Containers: 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. 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.
- (^) Removing the last element takes only constant time because no resizing
happens. Inserting and erasing at the beginning or in the middle is linear in
time.
functions associated with the vector
// C++ program to illustrate the iterators in vector #include #include using namespace std; int main() { vector g1; for (int i = 1; i <= 5; i++) g1.push_back(i); cout << "Output of begin and end: "; for (auto i = g1.begin(); i != g1.end(); ++i) cout << *i << " "; cout << "\nOutput of cbegin and cend: "; for (auto i = g1.cbegin(); i != g1.cend(); ++i) cout << *i << " "; cout << "\nOutput of rbegin and rend: "; for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir) cout << *ir << " "; cout << "\nOutput of crbegin and crend : "; for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir) cout << *ir << " "; return 0; } Output: Output of begin and end: 1 2 3 4 5 Output of cbegin and cend: 1 2 3 4 5 Output of rbegin and rend: 5 4 3 2 1 Output of crbegin and crend : 5 4 3 2 1
// C++ program to illustrate the capacity function in vector #include #include using namespace std; int main() { vector g1; for (int i = 1; i <= 5; i++) g1.push_back(i); cout << "Size : " << g1.size(); cout << "\nCapacity : " << g1.capacity(); cout << "\nMax_Size : " << g1.max_size(); // resizes the vector size to 4 g1.resize(4); // prints the vector size after resize() cout << "\nSize : " << g1.size(); // checks if the vector is empty or not if (g1.empty() == false) cout << "\nVector is not empty"; else cout << "\nVector is empty"; // Shrinks the vector g1.shrink_to_fit(); cout << "\nVector elements are: "; for (auto it = g1.begin(); it != g1.end(); it++) cout << *it << " "; return 0; } Output: Size : 5 Capacity : 8 Max_Size : 4611686018427387903 Size : 4 Vector is not empty Vector elements are: 1 2 3 4
functions associated with the vector
functions associated with the vector
Sequence Container: 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. For
implementing a singly linked list, we use forward list.