STL in OOP: Containers, Iterators, and Algorithms, Slides of Object Oriented Programming

The standard template library (stl) is a powerful collection of data structures and algorithms added to the c++ standard library. An overview of stl, its components, and benefits. It includes examples of using stl vectors, deques, lists, sets, multisets, maps, and multimaps.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(41)

213 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 41
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download STL in OOP: Containers, Iterators, and Algorithms and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 41

Standard Template Library

  • C++ programmers commonly use many

data structures and algorithms

  • So C++ standard committee added the

STL to C++ standard library

  • STL is designed to operate efficiently

across many different applications

STL Promotes Reuse

  • Software reuse saves development time

and cost

  • Once tested component can be reused

several times

STL Containers

  • Container is an object that contains a

collection of data elements

  • STL provides three kinds of containers
    • Sequence Containers
    • Associative Containers
    • Container Adapters

…Sequence Containers

  • vector
    • Rapid insertions and deletions at back end
    • Random access to elements
  • deque
    • Rapid insertions and deletions at front or back
    • Random access to elements
  • list
    • Doubly linked list
    • Rapid insertions and deletions anywhere

Example – STL Vector

#include

int main() {

std::vector< int > iv; int x, y; char ch; do { cout<<"Enter the first integer:"; cin >> x; cout<<"Enter the second integer:"; cin >> y;

Sample Output

Enter the first integer: 1

Enter the second integer: 2

Current capacity of iv = 2

Current size of iv = 2

Do you want to continue? y

…Sample Output

Enter the first integer: 3

Enter the second integer: 4

Current capacity of iv = 4

Current size of iv = 4

Do you want to continue? y

Example – STL Deque

#include

int main() {

std::deque< int > dq; dq.push_front( 3 ); dq.push_back( 5 );

dq.pop_front(); dq.pop_back() return 0;

}

Example – STL List

#include

int main() {

std::list< float > _list; _list.push_back( 7.8 ); _list.push_back( 8.9 ); std::list< float >::iterator it = _list.begin(); _list.insert( ++it, 5.3 ); return 0;

}

…Associative Containers

  • set
    • No duplicates
  • multiset
    • Duplicates allowed
  • map
    • No duplicate keys
  • multimap
    • Duplicate keys allowed

Example – STL Set

#include

int main() {

std::set< char > cs; cout << “Size before insertions: “ << cs.size() << endl; cs.insert( ‘a’ ); cs.insert( ‘b' ); cs.insert( ‘b' ); cout << “Size after insertions: ” << cs.size(); return 0;

} Docsity.com

Example – STL Multi-Set

#include

int main() {

std::multiset< char > cms; cout << "Size before insertions: " << cms.size() << endl; cms.insert( 'a' ); cms.insert( 'b' ); cms.insert( 'b' ); cout << "Size after insertions: " << cms.size(); return 0;

} Docsity.com

Output

Size before insertions: 0

Size after insertions: 3