• No results found

stl.ppt

N/A
N/A
Protected

Academic year: 2020

Share "stl.ppt"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

2

Standard Template

Library (STL)

is a library of generic container classes

which are both efficient and functional

C++ STL developed in early 90's at

Hewlett Packard Laboratories

Alex Stepanov and Meng Lee

became part of C++ standard in 1994

implementations available by late 90's

(3)

STL and

Reuseability

STL is part of a movement toward

libraries of reusable code

function libraries (reusable algorithms)

class libraries (reusable ADTs)

STL separates data and algorithms

(4)

4

STL components

Container classes

Iterators

Algorithms

Containers

 templates for classes which hold a collection of elements

Algorithms

 templates for functions which operate on a range of

elements from a container

 range is specified by iterators

Iterators

 give access to the elements in a container

(5)

STL container

classes

Sequences - elements arranged in a linear order

 vector<T> - fast inserts only at the end; random access  list<T> - fast inserts anywhere; no random access

 deque<T> - fast inserts at the beginning and the end

Adapters - provides a new interface (set of

operations) for one of the sequences

 stack<T> ex: stack<int> operands;  queue<T> ex: queue<Customer> line;  priority_queue<T>

Associative Containers - elements have key values

(6)

6

vector<T>

It is dynamic array,growable, self-contained,

type-independent array

 element type specified when a vector is declared

 vector<double> numbers;

 vector<cashier> checkOutStations;

 has a set of operations (methods)

 capacity increases when needed

some vectors

 vector<int> v1; (capacity is 0)

 vector<float> v2 (10); (capacity is 10; size is 10)

(7)

Some vector<T>

methods

V.capacity( ) //size of array currently allocated

V.size( ) //number of values V contains

V.empty( ) //true iff V.size( ) is 0

V.reserve(n) //grow V so its capacity is n

V.push_back(val) //add val at end of V

V.pop_back( ) //erase V's last element

(8)

#include <iostream> #include <vector> using namespace std;

bool Search(const vector<int> & V, int item);

int main ( ) {

vector<int> numbers; int number;

while (cin >> number) { // enter <control> D to stop the loop if (Search(numbers, number))

cout << "Duplicate" << endl; else numbers.push_back(number); }

cout << "number of unique values: " << numbers.size( ); return 0;

}

bool Search(const vector<int> & V, int item) { int p = 0;

while(p < V.size( ) )

if (item = = V[p]) // or V.at(p) return true;

(9)

STL iterators

iterators are "pointer-like" objects

 provide a generic way to access the elements of any

container class

 many STL algorithms require iterators as arguments  some STL algorithms return an iterator

each STL container class has an iterator class

associated with it

 vector<T>::iterator  list<T>::iterator

stack<T> and queue<T> don't have iterators

(10)

10

Iterator

categories

category determines available operations

forward iterator

 iter++ (increment)  *iter (dereference)

 == and != (equality comparison)

bidirectional iterator adds

 iter-- (decrement)

random-access iterator adds

(11)

STL Algorithms

are function templates designed to operate on a

sequence of elements rather than methods

the sequence is designated by two iterators

most container classes have the following two

methods

 begin( ) - returns an iterator positioned at the

container's first element

 end( ) - returns an iterator positioned past the

container's last element (past-the-end)

C.begin( ), C.end( ) specifies a sequence which

(12)

12

STL Algorithms

some examples of STL algorithms

find (iter1, iter2, value) //returns an iterator

max_element (iter1, iter2) //returns an iterator

sort (iter1, iter2) //sorts using <

for_each (iter1, iter2, F) //applies F to every

//item

(13)

#include <iostream> #include <vector>

#include <algorithm>

using namespace std;

int main ( ) {

vector<int> numbers; int number;

while (cin >> number) {

if (find (numbers.begin ( ), numbers.end ( ), number) != numbers.end ( )) cout << "Duplicate" << endl;

else numbers.push_back(number); }

(14)

14

list<T> class

another STL container class

used for storing a linear collection of

like items

comparison to a vector?

linked list vs array is the underlying

data structure

no indexing (iterators are bidirectional)

inserts and deletes anywhere are done

(15)

a list<T> data

structure

2

---head

(16)

16

Basic list class

methods

list( ); // construct an empty list

list (const list<T> & aList); // copy constructor

~list( ); // destructor

list<T> operator= (const list<T> & aList);

// assignment operator

bool empty( );

(17)

Some more list

methods

L.push_back(value) // append value to L

L.push_front(value) // insert value at front of L

L.insert(pos, value) // insert value into L at

// position indicated by iterator pos

L.front( ) // return L's first element

L.back( ) // return L's last element

L.begin( ) // return an iterator positioned at

start

(18)

#include <list>

#include <iostream>

using namespace std;

int main ( ) {

list<int>L;

L.push_back (9);

L.push_back (7);

L.push_back (5);

L.push_back (3);

list<int>::iterator p;

for (p = L.begin ( ); p != L.end ( ); p++)

cout << *p << endl;

for (p = L.begin ( ); p != L.end ( ); p++)

(*p)++;

for (p = L.begin ( ); p != L.end ( ); p++)

cout << *p << endl;

return 0;

}

9

7

5

3

10

8

6

4

(19)

Associative

Containers

Offer O(log n) insertion, suppression and access

Store only weakly strict ordered types (eg. numeric

types)

 Must have operator<() and operator==() defined

and !(a<b) && !(b<a) ≡ (a==b)

The sorting criterion is also a template parameterset<T> – the item stored act as key, no duplicates  multiset<T> – set allowing duplicate items

map<K,V> – separate key and value, no duplicates  multimap<K,V> – map allowing duplicate keys

(20)

20

Container Adaptors

There are a few classes acting as

wrappers around other containers,

adapting them to a specific interface

stack

– ordinary LIFO

queue

– single-ended FIFO

priority_queue

– the sorting criterion

can be specified

Programmers can specify the

References

Related documents

One area that denotes Service Level Management maturity within ITSM is the development and maintenance of a Service Catalog that includes identifying and qualifying the types

As a result, it have been demonstrated that decline curve analysis based on pressure-normalized rates can be used as a reliable production forecasting technique suited to

 – Design Theory states facts Design Theory states facts -- Design Theory aids design -- Design Theory aids

In a Doubly Linked List, each node has a data item &amp; two pointers:..  A pointer to the

• There is a major need for providers in Maine that can prescribe buprenorphine, morphine for Medication Assisted Treatment (MAT) for Opioid Use Disorder (OUD). Much like primary

O objetivo deste artigo é demonstrar a aplicação de métodos automatizados para geração de mapas de danos de fachadas a partir das nuvens de pontos obtidas pelo

PROCESS FOR REVISING POs Inputs from stakeholders Department Committee BOS Academic Council Governing Council Department Vision, Mission and P E O Existing POs 74