0% found this document useful (0 votes)
23 views19 pages

Stepanov-The Standard Template Library-1994

Uploaded by

maxsuelo53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views19 pages

Stepanov-The Standard Template Library-1994

Uploaded by

maxsuelo53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

mII HEWLBTT

a:aI.PACKARD

The Standard Template Library

Alexander Stepanov
MengLee
Hewlett Packard Laboratories
1501 Page Mill Road, Palo Alio, CA 94304
stepanovtehpl.hp.com & [email protected]
March 7, 1994

___J
The design principles:
1. Comprehensive
- take all the bestfrom APL, Lisp, Dylan, C library, USL Standard Components...
- provide structure andfill the gaps
2. Extensible
- orthogonality ofthe component space
- semantically based interoperability guarantees
3. Emcient
- no penalty for generality
- complexity guarantees at the interface level
4. Natural
- C/C++ machine model and programming paradigm
- support for built-in data types

/
The Standard Template Library 2 Fli;'HEWLETT
a:~ PACKARD
Component Classification:

• container - manages a set of memory locations


• iterator - provides a traversal protocol through a container
• algorithm - encapsulates a computational process
• applicative object - encapsulates a state (possibly empty) together with an
algorithm

The Standard Template LInry 3 Fli;' HEWLETT


a:~ PACKARD
Merge

int a[lOOO]i
int b(2000)i
int c[3000);

mergeCopy(a, a + 1000, b, b + 2000, C)i

Vector<int> Xi
List<int> Yi
int z [ ... )

aergeCopy (X. begin ( ), x. end ( ), y. begin ( ), y. end ( ), z) ;

4 rG'l HEWLETT
~~PACKARD
MergeCopy(l)

template <class Iteratorl, class Iterator2, class Resultlterator>


Resultlterator mergeCopy(Iteratorl first, Iteratorl last,
Iterator2 otherFirst, Iterator2 otherLast,
Resultlterator result) {
while (first != iast && otherFirst != otherLast)
if (*otherFirst < *first>
*result++ = *otherFirst++i
else
*result++ = *first++i
return copy(otherFirst, otherLast, copy(first, last, result»i
)

5 rG'll HEWLETT
~z.tI PACKARD
,
j
i

I
I
f
t
I

MergeCopy(2)

template <class Iteratorl, class Iterator2, class ResultIterator,


class Cc:.pare>
ResultIterator mergeCopy(Iteratorl first, Iteratorl last,
Iterator2 otherFirst, Iterator2 otherLast,
Resultlterator result, Compare comp) {
while (first != last && otherFirst != otherLast)
if (comp(*otherFirst, *first»
*result++ = *otherFirst++;
else
*result++ = *first++;
return copy (otherFirst, otherLast, copy(first, last, result»;
}

FJiiiW HEWLETT
~~PACKARD
Intmerge

'include <8tl.h>

main(int argc, char** argv) {


if (argc != 3) throw(-usage: intmerge filel file2\n-)i
merg8Copy(InputIterator<int>(ifstream(argv[l]», Inputlterator<int>(O),
InputIterator<int>(ifstream(argv[2]», Inputlterator<int>(O),
Outputlterator<int>(N\nN»i
}

The Standard Tempt•• 1 Ubraty 7 FGII HEWLE.TT


~~PACKARD
Deterministic sort: free reference implementation of sort

template <class BidirectionalIterator>


void deterministicSort{BidirectionalIterator first,
Bidirectionallterator last) {
while (nextPermutation(first, last»;
)

te.plate <class Iterator, class Compare>


void deterministicSort(Bidirectionallterator first,
Bidirectionallterator last, Compare comp) {
while (nextPermutation(first, last, comp»;
}

r~HEWLETT
The Standard T...... LInry 8
Ito. PACKARD
Partial sort
linclude <stl.h>

II prints n • -Jl••t integers fra. stdin


main(int argc, char·· argyl {
if (argc !== 2)throw("usage: partialsort number\n")i
Vector<int> v(size_t(atoi(argv[l]», O)i
copy(v.begin(),
partialSortCopy(Inputlterator<int>(), Inputlj:erator<int>(O),
v.begin(), v.end(»,
OUtputlterator<int>("\n"»i
}

The Standard Template Libnuy 9 rmHEWLETT


a:!&
PACKARD
Sort

'include <stl.h>
'include -string.H N

II .orts a file l-.ioographical1y


main(int argc, char**) {
if (argc != 1) throw(Nusage: sort\nN)i
Vector<String> Vi
copy(Inputlterator<String>(), Inputlterator<String>(O),
VectorlnsertIterator<String> (v, v.end(»)i
sort(v.begin(), v.end(»i
copy(v.begin(), v.end(), OUtputlterator<String>(»;
}

The Standard Template Ubrary 10 .rG'l HEWLETT


~gPACKARD
Sequence containers:

Vector:
random access
constant time (amortized) insert and erase at the end
List:
sequentialaccess
constant time insert and erase anywhere
Deque:
random access (but slower than Vector)
constant time insert and erase at the beginning .and the end

All three share the same interface

The Standard Template LIwaIy 11 ~HEWLETT


~~PACKARD
Taxonomy of iterators
Primary
• Forward iterator
• Bidirectional iterator
• Random access iterator

Additional
• Iterator, result iterator - weaker versions of forward iterators
• Insert iterator - special kind of result iterator
• Bidirectional reverse iterator, random access reverse iterator - reverse iterators

NOTE: ALL THESE ARE NOT CLASSES BUT CLASS REQUIREMENTS

The Standard Template Ubrary 12 FG'II HEWLETT


a:U PACKARD
Iterator template classes: °

-0,

Random access iteratton:


(pointer), ReversePointer, DequeIterator, Dequekeverselterator .
Bidirectionalltentors:
Lisdterator, ListReverseIterator
Iteraton:
Inpudterator
Result iteraton:
OutputIterator, VectorlnsertIterator, ListInsertIterator, DequeInsertIterator,
InsertPointer

The Standard Template Ubnuy 13 ~·HEWLETT


~~PACKARD
Applicative objects:

cout « Greater<int>() (27, 5);


prints: true

cout « GreaterX<int> (5) (27); II bind the second ar~l. .nt·

prints: true

cout « XGreater<int> (27) (5); II bind the first ar~lmeDt

prints: true

Example:

int a[10000]i

sort(a, a + 10000, Greater<int>(»; II sort in descending order

The Standard Template Ubrary 14 ~HEWLETT


~U11 PACKARD
Appli~ative template classes
Identity
BiDary arithmetic operations:
Plus, Minus, Times, Divides, Modulus (with X<op> and -cope-X)
... "i,;

Unary arithmetic operations: Negate


Binary relational operations:
Equal, NotEqual, Greater, Less, GreaterEqual, LessEqual (with Xcop> and
<OP>X)
Unary reladoaal operations: Not
Increment & decrement:
PrefixPlusplus, PostfixPlusplus, PrefixMinusminus, PostfixMinusminus
Assipment
Trivial predicates: .True, False

The Standard Template Ubrary 15 Fl3HEWLETT


. ·~~PACKARD
Algorithmic template functions:
Swap
Functions 011 ordered elements: min, max
General Itentlons: forBach, accumulate, innerProduct
Searching: find, adjacentFind, mismatch, equal, search, count
Order selectors: maxElement, minElement
LeDcographical comparisons: lexicographicalCompare
SeMdd.ng in sorted structures: lesserRange, greaterRange, equalRange, isMeniber
Transformers: copy, copyBackward, swapRanges, transform, transformCopy, replace,
replaceCopy, partialSum, partialSumCopy, adjacentDifference, adjacentDifferenceCopy,
fill, iota
.
Removers: remove, removeCopy, unique, uniqueCopy
Merging of sorted structures: merge, mergeCopy

The Standard Template Ubrary 16 . rJ.'I3.'D. HEWLETT


~~PACKARD
Set operations on sorted structures: includes, unionCopy, intersectionCopy,
differenceCopy, symmetricDifferenceCopy
Permutations: reverse, reverseCopy, rotate, rotateCopy, randomShuffie
Permutation genentors: nextPermutation, prevPermutation
Partitions: partition, stablePartition
Sorting: sort, stableSort, partialSort, partialSortCopy, select
Heap operations: pushHeap, popfIeap, makeHeap, sortHeap
List mutative operations: listRemove, listUnique, listMerge, listReverse, listSort

The Standard Template Ubrary 17 r!3HEWLETT


~~PACKARD
Memory management

template <class T>
T* allocate(size_t n, T*);

template <class T>


void·deallocate(T* buffer);
. ,
'
.
template <class T>
Pair<T*, size_t> getTemporaryBuffer(size_t n, T*);

template <class T>


void construct(T* p, const T& value);

template <class T>


void destroy(T* pointer);

template <class T>


void destroy(T* first, T* last);

The Standard Template Library 18 r~.HEWLETT


~~PACKARD
Conclusions:
- II

1. lIP position:
- make the library widely available
,
• ::~ reference implementation and validation suite are under consideration

2. The status of the library:


- fully implemented under HP C++ compiler
- tested, the full validation suite is under construction
- porting to other compilers is under way

The Standard Template LInry 19 FJ.3 HEWLETT


~~PACKARD

You might also like