Top Banner
STANDARD TEMPLATE LIBRARIES Michael Heron
23

2CPP16 - STL

Nov 02, 2014

Download

Software

Michael Heron

This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 2CPP16 - STL

STANDARD TEMPLATE LIBRARIESMichael Heron

Page 2: 2CPP16 - STL

Introduction• The great promise of object orientation is reusability.• As of now, we haven’t done much re-using.• In the same way that Java provides comprehensive

libraries of basic structures, so too does C++• Through the mechanism of the Standard Template Library.

Page 3: 2CPP16 - STL

Standard Template Library

• The STL is not a part of the core C++ language.• It’s a set of extended libraries that have nearly

ubiquitous applicability.• They are defined in the C++ standard.

• It provides implementations of many of the standard data types available in other languages.

• It is driven by the power of templates (as discussed in the previous lecture).

Page 4: 2CPP16 - STL

Standard Template Library

• The STL subset that we are going to pay most attention to is that of collections.• Basic, core data structures.

• You’ll hear more about how these are internally implemented in a later part of the module.

• The collection classes allow us to make use of powerful, flexible data types without us having to write them ourselves.• There’s a considerable development burden in writing

objects that are genuinely reuseable.

Page 5: 2CPP16 - STL

Collections• Much like an array, a collection holds a number of discreet

elements.• Unlike an array, ordering cannot be assumed with many

collections.

• These classes are not designed to be base classes for more specialised derived classes.• Destructors for example are explictly designed to be non-virtual.

Page 6: 2CPP16 - STL

Collections• Collections break down into three main categories.• Sequential, where order is maintained.

• List• Vector

• Associative, where no order is guaranteed.• Set• Hash Map

• Adapter, which act as wrappers around other core structures.• Queue• Stack

Page 7: 2CPP16 - STL

The Vector• The Vector is defined in the std namespace.

• #include <vector> into your code.

• Vectors are resizeable arrays.• You can just keep adding things in and they’ll expand to meet your

requirements.

• Basic structure for interacting with a vector is common to all STL collections.

Page 8: 2CPP16 - STL

The Vector

#include <iostream>

#include <vector>

using namespace std;

int main() {

vector<int> *v = new vector<int>();

v->push_back (100);

v->push_back (200);

v->push_back (500);

for (int i = 0; i < v->size(); i++) {

cout << v->at (i) << endl;

}

return 0;

}

Page 9: 2CPP16 - STL

The Vector• Useful methods:

• push_back – push an element to the back of the structure• size – get the number of elements in the collection• at – pull the element out at that specific position.

• Memory management of a container done during accesses.

Page 10: 2CPP16 - STL

Vector Memory Management• Two measures for collections are available.

• capacity – how many elements can the collection hold before new space needs to be allocated.

• max_size – how many elements, in total, the collection can hold.• Determined by the system.

• resize() and reserve() allow for fine-grained control over capacity.

Page 11: 2CPP16 - STL

The List• The list implements a doubly-linked list for traversal.

• Can go forwards and backwards.

• Basic structure for adding to a list the same as with a vector.• Can also push_front

• Traversal of the list done through an iterator.• Possible for most of the STL classes.

Page 12: 2CPP16 - STL

Iterators• Iterators are a common interface across container

classes.• They represent an object that allows traversal through a collection.• Obtained by using the following syntax:

• Collection<type>::iterator• list<int>::iterator

• The iterator serves as the basis for more elegant output of state date.

Page 13: 2CPP16 - STL

Iterators

#include <iostream>

#include <list>

using namespace std;

int main() {

list<int> *l = new list<int>();

list<int>::iterator i;

l->push_back (100);

l->push_front(200);

for (i = l->begin(); i != l->end(); i++) {

cout << *i << endl;

}

return 0;

}

Page 14: 2CPP16 - STL

Reversal Traversal

#include <iostream>

#include <list>

using namespace std;

int main() {

list<int> *l = new list<int>();

list<int>::reverse_iterator i;

l->push_back (100);

l->push_front(200);

for (i = l->rbegin(); i != l->rend(); i++) {

cout << *i << endl;

}

return 0;

}

Page 15: 2CPP16 - STL

Collections• Most sequence collections also implement a sort function.

• Another time we need to overload an operator as part of C++• < operator must be overloaded to permit sorting of custom objects

based on developer requirements.

• Many other useful functions available.• Won’t cover these in the lecture – documentation available in many

places.

Page 16: 2CPP16 - STL

Algorithms• STL also contains implementations for common

algorithms.• Sort• Searches

• Must #include <algorithm> to get access to methods.• Many of these methods similarly require operators to be

overloaded.• Curse you C++.

Page 17: 2CPP16 - STL

Vector with Iterator, Sort and Search#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

int main() {

vector<int> *v = new vector<int>();

vector<int>::iterator i;

bool found;

v->push_back (200);

v->push_back (100);

v->push_back (500);

sort (v->begin(), v->end());

found = binary_search (v->begin(), v->end(), 200);

cout << "Value 200 found in Vector" << endl;

for (i = v->begin(); i < v->end(); i++) {

cout << *i << endl;

}

return 0;

}

Page 18: 2CPP16 - STL

Class Defined for Sorting

class Person {

private:

int age;

public:

Person();

Person (int x);

void set_age (int x);

int query_age();

bool operator< (Person &a);

};

Page 19: 2CPP16 - STL

Class Implementation

#include "Person.h"

Person::Person() :

age (18) {

}

Person::Person (int x) :

age (x) {

}

int Person::query_age() {

return age;

}

void Person::set_age (int na) {

age = na;

}

bool operator<(Person& a, Person& b) {

return a.query_age() < a.query_age();

}

Page 20: 2CPP16 - STL

Versus Java• It seems like much more is needed to implement a C++

class for collections than in Java.• The same amount of code is required for both.• Java requires an implementation of compareTo.

• C++ requires an implementation of an overloaded < operator.

Page 21: 2CPP16 - STL

Why Use STL Classes?• The STL classes are battle-hardened and battle-scarred

• They work.

• They make use of templates, and thus can handle any appropriately defined object you devise.

• Most of the common data structures are available as part of the STL.

Page 22: 2CPP16 - STL

Why Not Use Them?• Well, no real reason not to…• … for real code.• Array manipulation exercises and data structure creation are important ‘clarity’ topics.• It’s always worth understanding how data structures are

implemented.• Remember, C++ is all about how things are represented

internally.

• Best to learn how to ‘roll your own’ first.• Then use the STL versions because they are more

complete.

Page 23: 2CPP16 - STL

Summary• C++ provides implementations of most of the standard

data types.• And also corresponding implementations of default operations such

as searchs and sorts.

• Vectors and lists permit the traversal of ordered data.• Important to define user classes appropriately for sorting

and searching.