Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 41
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 41
(OOP)
Lecture No. 41
Standard Template Library
• C++ programmers commonly use many
data structures and algorithms
Current capacity of iv = 2
Current size of iv = 2
Current capacity of iv = 4
Current size of iv = 4
Current capacity of iv = 8
Current size of iv = 6
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 <list>
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
Value @ key 2 is b
Example – STL Multi-Map
#include <map>
int main() {
typedef std::multimap< int, char >
MyMap;
MyMap m;
m.insert(MyMap::value_type(1, 'a'));
m.insert(MyMap::value_type(2, 'b'));
m.insert(MyMap::value_type(3, 'b'));
…Example – STL Multi-Map
Value @ key 2 is b
Value @ key 3 is b
First-class Containers
• Default constructor
• Copy Constructor
• Destructor
• empty()
– Returns true if container contains no elements
• max_size()
– Returns the maximum number of elements
…Common Functions for All
Containers
• size()
– Return current number of elements
• operator = ()
– Assigns one container instance to another
• operator < ()
– Returns true if the first container is less than
the second container
…Common Functions for All
Containers
• operator <= ()
– Returns true if the first container is less than or
equal to the second container
• operator > ()
– Returns true if the first container is greater than
the second container
• operator >= ()
– Returns true if the first container is greater than
or equal to the second container
…Common Functions for All
Containers
• operator == ()
– Returns true if the first container is equal to
the second container
• operator != ()
– Returns true if the first container is not equal
to the second container
• swap ()
– swaps the elements of the two containers
Functions for First-class
Containers
• begin()
– Returns an iterator object that refers to the
first element of the container
• end()
– Returns an iterator object that refers to the
next position beyond the last element of the
container
…Functions for First-class
Containers
• rbegin()
– Returns an iterator object that refers to the
last element of the container
• rend()
– Returns an iterator object that refers to
the position before the first element
…Functions for First-class
Containers
• erase( iterator )
– Removes an element pointed to by the
iterator
• clear()
– erases all elements from the container
Container Requirements