lecture notes for OOdp, Study notes of Object Oriented Programming

lecture notes for Oodp with examples and codes

Typology: Study notes

2022/2023

Uploaded on 11/20/2022

shresta-banerjee-ra2111003011905
shresta-banerjee-ra2111003011905 🇮🇳

5

(1)

5 documents

1 / 205

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT -V
18CSC202J - OBJECT ORIENTED DESIGN AND
PROGRAMMING
Standard Template Library
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

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.