Download STL vector class, sort algorithm. STL list class, STL iterators

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Standard Template Library

The STL is a large part of the C++ Standard Library

Major components

data structures (containers/collections for data – CS2851)


algorithms


Common operations (functions) that can be applied to many STL
containers
iterators



Fall 2005
vector (array), list, deque, stack, queue, set, …
link the algorithms to the data structures.
iterators are designed for efficient traversal through the data structures.
Similar to pointers
CS-1030
Dr. Mark L. Hornick
1
Linked Lists – the list class

Arranges data in non-contiguous blocks

Links them together to create order
Stored Object
Link to Previous Cell
Link to Next Cell
Fall 2005
CS-1030
Dr. Mark L. Hornick
2
list is a Doubly Linked List
Main List Cell
Size
Data
Data
First
…
Last
…
Data
…
Fall 2005
CS-1030
Dr. Mark L. Hornick
3
list in C++ (STL)

Syntax
#include <list>
using namespace std;
list<T> collection;
// T: datatype stored in vector
Fall 2005
CS-1030
Dr. Mark L. Hornick
4
list Operations

Adding elements




push_back – At the end
push_front – At the beginning
insert – Anywhere
Removing an element



Fall 2005
pop_back – From the end
pop_front – From the beginning
erase, remove - Anywhere
CS-1030
Dr. Mark L. Hornick
5
list Operations (2)

Information accessors/inspectors



size – How many?
empty – Are there none?
Modification


Fall 2005
sort – NOT part of <algorithm>
reverse – change the order
CS-1030
Dr. Mark L. Hornick
6
The vector class




vector is a “smart” array
Like the Java ArrayList
Storage is in consecutive memory locations
Location =
vector start 
0


start + index*size
Very little storage overhead

Only need the start and object size
1
…
n-1
Fall 2005
CS-1030
Dr. Mark L. Hornick
7
vectors in C++ (STL)


User-specified data type is stored
Syntax:
#include <vector>
using namespace std;
vector<T> collection;
// T: datatype stored in vector
// Like Java’s ArrayList<T>
Fall 2005
CS-1030
Dr. Mark L. Hornick
8
vector Operations

Adding elements:




push_back – At the end
push_front – At the beginning
insert – Anywhere
Removing an element



Fall 2005
pop_back – From the end
pop_front – From the beginning
erase, remove - Anywhere
CS-1030
Dr. Mark L. Hornick
9
lists and vectors usage

Example
Fall 2005
CS-1030
Dr. Mark L. Hornick
10
lists vs. vectors

Advantages

Flexible storage



Grows easier
Ultimately more data can be stored
Disadvantages

O(n) access time


Fall 2005
Loss of indexing
Storage overhead is higher
CS-1030
Dr. Mark L. Hornick
11
Iterators

A mechanism for sequentially accessing


STL containers
More important for list than vector



Vector can use indexing for access (e.g. x[i])
Automatically included as part of the
container
Helps describe the next and previous links
Fall 2005
CS-1030
Dr. Mark L. Hornick
12
Iterator Notation

Declaration


list<TYPE>::iterator NAME;
Operators



* Access data element (dereferencing)
++,-- Advance or retreat in the list
==,!= Comparison

Fall 2005
NOTE: No < or >
CS-1030
Dr. Mark L. Hornick
13
Iterators in a Linked List
Main List Cell
Size
First
Last
.begin()
…
…
…
Fall 2005
Data
Data
CS-1030
Dr. Mark L. Hornick
Data
.end()
14
Iterator Variations

Standard iterators can be used to change a
list


*iter = newValue;
Special iterators for const lists (can’t
change)


list<TYPE>::const_iterator citer;
Reverse iterators


list<TYPE>::reverse_iterator riter;
list<TYPE>::const_reverse_iterator
criter;
Fall 2005
CS-1030
Dr. Mark L. Hornick
15
More on Reverse Iterators

For going from end to beginning



++ Retreats
-- Advances
Bounds


Fall 2005
rbegin() – Last
rend() – One “before” the first
CS-1030
Dr. Mark L. Hornick
16
Other iterator Member
Functions


These member functions need iterators

insert – Where to insert

erase – Which one to erase

find – Locate the item that was found
Iterators also apply to vectors

Usage is the same

Fall 2005
vector<TYPE>::iterator NAME;
CS-1030
Dr. Mark L. Hornick
17
STL Algorithms


An algorithm is an operation (function) that can be
applied to many STL containers
The STL algorithms are generic - they can operate
on a variety of data structures.



STL container classes such as vector and list
Program-defined data structures
 Behavior must satisfy the requirements of a particular
algorithm
STL algorithms achieve generality by accessing and
traversing the elements of a container indirectly
through iterators.

Fall 2005
Iterators must be settable and dereferencable
CS-1030
Dr. Mark L. Hornick
18
Sort Algorithm

The sort algorithm is an operation (function)
that can be applied to many STL containers


notable exception: list container (has it’s own
method)
sort() orders a container's contents in
ascending order


Fall 2005
as defined by the operator<() as applied to the
container’s elements
Programmer-defined types can be sorted just as
easily as a built-in type
CS-1030
Dr. Mark L. Hornick
19
find Algorithm



searches a subrange of the elements in a container (or all the
elements),
 looks for an element that is "equal to" a specified value; stops
when it finds the first element equal to the specified value
 the equality operator (==) must be defined for the type of the
container's elements.
search value must be of the same type as the elements stored in
the container
 or of a type that the compiler can automatically convert.
Return value is an iterator specifying the position of the first
matching element.
 If no matching element is found, the return value is equal to the
iterator specifying the end of the element subrange
Fall 2005
CS-1030
Dr. Mark L. Hornick
20