Date post: | 02-Nov-2014 |
Category: |
Software |
Upload: | michael-heron |
View: | 73 times |
Download: | 4 times |
STANDARD TEMPLATE LIBRARIESMichael Heron
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.
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).
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.
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.
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
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.
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;
}
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.
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.
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.
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.
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;
}
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;
}
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.
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++.
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;
}
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);
};
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();
}
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.
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.
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.
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.