0% found this document useful (0 votes)
82 views

C++ STL Standard Template Librairy

Here is a C++ program that reads in 20 numbers into a list and prints the list in reverse order: #include <iostream> #include <list> using namespace std; int main() { list<int> numbers; for(int i=0; i<20; i++) { int num; cin >> num; numbers.push_back(num); } list<int>::reverse_iterator rit; for(rit=numbers.rbegin(); rit!=numbers.rend(); rit++) { cout << *rit << " "; } return 0; } This program: 1. Includes the necessary list and iostream
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

C++ STL Standard Template Librairy

Here is a C++ program that reads in 20 numbers into a list and prints the list in reverse order: #include <iostream> #include <list> using namespace std; int main() { list<int> numbers; for(int i=0; i<20; i++) { int num; cin >> num; numbers.push_back(num); } list<int>::reverse_iterator rit; for(rit=numbers.rbegin(); rit!=numbers.rend(); rit++) { cout << *rit << " "; } return 0; } This program: 1. Includes the necessary list and iostream
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

C++ STL

CSCI 3110
STL – Standard Template Library
 Collections of useful classes for common data structures
 Ability to store objects of any type (template)
 Study of containers
 Containers form the basis for treatment of data structures
 Container – class that stores a collection of data
 STL consists of 10 container classes:
 Sequence containers
 Adapter containers
 Associative containers
STL Containers
 Sequence Container
 Stores data by position in linear order:
 First element, second element , etc:
 Associate Container
 Stores elements by key, such as name, social security number or
part number
 Access an element by its key which may bear no relationship to
the location of the element in the container
 Adapter Container
 Contains another container as its underlying storage structure
STL Containers
 Sequence Container
 Vector
 Deque
 List
 Adapter Containers
 Stack
 Queue
 Priority queue
 Associative Container
 Set, multiset
 Map, multimap
Vector Container
 Generalized array that stores a collection of elements of the
same data type
 Vector – similar to an array
 Vectors allow access to its elements by using an index in the
range from 0 to n-1 where n is the size of the vector
 Vector vs array
 Vector has operations that allow the collection to grow and
contract dynamically at the rear of the sequence
Vector Container
Example:
#include <vector>
.
.
.
vector<int> scores (100); //100 integer scores
vector<Passenger>passengerList(20);//list of 20 passengers
Vector Container
 Allows direct access to the elements via an index operator
 Indices for the vector elements are in the range from 0 to
size() -1
 Example:
#include <vector>
vector <int> v(20);
v[5]=15;
Vector Operations
 See
http://cs.smu.ca/~porter/csc/ref/stl/cont_vector.html
For list of vector operations.
List Container
 Stores elements by position
 Each item in the list has both a value and a memory address
(pointer) that identifies the next item in the sequence
 To access a specific data value in the list, one must start at the
first position (front) and follow the pointers from element to
element until data item is located.
 List is not a direct access structure
 Advantage: ability to add and remove items efficiently at any
position in the sequence
STL List
 See http://cs.smu.ca/~porter/csc/ref/stl/cont_list.html
 for list of STL list operations.
Stack Container
 Adapter Container
 These containers restrict how elements enter and leave a
sequence
 Stack
 allows access at only one end of the sequence (top)
 Adds objects to container by pushing the object onto the stack
 Removes objects from container by popping the stack
 LIFO ordering (last end, first out)
Queue Container
 Queue
 Allows access only at the front and rear of the sequence
 Items enter at the rear and exit from the front
 Example: waiting line at a grocery store
 FIFO ordering (first-in first-out )
 push(add object to a queue)
 pop (remove object from queue)
Priority Queue Container
 Priority queue
 Operations are similar to those of a stack or queue
 Elements can enter the priority queue in any order
 Once in the container, a delete operation removes the largest
(or smallest) value
 Example: a filtering system that takes in elements and then
releases them in priority order 8

18 13
3 15

27
Set Container
 Set
 Collection of unique values, called keys or set members
 Contains operations that allow a programmer to:
 determine whether an item is a member of the set
 insert and delete items very efficiently

Set A Set B
Buick Ford
5 1 3
Jeep BMW
6 27 15
Multi-Set Container
 A multi-set is similar to a set, but the same value can be in
the set more than once
 Multi-set container allows duplicates
Map Container
 Implements a key-value relationship
 Programmer can use a key to access corresponding values
 Example: key could be a part number such as A24-57 that
corresponds to a part: 8.75 price and Martin manufacturer

A22-56 A24-57 8.75 Martin

A23-57 A22-56 12.50 Calloway


A23-57 4.95 Mirage
A24-57
Multi-map Container
 Similar to a map container
 Multi-map container allows duplicates
How to access Components -
Iterator
 Iterator is an object that can access a collection of like objects
one object at a time.
 An iterator can traverse the collection of objects.
 Each container class in STL has a corresponding iterator that
functions appropriately for the container
 For example: an iterator in a vector class allows random
access
 An iterator in a list class would not allow random access (list
requires sequential access)
Common Iterator Operations

* Return the item that the iterator currently references


++ Move the iterator to the next item in the list
-- Move the iterator to the previous item in the list
== Compare two iterators for equality
!= Compare two iterators for inequality
STL List Class
Constructors and assignment
 list <T> v;
 list<T> v(aList);
 l=aList;
Access
 l.front() ----returns 1st element in the list
 l.back()----returns the last element in the list
STL List
Insert and Remove
 l.push_front(value)
 l.push_back(value)

Iterator Delaration
 list<T>::iterator itr;

Iterator Options
 itr = l.begin() set iterator to beginning of the list
 Itr = l.end() set iterator to after the end of the list
Writing classes that work with the STL
 Classes that will be stored in STL containers should
explicitly define the following:
◦ Default constructor
◦ Copy constructor
◦ Destructor
◦ operator =
◦ operator==
◦ operator<
 Not all of these are always necessary, but it might be easier
to define them than to figure out which ones you actually
need
 Many STL programming errors can be traced to omitting or
improperly defining these methods
More on STL Lists
 Go to: http://cs.smu.ca/~porter/csc/ref/stl/
Create a client file to read in 20 numbers
into a list and print the list in reverse order.

You might also like