The C++ Standard Template Libraryschmidt/PDF/stl2.pdf · February 12, 2014 The C++ STL Douglas C....
Transcript of The C++ Standard Template Libraryschmidt/PDF/stl2.pdf · February 12, 2014 The C++ STL Douglas C....
Th
eC
++
Sta
nd
ard
Te
mp
late
Lib
rary
Do
ug
las
C.
Sch
mid
t
Pro
fesso
rD
ep
artm
en
to
fE
EC
S
d.s
ch
mid
t@va
nd
erb
ilt.ed
uV
an
de
rbilt
Un
ivers
ityw
ww
.dre
.van
de
rbilt.e
du
/∼
sch
mid
t/(6
15
)3
43
-81
97
Fe
bru
ary
12
,2
01
4
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Th
eC
++
Sta
nd
ard
Te
mp
late
Lib
rary
•W
ha
tis
ST
L?
•G
en
eric
Pro
gra
mm
ing
:W
hy
Use
ST
L?
•O
verv
iew
of
ST
Lco
nce
pts
&fe
atu
res
–e.g
.,h
elp
er
cla
ss
&fu
nctio
nte
mp
late
s,
co
nta
ine
rs,
itera
tors
,g
en
eric
alg
orith
ms,
fun
ctio
no
bje
cts
,a
da
pto
rs
•A
Co
mp
lete
ST
LE
xa
mp
le
•R
efe
ren
ce
sfo
rM
ore
Info
rma
tion
on
ST
L
Vanderb
iltU
nive
rsity
1
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Wh
at
isS
TL
?
Th
eS
tan
da
rdTe
mp
late
Lib
rary
pro
vid
es
ase
to
fw
ell
stru
ctu
red
ge
ne
ricC
++
co
mp
on
en
tsth
atw
ork
tog
eth
er
ina
se
am
les
sw
ay.
–A
lexa
nd
er
Ste
pa
nov
&M
en
gL
ee,T
he
Sta
nd
ard
Te
mp
late
Lib
rary
Vanderb
iltU
nive
rsity
2
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Wh
at
isS
TL
(co
nt’d
)?
•A
co
llectio
no
fco
mp
osa
ble
cla
ss
&fu
nctio
nte
mp
late
s
–H
elp
er
cla
ss
&fu
nctio
nte
mp
late
s:
op
era
tors
,p
air
–C
on
tain
er
&ite
rato
rcla
ss
tem
pla
tes
–G
en
eric
alg
orith
ms
tha
to
pe
rate
ove
rite
rato
rs
–F
un
ctio
no
bje
cts
–A
da
pto
rs
•E
na
ble
sg
en
eric
pro
gra
mm
ing
inC
++
–E
ach
ge
ne
rica
lgo
rithm
ca
no
pe
rate
ove
ra
ny
itera
tor
for
wh
ich
the
ne
ce
ssa
ryo
pe
ratio
ns
are
pro
vid
ed
–E
xte
nsib
le:
ca
nsu
pp
ort
new
alg
orith
ms,
co
nta
ine
rs,ite
rato
rs
Vanderb
iltU
nive
rsity
3
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ge
ne
ricP
rog
ram
min
g:
Wh
yU
se
ST
L?
•R
eu
se
:“w
ritele
ss
,d
om
ore
”
–S
TL
hid
es
co
mp
lex,
ted
iou
s&
erro
rp
ron
ed
eta
ils
–T
he
pro
gra
mm
er
ca
nth
en
focu
so
nth
ep
roble
ma
th
an
d–
Typ
e-s
afe
plu
gco
mp
atib
ilityb
etw
ee
nS
TL
co
mp
on
en
ts
•F
lex
ibility
–Ite
rato
rsd
eco
up
lea
lgo
rithm
sfro
mco
nta
ine
rs–
Un
an
ticip
ate
dco
mb
ina
tion
se
asily
su
pp
orte
d
•E
ffic
ien
cy
–Te
mp
late
savo
idvirtu
alfu
nctio
nove
rhe
ad
–S
trict
atte
ntio
nto
time
co
mp
lexity
ofa
lgo
rithm
s
Vanderb
iltU
nive
rsity
4
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LF
ea
ture
s:
Co
nta
ine
rs,
Itera
tors
,&
Alg
orith
ms
•C
on
tain
ers
–S
eq
ue
ntia
l:vector
,deque
,list
–A
sso
cia
tive:set
,multiset
,map
,multimap
–A
da
pte
rs:stack
,queue
,priorityqueue
•Ite
rato
rs
–In
pu
t,o
utp
ut,
forw
ard
,b
idire
ctio
na
l,&
ran
do
ma
cce
ss
–E
ach
co
nta
ine
rd
ecla
res
atra
itfo
rth
ety
pe
ofite
rato
rit
pro
vid
es
•G
en
eric
Alg
orith
ms
–M
uta
ting
,n
on
-mu
tatin
g,so
rting
,&
nu
me
ric
Vanderb
iltU
nive
rsity
5
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LC
on
tain
er
Ov
erv
iew
•S
TL
co
nta
ine
rsa
reA
bstra
ct
Da
taTyp
es
(AD
Ts)
•A
llco
nta
ine
rsa
rep
ara
me
teriz
ed
by
the
typ
e(s
)th
ey
co
nta
in
•E
ach
co
nta
ine
rd
ecla
res
vario
us
traits
–e.g
.,iterator
,constiterator
,valuetype
,e
tc.
•E
ach
co
nta
ine
rp
rovid
es
facto
rym
eth
od
sfo
rcre
atin
gite
rato
rs:
–begin()
/end()
for
trave
rsin
gfro
mfro
ntto
ba
ck
–rbegin()
/rend()
for
trave
rsin
gfro
mb
ack
tofro
nt
Vanderb
iltU
nive
rsity
6
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ty
pe
so
fS
TL
Co
nta
ine
rs
•T
he
rea
reth
ree
typ
es
ofco
nta
ine
rs
–S
eq
ue
ntia
lc
on
tain
ers
tha
ta
rran
ge
the
da
tath
ey
co
nta
inin
a
line
ar
ma
nn
er
∗E
lem
en
to
rde
rh
as
no
thin
gto
do
with
the
irva
lue
∗S
imila
rto
bu
iltina
rrays,
bu
tn
ee
dn
’tb
esto
red
co
ntig
uo
us
–A
ss
oc
iativ
ec
on
tain
ers
tha
tm
ain
tain
da
tain
stru
ctu
res
su
itable
for
fasta
sso
cia
tiveo
pe
ratio
ns
∗S
up
po
rtse
fficie
nt
op
era
tion
so
ne
lem
en
tsu
sin
gkeys
ord
ere
dbyoperator<
∗Im
ple
me
nte
da
sb
ala
nce
db
ina
rytre
es
–A
da
pte
rsth
atp
rovid
ed
iffere
ntw
ays
toa
cce
ss
se
qu
en
tial&
asso
cia
tiveco
nta
ine
rs
∗e.g
.,stack
,queue
,&priorityqueue
Vanderb
iltU
nive
rsity
7
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LV
ec
tor
Se
qu
en
tial
Co
nta
ine
r
•A
std
::ve
cto
ris
ad
yn
am
ic
arra
yth
atca
ngro
w&
sh
rink
atth
ee
nd
–e.g
.,it
pro
vid
es
(pre
—re
)allo
ca
tion
,in
dexe
dsto
rag
e,
pushback()
,
popback()
•S
up
po
rtsra
nd
om
acce
ss
itera
tors
•S
imila
rto
—bu
tm
ore
pow
erfu
lth
an
—bu
ilt-in
C/C
++
arra
ys
#include<iostream>
#include<vector>
#include<string>
intmain(intargc,char*argv[])
{
std::vector<std::string>projects;
std::cout<<"programname:"
<<argv[0]<<std::endl;
for(inti=
1;i<
argc;++i){
projects.push_back(argv[i]);
std::cout<<projects[i-1]
<<std::endl;
}return0;
}
Vanderb
iltU
nive
rsity
8
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LD
eq
ue
Se
qu
en
tial
Co
nta
ine
r
•A
std
::de
qu
e(p
ron
ou
nce
d
“de
ck”)
isa
do
uble
-en
de
d
qu
eu
e
•It
ad
ds
effi
cie
nt
inse
rtion
&
rem
ova
la
tth
eb
eg
inn
ing
&e
nd
ofth
ese
qu
en
ce
via
pushfront()
&
popfront()
#include<deque>
#include<iostream>
#include<iterator>
#include<algorithm>
intmain(){
std::deque<int>a_deck;
a_deck.push_back(3);
a_deck.push_front(1);
a_deck.insert(a_deck.begin()+1,
2);
a_deck[2]=0;
std::copy(a_deck.begin(),a_deck.end(),
std::ostream_iterator<int>
(std::cout,""));
return0;
}
Vanderb
iltU
nive
rsity
9
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LL
ist
Se
qu
en
tial
Co
nta
ine
r
•A
std
::list
ha
s
co
nsta
nttim
e
inse
rtion
&d
ele
tion
at
any
po
int
inth
e
se
qu
en
ce
(no
tju
sta
tth
eb
eg
inn
ing
&e
nd
)
–p
erfo
rma
nce
trad
e-o
ff:d
oe
sn
ot
offe
ra
ran
do
m
acce
ss
itera
tor
•Im
ple
me
nte
da
s
do
ubly
-linke
dlis
t
#include<list>
#include<iostream>
#include<iterator>
#include<string>
intmain(){
std::list<std::string>a_list;
a_list.push_back("banana");
a_list.push_front("apple");
a_list.push_back("carrot");
std::ostream_iterator<std::string>out_it
(std::cout,"\n");
std::copy(a_list.begin(),a_list.end(),out_it);
std::reverse_copy(a_list.begin(),a_list.end(),
out_it);
std::copy(a_list.rbegin(),a_list.rend(),out_it);
return0;
}
Vanderb
iltU
nive
rsity
10
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LA
ss
oc
iativ
eC
on
tain
er:
Se
t
•A
ns
td::s
et
isa
n
ord
ere
dco
llectio
n
ofu
niq
ue
keys
–e.g
.,a
se
to
f
stu
de
ntid
nu
mb
ers
#include<iostream>
#include<iterator>
#include<set>
intmain(){
std::set<int>myset;
for(inti
=1;i
<=5;i++)myset.insert(i*10);
std::pair<std::set<int>::iterator,bool>ret=
myset.insert(20);
assert(ret.second==
false);
intmyints[]=
{5,10,15};
myset.insert(myints,myints+3);
std::copy(myset.begin(),myset.end(),
std::ostream_iterator<int>(std::cout,"\n"));
return0;
}
Vanderb
iltU
nive
rsity
11
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LP
air
He
lpe
rC
las
s
•T
his
tem
pla
tegro
up
isth
e
ba
sis
for
themap
&set
asso
cia
tiveco
nta
ine
rsb
eca
use
itsto
res
(po
ten
tially
)
he
tero
ge
ne
ou
sp
airs
of
da
tato
ge
the
r
•A
pa
irb
ind
sa
key
(kn
ow
na
s
the
firs
te
lem
en
t)w
itha
na
sso
cia
ted
valu
e(k
now
na
sth
e
se
co
nd
ele
me
nt)
template<typenameT,typenameU>
structpair{
//
Datamembers
Tfirst;
Usecond;
//
Defaultconstructor
pair(){}
//
Constructorfromvalues
pair(constT&t,
constU&u)
:first(t),second(u){}
};
Vanderb
iltU
nive
rsity
12
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LP
air
He
lpe
rC
las
s(c
on
t’d)
//Pair
equivalence
comparisonoperator
template
<typenameT,
typenameU>
inline
bool
operator
==(constpair<T,
U>&lhs,
constpair<T,
U>&rhs)
{
return
lhs.first==
rhs.first&&
lhs.second
==rhs.second;
}//Pair
lessthancomparison
operator
template
<typenameT,
typenameU>
inline
bool
operator
<(constpair<T,
U>&lhs,
constpair<T,
U>&rhs)
{
return
lhs.first<
rhs.first||
(!(rhs.first
<lhs.first)
&&lhs.second
<rhs.second);
}Vanderb
iltU
nive
rsity
13
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LA
ss
oc
iativ
eC
on
tain
er:
Ma
p•
An
std
::ma
pa
sso
cia
tes
ava
lue
with
ea
ch
un
iqu
e
key
–a
stu
de
nt’s
idnu
mb
er
•Its
valuetype
is
imp
lem
en
ted
as
pair<const
Key,
Data>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
typedefstd::map<std::string,int>My_Map;
structprint{
voidoperator()(constMy_Map::value_type&p)
{std::cout<<p.second<<""
<<p.first<<std::endl;}
};
intmain(){
My_Mapmy_map;
for(std::stringa_word;
std::cin>>a_word;)
my_map[a_word]++;
std::for_each(my_map.begin(),
my_map.end(),print());
return0;
}
Vanderb
iltU
nive
rsity
14
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LA
ss
oc
iativ
eC
on
tain
er:
Mu
ltiSe
t&
Mu
ltiMa
p
•A
ns
td::m
ultis
et
or
an
std
::mu
ltima
pca
nsu
pp
ort
mu
ltiple
eq
uiva
len
t(n
on
-un
iqu
e)
keys
–e.g
.,stu
de
ntfirs
tn
am
es
or
last
na
me
s
•U
niq
ue
ne
ss
isd
ete
rmin
ed
by
an
eq
uiva
len
ce
rela
tion
–e.g
.,strncmp()
mig
ht
trea
tla
stn
am
es
tha
ta
red
istin
gu
ish
able
bystrcmp()
as
be
ing
the
sa
me
Vanderb
iltU
nive
rsity
15
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LA
ss
oc
iativ
eC
on
tain
er:
Mu
ltiSe
tE
xa
mp
le
#include<set>
#include<iostream>
#include<iterator>
intmain()
{
constintN=
10;
inta[N]=
{4,1,
1,1,1,1,0,
5,1,0};
intb[N]=
{4,4,
2,4,2,4,0,
1,5,5};
std::multiset<int>A(a,a+
N);
std::multiset<int>B(b,b+
N);
std::multiset<int>C;
std::cout<<"SetA:";
std::copy(A.begin(),A.end(),std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
std::cout<<"SetB:";
std::copy(B.begin(),B.end(),std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
Vanderb
iltU
nive
rsity
16
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LA
ss
oc
iativ
ec
on
tain
er:
Mu
ltiSe
tE
xa
mp
le(c
on
t’d)
std::cout<<"Union:";
std::set_union(A.begin(),A.end(),B.begin(),B.end(),
std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
std::cout<<"Intersection:";
std::set_intersection(A.begin(),A.end(),B.begin(),B.end(),
std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
std::set_difference(A.begin(),A.end(),B.begin(),B.end(),
std::inserter(C,C.end()));
std::cout<<"SetC(differenceofA
andB):";
std::copy(C.begin(),C.end(),std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
return0;
}Vanderb
iltU
nive
rsity
17
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LIte
rato
rO
ve
rvie
w
•S
TL
itera
tors
are
aC
++
imp
lem
en
tatio
no
fth
eIte
rato
rp
atte
rn
–T
his
pa
ttern
pro
vid
es
acce
ss
toth
ee
lem
en
tso
fa
na
ggre
ga
te
ob
jectse
qu
en
tially
with
ou
texp
osin
gits
un
de
rlyin
gre
pre
se
nta
tion
–A
nIte
rato
ro
bje
ct
en
ca
psu
late
sth
ein
tern
alstru
ctu
reo
fh
ow
the
itera
tion
occu
rs
•S
TL
itera
tors
are
ag
en
era
liza
tion
ofp
oin
ters
,i.e
.,th
ey
are
ob
jects
tha
tp
oin
tto
oth
er
ob
jects
•Ite
rato
rsa
reo
ften
use
dto
itera
teove
ra
ran
ge
ofo
bje
cts
:if
an
itera
tor
po
ints
too
ne
ele
me
nt
ina
ran
ge,
the
nit
isp
ossib
leto
incre
me
ntit
so
tha
tit
po
ints
toth
en
exte
lem
en
t
Vanderb
iltU
nive
rsity
18
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LIte
rato
rO
ve
rvie
w(c
on
t’d)
•Ite
rato
rsa
rece
ntra
lto
ge
ne
ricp
rogra
mm
ing
be
ca
use
they
are
an
inte
rface
be
twe
en
co
nta
ine
rs&
alg
orith
ms
–A
lgo
rithm
sty
pic
ally
take
itera
tors
as
arg
um
en
ts,so
aco
nta
ine
rn
ee
do
nly
pro
vid
ea
way
toa
cce
ss
itse
lem
en
tsu
sin
gite
rato
rs
–T
his
ma
ke
sit
po
ssib
leto
write
ag
en
eric
alg
orith
mth
ato
pe
rate
so
nm
any
diffe
ren
tkin
ds
of
co
nta
ine
rs,eve
nco
nta
ine
rsa
sd
iffere
nt
as
ave
cto
r&
ad
ou
bly
linke
dlis
t
Vanderb
iltU
nive
rsity
19
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Sim
ple
ST
LIte
rato
rE
xa
mp
le
#include
<iostream>
#include
<vector>
#include
<string>
intmain
(intargc,
char*argv[])
{
std::vector
<std::string>
projects;
//Names
oftheprojects
for(int
i=1;i
<argc;++i)
projects.push_back
(std::string
(argv[i]));
for(std::vector<std::string>::iterator
j=
projects.begin
();
j!=projects.end
();++j)
std::cout
<<*j
<<std::endl;
return
0;
}Vanderb
iltU
nive
rsity
20
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LIte
rato
rC
ate
go
ries
•Ite
rato
rca
teg
orie
sd
ep
en
do
nty
pe
pa
ram
ete
riza
tion
rath
er
tha
no
n
inh
erita
nce
:a
llow
sa
lgo
rithm
sto
op
era
tese
am
lessly
on
bo
thn
ative
(i.e.,
po
inte
rs)
&u
se
r-de
fin
ed
itera
tor
typ
es
•Ite
rato
rca
teg
orie
sa
reh
iera
rch
ica
l,w
ithm
ore
refin
ed
ca
teg
orie
sa
dd
ing
co
nstra
ints
tom
ore
ge
ne
ralo
ne
s
–F
orw
ard
itera
tors
are
bo
thin
pu
t&
ou
tpu
tite
rato
rs,
bu
tn
ota
llin
pu
to
ro
utp
utite
rato
rsa
refo
rwa
rdite
rato
rs
–B
idire
ctio
na
lite
rato
rsa
rea
llfo
rwa
rdite
rato
rs,
bu
tn
ota
llfo
rwa
rdite
rato
rsa
reb
idire
ctio
na
lite
rato
rs
–A
llra
nd
om
acce
ss
itera
tors
are
bid
irectio
na
lite
rato
rs,
bu
tn
ota
llb
idire
ctio
na
lite
rato
rsa
rera
nd
om
acce
ss
itera
tors
•N
ative
typ
es
(i.e.,
po
inte
rs)
tha
tm
ee
tth
ere
qu
irem
en
tsca
nb
eu
se
da
site
rato
rso
fva
riou
skin
ds
Vanderb
iltU
nive
rsity
21
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LIn
pu
tIte
rato
rs
•In
pu
tite
rato
rsa
reu
se
dto
rea
dva
lue
sfro
ma
se
qu
en
ce
•T
hey
may
be
de
refe
ren
ce
dto
refe
rto
so
me
ob
ject
&m
ay
be
incre
me
nte
dto
ob
tain
the
nextite
rato
rin
ase
qu
en
ce
•A
nin
pu
tite
rato
rm
ust
allo
wth
efo
llow
ing
op
era
tion
s
–C
opy
cto
r&
assig
nm
en
to
pe
rato
rfo
rth
atsa
me
itera
tor
typ
e
–O
pe
rato
rs=
=&
!=fo
rco
mp
aris
on
with
itera
tors
ofth
atty
pe
–O
pe
rato
rs*
(ca
nb
eco
nst)
&+
+(b
oth
pre
fix
&p
ostfi
x)
Vanderb
iltU
nive
rsity
22
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LIn
pu
tIte
rato
rE
xa
mp
le
//Fill
avectorwith
valuesread
fromstandard
input.
std::vector<int>
v;
for(istream_iterator<int>
i=cin;
i!=istream_iterator<int>
();
++i)
v.push_back
(*i);
//Fill
vectorwith
valuesreadfrom
stdinusing
std::copy()
std::vector<int>
v;
std::copy
(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter
(v));
Vanderb
iltU
nive
rsity
23
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LO
utp
ut
Itera
tors
•O
utp
ut
itera
tor
isa
typ
eth
at
pro
vid
es
am
ech
an
ism
for
sto
ring
(bu
t
no
tn
ece
ssa
rilya
cce
ssin
g)
ase
qu
en
ce
of
valu
es
•O
utp
ut
itera
tors
are
inso
me
se
nse
the
co
nve
rse
of
Inp
ut
Itera
tors
,bu
th
ave
afa
rm
ore
restric
tivein
terfa
ce
:
–O
pe
rato
rs=
&=
=&
!=n
ee
dn
otb
ed
efin
ed
(bu
tco
uld
be
)–
Mu
st
su
pp
ort
no
n-c
on
sto
pe
rato
r*
(e.g
.,*ite
r=
3)
•In
tuitive
ly,a
no
utp
utite
rato
ris
like
ata
pe
wh
ere
yo
uca
nw
ritea
valu
e
toth
ecu
rren
tlo
ca
tion
&yo
uca
na
dva
nce
toth
en
extlo
ca
tion
,bu
tyo
uca
nn
otre
ad
valu
es
&yo
uca
nn
otb
ack
up
or
rew
ind
Vanderb
iltU
nive
rsity
24
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LO
utp
ut
Itera
tor
Ex
am
ple
//Copy
afiletocout
viaaloop.
std::ifstream
ifile
("example_file");
inttmp;
while(ifile
>>tmp)
std::cout<<
tmp;
//Copy
afiletocout
viainput
&outputiterators
std::ifstream
ifile
("example_file");
std::copy
(std::istream_iterator<int>
(ifile),
std::istream_iterator<int>
(),
std::ostream_iterator<int>
(std::cout));
Vanderb
iltU
nive
rsity
25
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LF
orw
ard
Itera
tors
•F
orw
ard
itera
tors
mu
st
imp
lem
en
t(ro
ug
hly
)th
eu
nio
no
f
req
uire
me
nts
for
inp
ut&
ou
tpu
tite
rato
rs,
plu
sa
de
fau
ltcto
r
•T
he
diffe
ren
ce
from
the
inp
ut&
ou
tpu
tite
rato
rsis
tha
tfo
rtw
ofo
rwa
rdite
rato
rsr
&s
,r==s
imp
lies++r==++s
•A
diffe
ren
ce
toth
eo
utp
utite
rato
rsis
tha
toperator*
isa
lso
valid
on
the
left
sid
eo
foperator=
(*it
=v
isva
lid)
&th
atth
enu
mb
er
ofa
ssig
nm
en
tsto
afo
rwa
rdite
rato
ris
no
tre
stric
ted
Vanderb
iltU
nive
rsity
26
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LF
orw
ard
Itera
tor
Ex
am
ple
template
<typenameForwardIterator,
typenameT>
voidreplace
(ForwardIterator
first,
ForwardIterator
last,
const
T&old_value,
constT&new_value)
{
for
(;first!=last;
++first)
if
(*first==
old_value)*first
=new_value;
}//Iniitalize
3ints
todefaultvalue
1
std::vector<int>
v(3,
1);
v.push_back
(7);
//vector
v:1117
replace
(v.begin(),
v.end(),7,1);
assert
(std::find(v.begin(),
v.end(),
7)==v.end());
Vanderb
iltU
nive
rsity
27
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
idire
ctio
na
lIte
rato
rs
•B
idire
ctio
na
lite
rato
rsa
llow
alg
orith
ms
top
ass
thro
ug
hth
ee
lem
en
ts
forw
ard
&b
ackw
ard
•B
idire
ctio
na
lite
rato
rsm
ust
imp
lem
en
tth
ere
qu
irem
en
tsfo
rfo
rwa
rdite
rato
rs,
plu
sd
ecre
me
nto
pe
rato
rs(p
refix
&p
ostfi
x)
•M
any
ST
Lco
nta
ine
rsim
ple
me
nt
bid
irectio
na
lite
rato
rs
–e.g
.,list
,set
,multiset
,map
,&multimap
Vanderb
iltU
nive
rsity
28
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
idire
ctio
na
lIte
rato
rE
xa
mp
letemplate
<typenameBidirectionalIterator,
typename
Compare>
voidbubble_sort
(BidirectionalIterator
first,
BidirectionalIterator
last,
Compare
comp){
BidirectionalIterator
left_el=
first,right_el
=first;
++right_el;
while
(first!=last)
{
while
(right_el
!=last){
if
(comp(*right_el,
*left_el))
std::swap
(left_el,right_el);
++right_el;
++left_el;
}--last;
left_el
=first,
right_el=first;
++right_el;
}
}Vanderb
iltU
nive
rsity
29
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LR
an
do
mA
cc
es
sIte
rato
rs
•R
an
do
ma
cce
ss
itera
tors
allo
wa
lgo
rithm
sto
have
ran
do
ma
cce
ss
to
ele
me
nts
sto
red
ina
co
nta
ine
rth
atp
rovid
es
ran
do
ma
cce
ss
itera
tors
–e.g
.,vector
&deque
•R
an
do
ma
cce
ss
itera
tors
mu
st
imp
lem
en
tth
ere
qu
irem
en
tsfo
r
bid
irectio
na
lite
rato
rs,
plu
s:
–A
rithm
etic
assig
nm
en
to
pe
rato
rs+
=&
-=–
Op
era
tors
+&
-(m
ust
ha
nd
lesym
me
tryo
fa
rgu
me
nts
)
–O
rde
ring
op
era
tors
<&>
&<=
&>=
–S
ub
scrip
to
pe
rato
r[]
Vanderb
iltU
nive
rsity
30
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LR
an
do
mA
cc
es
sIte
rato
rE
xa
mp
le
std::vector<int>v
(1,1);
v.push_back(2);v.push_back(3);v.push_back(4);//vectorv:12
34
std::vector<int>::iteratori=
v.begin();
std::vector<int>::iteratorj=
i+
2;cout<<
*j<<"";
i+=
3;std::cout<<*i
<<"";
j=
i-
1;std::cout<<
*j<<"";
j-=
2;
std::cout<<
*j<<"";
std::cout<<
v[1]<<endl;
(j<
i)?std::cout<<"j<i"
:std::cout<<
"not(j<
i)";
std::cout<<
endl;
(j>
i)?std::cout<<"j>i"
:std::cout<<
"not(j>
i)";
std::cout<<
endl;
i=
j;
i<=
j&&j
<=i?
std::cout<<"i
&j
equal":
std::cout<<"i
&j
notequal";std::cout<<endl;
Vanderb
iltU
nive
rsity
31
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Imp
lem
en
ting
Itera
tors
Us
ing
ST
LP
atte
rns
•S
ince
aC
++
itera
tor
pro
vid
es
afa
milia
r,sta
nd
ard
inte
rface,a
tso
me
po
intyo
uw
illw
an
tto
ad
do
ne
toyo
ur
ow
ncla
sse
sso
yo
uca
n“p
lug
-&a
nd
-pla
yw
ithS
TL
alg
orith
ms
•W
riting
yo
ur
ow
nite
rato
rsis
astra
igh
tforw
ard
(alb
eit
ted
iou
s
pro
ce
ss,
with
on
lya
co
up
leo
fsu
btle
ties
yo
un
ee
dto
be
aw
are
of,
e.g
.,w
hic
hca
teg
ory
tosu
pp
ort,
etc
.
•S
om
eg
oo
da
rticle
so
nu
sin
g&
writin
gS
TL
itera
tors
ap
pe
ar
at
–http://www.oreillynet.com/pub/a/network/2005/10/
18/what-is-iterator-in-c-plus-plus.html
–http://www.oreillynet.com/pub/a/network/2005/11/
21/what-is-iterator-in-c-plus-plus-part2.html
Vanderb
iltU
nive
rsity
32
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LG
en
eric
Alg
orith
ms
•A
lgo
rithm
so
pe
rate
ove
rite
rato
rsra
the
rth
an
co
nta
ine
rs
•E
ach
co
nta
ine
rd
ecla
res
aniterator
&constiterator
as
atra
it
–vector
&deque
de
cla
rera
nd
om
acce
ss
itera
tors
–list
,map
,set
,multimap
,&multiset
de
cla
reb
idire
ctio
na
l
itera
tors
•E
ach
co
nta
ine
rd
ecla
res
facto
rym
eth
od
sfo
rits
itera
tor
typ
e:
–begin()
,end()
,rbegin()
,rend()
•C
om
po
sin
ga
na
lgo
rithm
with
aco
nta
ine
ris
do
ne
sim
ply
by
invo
kin
g
the
alg
orith
mw
ithite
rato
rsfo
rth
atco
nta
ine
r
•Te
mp
late
sp
rovid
eco
mp
ile-tim
ety
pe
sa
fety
for
co
mb
ina
tion
so
fco
nta
ine
rs,ite
rato
rs,
&a
lgo
rithm
s
Vanderb
iltU
nive
rsity
33
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ca
teg
oriz
ing
ST
LG
en
eric
Alg
orith
ms
•T
he
rea
reva
riou
sw
ays
toca
teg
oriz
eS
TL
alg
orith
ms,e.g
.:
–N
on
-mu
tatin
g,
wh
ich
op
era
teu
sin
ga
ran
ge
ofite
rato
rs,
bu
td
on
.tch
an
ge
the
da
tae
lem
en
tsfo
un
d
–M
uta
ting
,w
hic
ho
pe
rate
usin
ga
ran
ge
ofite
rato
rs,
bu
tca
n
ch
an
ge
the
ord
er
ofth
ed
ata
ele
me
nts
–S
ortin
g&
se
ts,
wh
ich
so
rto
rse
arc
he
sra
ng
es
of
ele
me
nts
&a
ct
on
so
rted
ran
ge
sby
testin
gva
lue
s–
Nu
me
ric,
wh
ich
are
mu
tatin
ga
lgo
rithm
sth
atp
rod
uce
nu
me
ric
resu
lts
•In
ad
ditio
nto
the
se
ma
inty
pe
s,
the
rea
resp
ecifi
ca
lgo
rithm
sw
ithin
ea
ch
typ
eth
ata
cce
pta
pre
dic
ate
co
nd
ition
–P
red
ica
ten
am
es
en
dw
ithth
eif
su
ffix
tore
min
du
sth
atth
ey
req
uire
an
“if”te
st.s
resu
lt(tru
eo
rfa
lse
),a
sa
na
rgu
me
nt;
the
se
ca
nb
eth
ere
su
lto
ffu
ncto
rca
lls
Vanderb
iltU
nive
rsity
34
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Be
ne
fits
of
ST
LG
en
eric
Alg
orith
ms
•S
TL
alg
orith
ms
are
de
co
up
led
from
the
pa
rticu
lar
co
nta
ine
rsth
ey
op
era
teo
n&
are
inste
ad
pa
ram
ete
rize
dby
itera
tors
•A
llco
nta
ine
rsw
ithth
esa
me
itera
tor
typ
eca
nu
se
the
sa
me
alg
orith
ms
•S
ince
alg
orith
ms
are
writte
nto
wo
rko
nite
rato
rsra
the
rth
an
co
mp
on
en
ts,th
eso
ftwa
red
eve
lop
me
nte
ffort
isd
rastic
ally
red
uce
d
–e.g
.,in
ste
ad
ofw
riting
ase
arc
hro
utin
efo
re
ach
kin
do
fco
nta
ine
r,
on
eo
nly
write
on
efo
re
ach
itera
tor
typ
e&
ap
ply
ita
ny
co
nta
ine
r.
•S
ince
diffe
ren
tco
mp
on
en
tsca
nb
ea
cce
sse
dby
the
sa
me
itera
tors
,ju
sta
few
vers
ion
so
fth
ese
arc
hro
utin
em
ust
be
imp
lem
en
ted
Vanderb
iltU
nive
rsity
35
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::fin
d()
Alg
orith
mR
etu
rns
afo
rwa
rdite
rato
rp
ositio
ne
da
tth
efirs
te
lem
en
tin
the
give
nse
qu
en
ce
ran
ge
tha
tm
atc
he
sa
pa
sse
dva
lue
#include<vector>
#include<algorithm>
#include<assert>
#include<string>
intmain(intargc,char*argv[]){
std::vector<std::string>projects;
for(inti
=1;i
<argc;++i)
projects.push_back(std::string(argv[i]));
std::vector<std::string>::iteratorj
=
std::find(projects.begin(),projects.end(),std::string("Lab8"));
if
(j==projects.end())return1;
assert((*j)==std::string("Lab8"));
return0;
}Vanderb
iltU
nive
rsity
36
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::fin
d()
Alg
orith
m(c
on
t’d)
ST
La
lgo
rithm
sca
nw
ork
on
bo
thbu
ilt-in&
use
r-de
fin
ed
typ
es
inta[]=
{10,30,20,15};
int*ibegin=a;
int*iend=
a+
(sizeof(a)/sizeof(*a));
int*iter=
std::find(ibegin,iend,10);
if(iter==iend)
std::cout<<"10notfound\n";
else
std::cout<<*iter<<"found\n";
intA[]=
{10,30,20,15};
std::set<int>int_set
(A,A+
(sizeof(A)/sizeof(*A)));
std::set<int>::iteratoriter=
//int_set.find(10)willbefaster!
std::find(int_set.begin(),
int_set.end(),10);
if
(iter==int_set.end())
std::cout<<"10notfound\n";
else
std::cout<<*iter<<"found\n";
Vanderb
iltU
nive
rsity
37
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
std
::ad
jac
en
tfi
nd
()A
lgo
rithm
Re
turn
sth
efirs
tite
rato
ri
su
ch
tha
ti
&i
+1
are
bo
thva
lidite
rato
rsin[first,
last)
,&
su
ch
tha
t*i==*(i+1)
orbinarypred
(*i,
*(i+1))
istru
e(it
retu
rnslast
ifn
osu
ch
itera
tor
exis
ts)
//Find
thefirstelement
thatis
greaterthan
itssuccessor:
intA[]
={1,2,3,
4,6,5,7,8};
constint
N=sizeof(A)
/sizeof(int);
constint
*p=std::adjacent_find(A,
A+N,std::greater<int>());
std::cout
<<"Element
"<<p-A
<<"isoutof
order:"
<<*p<<"
>"<<*(p+
1)<<"."<<
std::endl;
Vanderb
iltU
nive
rsity
38
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::co
py
()A
lgo
rithm
Co
pie
se
lem
en
tsfro
ma
inp
utite
rato
rse
qu
en
ce
ran
ge
into
an
ou
tpu
tite
rato
r
std::vector<int>
v;
std::copy
(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter
(v));
std::copy
(v.begin(),
v.end(),
std::ostream_iterator<int>
(std::cout));
Vanderb
iltU
nive
rsity
39
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::fill()
Alg
orith
m
Assig
na
valu
eto
the
ele
me
nts
ina
se
qu
en
ce
inta[10];
std::fill
(a,a+10,
100);
std::fill_n
(a,10,
200);
std::vector<int>
v(10,
100);
std::fill
(v.begin(),
v.end(),
200);
std::fill_n
(v.begin
(),v.size(),
200);
Vanderb
iltU
nive
rsity
40
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::rep
lac
e()
Alg
orith
m
Re
pla
ce
sa
llin
sta
nce
so
fa
give
nexis
ting
valu
ew
itha
give
nn
ew
valu
e,
with
ina
give
nse
qu
en
ce
ran
ge
std::vector<int>
v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(1);
std::replace
(v.begin
(),v.end(),
1,99);
assert
(V[0]==99&&
V[3]==99);
Vanderb
iltU
nive
rsity
41
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::rem
ov
e()
Alg
orith
mR
em
ove
sfro
mth
era
ng
e[first,
last)
the
ele
me
nts
with
ava
lue
eq
ua
lto
valu
e&
retu
rns
an
itera
tor
toth
en
ew
en
do
fth
era
ng
e,
wh
ich
now
inclu
de
so
nly
the
valu
es
no
te
qu
alto
valu
e
#include<iostream>
#include<algorithm>
#include<iterator>
intmain(){
intmyints[]={10,20,30,30,20,10,10,20};
int*pbegin=
myints,*pend=myints+sizeofmyints/
sizeof*myints;
std::cout<<"originalarraycontains:";
std::copy(pbegin,pend,std::ostream_iterator<int>(std::cout,"
"));
int*nend=std::remove(pbegin,pend,20);
std::cout<<"\nrangecontains:";
std::copy(pbegin,nend,std::ostream_iterator<int>(std::cout,"
"));
std::cout<<"\ncompletearraycontains:";
std::copy(pbegin,pend,std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
return0;
}Vanderb
iltU
nive
rsity
42
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::rem
ov
eif()
Alg
orith
mR
em
ove
sfro
mth
era
ng
e[first,
last)
the
ele
me
nts
for
wh
ich
pre
da
pp
lied
toits
valu
eis
true,
&re
turn
sa
nite
rato
rto
the
new
en
do
fth
era
ng
e,w
hic
hn
ow
inclu
de
so
nly
the
valu
es
for
wh
ich
pre
dw
as
fals
e.
#include<iostream>
#include<algorithm>
structis_odd{
//Couldalsobea
C-stylefunction.
booloperator()(inti){return(i%2)==1;}
};
intmain(){
intmyints[]={1,2,
3,4,5,6,7,
8,9};
int*pbegin=
myints;
int*pend=myints+
sizeofmyints/
sizeof*myints;
pend=
std::remove_if(pbegin,pend,is_odd());
std::cout<<"rangecontains:";
std::copy(pbegin,pend,std::ostream_iterator<int>(std::cout,"
"));
std::cout<<std::endl;
return0;
}Vanderb
iltU
nive
rsity
43
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::tran
sfo
rm()
Alg
orith
m
Sca
ns
ara
ng
e&
for
ea
ch
use
afu
nctio
nto
ge
ne
rate
an
ew
ob
jectp
ut
ina
se
co
nd
co
nta
ine
ro
rta
ke
stw
oin
terva
ls&
ap
plie
sa
bin
ary
op
era
tion
toite
ms
tog
en
era
tea
new
co
nta
ine
r
#include<iostream>
#include<algorithm>
#include<ctype.h>
#include<functional>
classto_lower{
public:
charoperator()(charc)const
{
returnisupper(c)
?tolower(c):c;
}
};
std::stringlower(conststd::string&str){
std::stringlc;
std::transform(str.begin(),str.end(),
std::back_inserter(lc),
to_lower());
returnlc;
}intmain(){
std::strings="HELLO";
std::cout<<s
<<std::endl;
s=
lower(s);
std::cout<<s
<<std::endl;
}
Vanderb
iltU
nive
rsity
44
The
C+
+S
TL
Dougla
sC
.S
chm
idt
An
oth
er
Ex
am
ple
of
std
::tran
sfo
rm()
Alg
orith
m
#include<iostream>
#include<algorithm>
#include<functional>
#include<numeric>
#include<vector>
#include<iterator>
intmain(){
std::vector<float>v
(5,1);//
avectorof5
floatsallinitializedto1.0.
std::partial_sum(v.begin(),v.end(),v.begin());
std::transform(v.begin(),v.end(),v.begin(),
v.begin(),std::multiplies<float>());
std::copy(v.begin(),v.end(),std::ostream_iterator<float>(std::cout,"\n"));
std::transform(v.begin(),v.end(),v.begin(),
std::bind2nd(std::divides<float>(),3));
std::copy(v.begin(),v.end(),std::ostream_iterator<float>(std::cout,"\n"));
return0;
}Vanderb
iltU
nive
rsity
45
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Ex
am
ple
of
std
::for
ea
ch
()A
lgo
rithm
Ap
plie
sth
efu
nctio
no
bje
ctf
toe
ach
ele
me
ntin
the
ran
ge[first,
last)
;f
’sre
turn
valu
e,if
any,
isig
no
red
template<classT>
structprint{
print(std::ostream&out):os_(out),count_(0){}
voidoperator()(constT
&t){os<<
t<<’
’;++count_;}
std::ostream&os_;
intcount_;
};
intmain(){
intA[]=
{1,4,2,8,5,7};
constintN=
sizeof(A)/sizeof(int);
//
for_each()returnsfunctionobjectafterbeingappliedtoeachelement
print<int>f=std::for_each(A,A+
N,print<int>(std::cout));
std::cout<<std::endl<<f.count_<<"
objectsprinted."<<std::endl;
}Vanderb
iltU
nive
rsity
46
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LF
un
ctio
nO
bje
cts
•F
un
ctio
no
bje
cts
(aka
fun
cto
rs)
de
cla
re&
de
fin
eoperator()
•S
TL
pro
vid
es
he
lpe
rb
ase
cla
ss
tem
pla
tesunaryfunction
&
binaryfunction
tofa
cilita
teu
se
r-de
fin
ed
fun
ctio
no
bje
cts
•S
TL
pro
vid
es
anu
mb
er
ofco
mm
on
-use
fun
ctio
no
bje
ctcla
ss
tem
pla
tes:
–A
rithm
etic
:plus
,minus
,times
,divides
,modulus
,negate
–c
om
pa
riso
n:equalto
,notequalto
,greater
,less
,
greaterequal
,lessequal
–lo
gic
al:logicaland
,logicalor
,logicalnot
•A
nu
mb
er
of
ST
Lg
en
eric
alg
orith
ms
ca
nta
ke
ST
L-p
rovid
ed
or
use
r-de
fin
ed
fun
ctio
no
bje
ct
arg
um
en
tsto
exte
nd
alg
orith
mb
eh
avio
r
Vanderb
iltU
nive
rsity
47
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LF
un
ctio
nO
bje
cts
Ex
am
ple
#include<vector>
#include<algorithm>
#include<iterator>
#include<functional>
#include<string>
intmain(intargc,char*argv[])
{
std::vector<std::string>projects;
for(inti
=0;i
<argc;++i)
projects.push_back(std::string(argv[i]));
//
Sortin
descendingorder:noteexplicitctorforgreater
std::sort(projects.begin(),projects.end(),
std::greater<std::string>());
return0;
}Vanderb
iltU
nive
rsity
48
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LA
da
pto
rs
•S
TL
ad
ap
tors
imp
lem
en
tth
eA
da
pte
rd
esig
np
atte
rn
–i.e
.,th
ey
co
nve
rto
ne
inte
rface
into
an
oth
er
inte
rface
clie
nts
exp
ect
•C
on
tain
er
ad
ap
tors
inclu
destack
,queue
,priorityqueue
•Ite
rato
ra
da
pto
rsin
clu
dereverseiterators
&backinserter()
itera
tors
•F
un
ctio
na
da
pto
rsin
clu
de
ne
ga
tors
&b
ind
ers
•S
TL
ad
ap
tors
ca
nb
eu
se
dto
na
rrow
inte
rface
s(e
.g.,
astack
ad
ap
tor
forvector
)
Vanderb
iltU
nive
rsity
49
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LC
on
tain
er
Ad
ap
tors
•T
hestack
co
nta
ine
ra
da
pto
ris
an
ide
alch
oic
ew
he
no
ne
ne
ed
to
use
a“L
astIn
,F
irst
Ou
t”(L
IFO
)d
ata
stru
ctu
rech
ara
cte
rize
dby
havin
ge
lem
en
tsin
se
rted
&re
move
dfro
mth
esa
me
en
d
•T
hequeue
co
nta
ine
ra
da
pto
ris
a“F
irst
In,F
irst
Ou
t”(F
IFO
)d
ata
stru
ctu
rech
ara
cte
rize
dby
havin
ge
lem
en
tsin
se
rted
into
on
ee
nd
&re
move
dfro
mth
eo
the
re
nd
•T
hepriorityqueue
assig
ns
ap
riority
toeve
rye
lem
en
tth
atit
sto
res
–N
ew
ele
me
nts
are
ad
de
dto
the
qu
eu
eu
sin
gth
epush()
fun
ctio
n,
just
as
with
aqueue
–H
ow
eve
r,its
pop()
fun
ctio
ng
ets
ele
me
nt
with
the
hig
he
stp
riority
Vanderb
iltU
nive
rsity
50
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
Ls
tack
&q
ue
ue
Co
nta
ine
rA
da
pto
rD
efi
nitio
ns
template<typenameT,
typenameST=
deque<T>>
classstack
{public:
explicitstack(constST&c=
ST());
boolempty()const;
size_typesize()const;
value_type&top();
constvalue_type&top()const;
voidpush(constvalue_type&t);
voidpop();
private:
STcontainer_;
//.
};
template<typenameT,
typenameQ
=deque<T>>
classqueue
{public:
explicitqueue(constQ&c
=Q());
boolempty()const;
size_typesize()const;
value_type&front();
constvalue_type&front()const;
value_type&back();
constvalue_type&back()const;
voidpush(constvalue_type&t);
voidpop();
private:
Qcontainer_;
//.
};
Vanderb
iltU
nive
rsity
51
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
Ls
tack
&q
ue
ue
Co
nta
ine
rA
da
pto
rE
xa
mp
les
//STLstack
#include<iostream>
#include<stack>
intmain()
{
std::stack<char>st;
st.push(’A’);
st.push(’B’);
st.push(’C’);
st.push(’D’);
for(;
!st.empty();st.pop()){
cout<<"\nPopping:";
cout<<st.top();
}return0;
}
//STLqueue
#include<iostream>
#include<queue>
#include<string>
intmain()
{
std::queue<string>q;
std::cout<<"Pushingonetwothree\n";
q.push("one");
q.push("two");
q.push("three");
for(;!q.empty();q.pop()){
std::cout<<"\nPopping";
std::cout<<q.front();
}return0;
}
Vanderb
iltU
nive
rsity
52
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
Lp
riority
qu
eu
eC
on
tain
er
Ad
ap
tor
Ex
am
ple
#include<queue>//
priority_queue
#include<string>
#include<iostream>
structPlace{
unsignedintdist;
std::stringdest;
Place(conststd::stringdt,size_tds):dist(ds),dest(dt){}
booloperator<(constPlace&right)const{
returndist<
right.dist;}
};
std::ostream&operator<<(std::ostream&os,constPlace&p)
{returnos<<p.dest<<"
"<<p.dist;}
intmain(){
std::priority_queue<Place>pque;
pque.push(Place("Poway",
10));
pque.push(Place("ElCajon",
20));
pque.push(Place("LaJolla",3));
for(;
!pque.empty();pque.pop())std::cout<<pque.top()<<
std::endl;
return0;
}Vanderb
iltU
nive
rsity
53
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LIte
rato
rA
da
pto
rs
•S
TL
alg
orith
ms
tha
tco
py
ele
me
nts
are
pa
sse
da
nite
rato
rth
at
ma
rks
the
po
sitio
nw
ithin
aco
nta
ine
rto
be
gin
co
pyin
g
–e.g
.,copy()
,uniquecopy()
,copybackwards()
,
removecopy()
,&replacecopy()
•W
ithe
ach
ele
me
ntco
pie
d,th
eva
lue
isa
ssig
ne
d&
the
itera
tor
is
incre
me
nte
d
•E
ach
co
py
req
uire
sth
eta
rge
tco
nta
ine
ris
ofa
su
fficie
ntsiz
eto
ho
ldth
ese
to
fa
ssig
ne
de
lem
en
ts
•W
eca
nu
se
itera
tor
ad
ap
ters
toexp
an
dth
eco
nta
ine
rsa
sw
e
pe
rform
the
alg
orith
m
–S
tart
with
an
em
pty
co
nta
ine
r,&
use
the
inse
rter
alo
ng
with
the
alg
orith
ms
tom
ake
the
co
nta
ine
rgro
wo
nly
as
ne
ed
ed
Vanderb
iltU
nive
rsity
54
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
Lb
ack
ins
erte
r()Ite
rato
rA
da
pto
rE
xa
mp
le
•backinserter()
ca
use
s
the
co
nta
ine
r’spushback()
op
era
tor
tob
ein
voke
din
pla
ce
ofth
ea
ssig
nm
en
to
pe
rato
r
•T
he
arg
um
en
tp
asse
dto
backinserter()
isth
e
co
nta
ine
rits
elf
//
Fillvectorwith
valuesread
//
fromstdinusing
std::copy()
std::vector<int>
v;
std::vector<int>::iterator
in_begin
=
std::istream_iterator<int>(std::cin)
std::vector<int>::iterator
in_end
=
std::istream_iterator<int>(),
std::copy
(in_begin,
in_end,
std::back_inserter
(v));
Vanderb
iltU
nive
rsity
55
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LF
un
ctio
nA
da
pto
rs
•S
TL
ha
sp
red
efin
ed
fun
cto
ra
da
pto
rsth
atw
illch
an
ge
the
irfu
ncto
rs
so
tha
tth
ey
ca
n:
–P
erfo
rmfu
nctio
nco
mp
ositio
n&
bin
din
g–
Allo
wfe
we
rcre
ate
dfu
ncto
rs
•T
he
se
fun
cto
rsa
llow
on
eto
co
mb
ine,
tran
sfo
rmo
rm
an
ipu
late
fun
cto
rsw
ithe
ach
oth
er,
ce
rtain
valu
es
or
with
sp
ecia
lfu
nctio
ns
•S
TL
fun
ctio
na
da
pte
rsin
clu
de
–B
ind
ers
(bind1st()
&bind2nd()
)b
ind
on
eo
fth
eir
arg
um
en
ts
–N
eg
ato
rs(not1
¬2
)a
da
ptfu
ncto
rsby
ne
ga
ting
arg
um
en
ts–
Me
mb
er
fun
ctio
ns
(ptrfun
&memfun
)a
llow
fun
cto
rsto
be
cla
ss
me
mb
ers
Vanderb
iltU
nive
rsity
56
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
ind
er
Fu
nc
tion
Ad
ap
tor
•A
bin
de
rca
nb
eu
se
dto
tran
sfo
rma
bin
ary
fun
cto
rin
toa
nu
na
ryo
ne
by
actin
ga
sa
co
nve
rter
be
twe
en
the
fun
cto
r&
an
alg
orith
m
•B
ind
ers
alw
ays
sto
reb
oth
the
bin
ary
fun
cto
r&
the
arg
um
en
tin
tern
ally
(the
arg
um
en
tis
pa
sse
da
so
ne
of
the
arg
um
en
tso
fth
e
fun
cto
reve
rytim
eit
isca
lled
)
–bind1st(Op,
Arg)
ca
lls’O
p’w
ith’A
rg’a
sits
firs
tp
ara
me
ter
–bind2nd(Op,
Arg)
ca
lls’O
p’w
ith’A
rg’a
sits
se
co
nd
pa
ram
ete
r
Vanderb
iltU
nive
rsity
57
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
ind
er
Fu
nc
tion
Ad
ap
tor
Ex
am
ple
1
#include<vector>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<functional>
intmain(intargc,char*argv[]){
std::vector<int>v(10,2);
std::partial_sum(v.begin(),v.end(),v.begin());
std::random_shuffle(v.begin(),v.end());
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,"\n"));
std::cout<<"numbergreaterthan10
="
<<count_if(v.begin(),v.end(),
std::bind2nd(std::greater<int>(),10))<<std::endl;
return0;
}Vanderb
iltU
nive
rsity
58
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
ind
er
Fu
nc
tion
Ad
ap
tor
Ex
am
ple
2#include<vector>
#include<iostream>
#include<algorithm>
#include<iterator>
#include<functional>
#include<cstdlib>
#include<ctime>
intmain(intargc,char*argv[]){
srand(time(0));
std::vector<int>v,v2(10,20);
std::generate_n(std::back_inserter(v),10,rand);
std::transform(v.begin(),v.end(),v2.begin(),v.begin(),std::modulus<int>());
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,"\n"));
std::cout<<std::endl;
intfactor=2;
std::transform(v.begin(),v.end(),
v.begin(),std::bind2nd(std::multiplies<int>(),factor));
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,"\n"));
return0;
}Vanderb
iltU
nive
rsity
59
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
ind
er
Fu
nc
tion
Ad
ap
tor
Ex
am
ple
3T
his
exa
mp
lere
move
ssp
ace
sin
astrin
gth
atu
se
sth
eequalto
an
dbind2nd
fun
cto
rsto
pe
rform
removeif
wh
enequalto
fin
ds
abla
nk
ch
ar
inth
estrin
g
#include<iostream>
#include<string>
intmain(){
std::strings
="spacesintext";
std::cout<<s<<
std::endl;
std::string::iteratornew_end=
std::remove_if(s.begin(),s.end(),std::bind2nd(std::equal_to<char>(),’’));
//
remove_if()justmovesunwantedelementstotheendandreturnsan
iterator
//
tothefirstunwantedelementsinceit’.sa
genericalgorithm&doesn’t"know"
//
whetherthecontainerbechanged.
s.erase()*does*knowthis,however..
s.erase(new_end,s.end());
std::cout<<s<<
std::endl;
return0;
}Vanderb
iltU
nive
rsity
60
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LB
ind
er
Fu
nc
tion
Ad
ap
tor
Ex
am
ple
4#include<vector>
#include<algorithm>
#include<functional>
#include<iostream>
#include<iterator>
intmain()
{//Contrastsstd::remove_if()&
erase().
std::vector<int>v;
v.push_back(1);v.push_back(4);v.push_back(2);
v.push_back(8);v.push_back(5);v.push_back(7);
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,""));
intsum=
std::count_if(v.begin(),v.end(),
std::bind2nd(std::greater<int>(),5));
std::cout<<"\nThereare"
<<sum<<"number(s)greaterthan5"<<
std::endl;
std::vector<int>::iteratornew_end=//"remove"alltheelementslessthan4.
std::remove_if(v.begin(),v.end(),std::bind2nd(std::less<int>(),4));
v.erase(new_end,v.end());
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,"
"));
std::cout<<"\nElementslessthan4
removed"<<std::endl;
return0;
}Vanderb
iltU
nive
rsity
61
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LN
eg
ato
rA
da
pte
rs&
Fu
nc
tion
Ad
ap
tors
•A
ne
ga
tor
ca
nb
eu
se
dto
sto
reth
eo
pp
osite
resu
lto
fa
fun
cto
r
–not1(Op)
ne
ga
tes
the
resu
lto
fu
na
ry’O
p’
–not2(Op)
ne
ga
tes
resu
lto
fb
ina
ry’O
p’
•A
me
mb
er
fun
ctio
n&
po
inte
r-to-fu
nctio
na
da
pte
rca
nb
eu
se
dto
allo
wcla
ss
me
mb
er
fun
ctio
ns
or
C-s
tyle
fun
ctio
ns
as
arg
um
en
tsto
ST
Lp
red
efin
ed
alg
orith
ms
–memfun(PtrToMember
mf)
co
nve
rtsa
po
inte
rto
me
mb
er
toa
fun
cto
rw
ho
se
firs
ta
rgis
ap
oin
ter
toth
eo
bje
ct
–ptrfun()
co
nve
rtsa
po
inte
rto
afu
nctio
n&
turn
sit
into
a
fun
cto
r
Vanderb
iltU
nive
rsity
62
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LN
eg
ato
rF
un
ctio
nA
da
pto
rE
xa
mp
le#include<vector>
#include<iostream>
#include<iterator>
#include<algorithm>
#include<functional>
intmain(){
std::vector<int>v1;
v1.push_back(1);v1.push_back(2);v1.push_back(3);v1.push_back(4);
std::vector<int>v2;
std::remove_copy_if(v1.begin(),v1.end(),std::back_inserter(v2),
std::bind2nd(std::greater<int>(),3));
std::copy(v2.begin(),v2.end(),
std::ostream_iterator<int>(std::cout,"\n"));
std::vector<int>v3;
std::remove_copy_if(v1.begin(),v1.end(),std::back_inserter(v3),
std::not1(std::bind2nd(std::greater<int>(),3)));
std::copy(v3.begin(),v3.end(),
std::ostream_iterator<int>(std::cout,"\n"));
return0;
}Vanderb
iltU
nive
rsity
63
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LP
oin
ter-to
-Me
mF
un
Ad
ap
tor
Ex
am
ple
classWrapInt{
public:
WrapInt():val_(0){}
WrapInt(intx):val_(x){}
voidshowval(){
std::cout<<val_<<"
";
}boolis_prime(){
for(inti=2;
i<=(val_/2);i++)
if
((val_%i)==
0)
returnfalse;
returntrue;
}
private:
intval_;
};
Vanderb
iltU
nive
rsity
64
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LP
oin
ter-to
-Me
mF
un
Ad
ap
tor
Ex
am
ple
(co
nt’d
)intmain(){
std::vector<WrapInt>v(10);
for(inti
=0;i
<10;i++)
v[i]=WrapInt(i+1);
std::cout<<"Sequencecontains:";
std::for_each(v.begin(),v.end(),
std::mem_fun_ref(&WrapInt::showval));
std::cout<<std::endl;
std::vector<WrapInt>::iteratorend_p=//removetheprimes
std::remove_if(v.begin(),v.end(),
std::mem_fun_ref(&WrapInt::is_prime));
std::cout<<"Sequenceafterremovingprimes:";
for_each(v.begin(),end_p,std::mem_fun_ref(&WrapInt::showval));
std::cout<<std::endl;
return0;
}Vanderb
iltU
nive
rsity
65
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LP
oin
ter-to
-Fu
nc
tion
Ad
ap
tor
Ex
am
ple
#include<vector>
#include<iostream>
#include<iterator>
#include<algorithm>
#include<functional>
intmain(){
std::vector<char*>v;
v.push_back("One");v.push_back("Two");v.push_back("Three");v.push_back("Four");
std::cout<<"Sequencecontains:";
std::copy(v.begin(),v.end(),std::ostream_iterator<char*>
(std::cout,"
"));
std::cout<<std::endl<<"SearchingforThree.\n";
std::vector<char*>::iteratorit
=std::find_if(v.begin(),v.end(),
std::not1(std::bind2nd(std::ptr_fun(strcmp),"Three")));
if
(it!=v.end()){
std::cout<<"Foundit!Hereistherestofthestory:";
std::copy(it,v.end(),std::ostream_iterator<char*>(std::cout,"\n"));
}return0;
}Vanderb
iltU
nive
rsity
66
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LU
tilityO
pe
rato
rs
template
<typenameT,
typenameU>
inline
bool
operator
!=(constT&
t,constU&
u)
{
return
!(t==u);
}template
<typenameT,
typenameU>
inline
bool
operator
>(constT&
t,constU&
u)
{
return
u<t;
}Vanderb
iltU
nive
rsity
67
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LU
tilityO
pe
rato
rs(c
on
t’d)
template
<typenameT,
typenameU>
inline
bool
operator
<=(constT&
t,constU&
u)
{
return
!(u<t);
}template
<typenameT,
typenameU>
inline
bool
operator
>=(constT&
t,constU&
u)
{
return
!(t<u);
}Vanderb
iltU
nive
rsity
68
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LU
tilityO
pe
rato
rs(c
on
t’d)
•Q
ue
stio
n:
why
req
uire
tha
tp
ara
me
teriz
ed
typ
es
su
pp
ort
op
era
tor
==
as
we
lla
so
pe
rato
r<
?
–O
pe
rato
rs>
&>=
&<=
are
imp
lem
en
ted
on
lyin
term
so
fo
pe
rato
r<
on
u&
t(a
nd
!o
nb
oo
lea
nre
su
lts)
–C
ou
ldim
ple
me
nt
op
era
tor
==
as
!(t
<u)
&&
!(u
<t)
so
cla
sse
sT
&U
on
lyh
ad
top
rovid
eo
pe
rato
r<
&d
idn
ot
have
top
rovid
eo
pe
rato
r=
=
•A
nsw
er:
effi
cie
ncy
(two
op
era
tor<
ca
llsa
ren
ee
de
dto
imp
lem
en
t
op
era
tor
==
imp
licitly
)
•A
nsw
er:
allo
ws
eq
uiva
len
ce
cla
sse
so
fo
rde
red
typ
es
Vanderb
iltU
nive
rsity
69
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
•G
oa
ls:
–R
ea
din
alis
to
fco
urs
en
am
es,
alo
ng
with
the
co
rresp
on
din
g
day(s
)o
fth
ew
ee
k&
time
(s)
ea
ch
co
urs
em
ee
ts∗
Days
of
the
we
ek
are
rea
din
as
ch
ara
cte
rsM
,T,W
,R,F,S
,U
∗T
ime
sa
rere
ad
as
un
sig
ne
dd
ecim
alin
teg
ers
in2
4h
ou
rH
HM
Mfo
rma
t,w
ithn
ole
ad
ing
ze
roe
s(e
.g.,
11
:59
pm
sh
ou
ldb
ere
ad
in
as
23
59
,&
mid
nig
htsh
ou
ldb
ere
ad
ina
s0
)–
So
rtth
elis
ta
cco
rdin
gto
day
of
the
we
ek
&th
en
time
ofd
ay
–D
ete
ct
any
time
so
fove
rlap
be
twe
en
co
urs
es
&p
rintth
em
ou
t–
Prin
to
uta
no
rde
red
sch
ed
ule
for
the
we
ek
•S
TL
pro
vid
es
mo
st
of
the
co
de
for
the
ab
ove
Vanderb
iltU
nive
rsity
70
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
%catinfile
CS101W
17302030
CS242T
10001130
CS242T
12301430
CS242R
10001130
CS281T
13001430
CS281R
13001430
CS282M
13001430
CS282W
13001430
CS201T
16001730
CS201R
16001730
%catinfile|xargsmain
CONFLICT:
CS242T
12301430
CS281T
13001430
CS282M13001430
CS242T10001130
CS242T12301430
CS281T13001430
CS201T16001730
CS282W13001430
CS101W17302030
CS242R10001130
CS281R13001430
CS201R16001730
Vanderb
iltU
nive
rsity
71
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
structMeeting{
enumDay_Of_Week
{MO,TU,WE,TH,FR,SA,SU};
staticDay_Of_Week
day_of_week(charc);
Meeting(conststd::string&title,
Day_Of_Weekday,
size_tstart_time,
size_tfinish_time);
Meeting(constMeeting&
m);
Meeting(char**argv);
Meeting&operator=
(constMeeting&m);
booloperator<
(constMeeting&m)const;
booloperator==
(constMeeting&m)const;
std::stringtitle_;
//Titleof
themeeting
Day_Of_Weekday_;
//Weekdayofmeeting
size_tstart_time_;
//MeetingstarttimeinHHMMformat
size_tfinish_time_;
//MeetingfinishtimeinHHMMformat
};
//Helperoperatorforoutput
std::ostream&
operator<<(std::ostream&os,
constMeeting&m);
Vanderb
iltU
nive
rsity
72
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
Meeting::Day_Of_Week
Meeting::day_of_week(charc)
{
switch(c){
case’M’:returnMeeting::MO;
case’T’:returnMeeting::TU;
case’W’:returnMeeting::WE;
case’R’:returnMeeting::TH;
case’F’:returnMeeting::FR;
case’S’:returnMeeting::SA;
case’U’:returnMeeting::SU;
default:
assert(!"nota
weekday");
returnMeeting::MO;
}
}
Meeting::Meeting
(conststd::string&title,
Day_Of_Weekday,
size_tstart,size_tfinish)
:title_(title),day_(day),
start_time_(start),
finish_time_(finish){}
Meeting::Meeting(constMeeting&m)
:title_(m.title_),day_(m.day_),
start_time_(m.start_time_),
finish_time_(m.finish_time_){}
Meeting::Meeting(char**argv)
:title_(argv[0]),
day_(Meeting::day_of_week(*argv[1])),
start_time_(atoi(argv[2])),
finish_time_(atoi(argv[3])){}
Vanderb
iltU
nive
rsity
73
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
Meeting&Meeting::operator=
(constMeeting&m){
title_=m.title_;
day_=
m.day_;
start_time_=
m.start_time_;
finish_time_=m.finish_time_;
return*this;
}boolMeeting::operator==
(constMeeting&m)const{
return
(day_==
m.day_&&
((start_time_<=m.start_time_&&
m.start_time_<=
finish_time_)||
(m.start_time_<=
start_time_&&
start_time_<=m.finish_time_)))
?true:
false;
}
boolMeeting::operator<
(constMeeting&m)const
{
return
day_<
m.day_
||
(day_==m.day_
&&
start_time_<
m.start_time_)
||
(day_==m.day_
&&
start_time_==m.start_time_
&&
finish_time_<m.finish_time_)
?true:false;
}
Vanderb
iltU
nive
rsity
74
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
std::ostream&operator<<
(std::ostream&os,
constMeeting&m){
constchar*d="
";
switch(m.day_){
caseMeeting::MO:d="M";break;
caseMeeting::TU:d="T";break;
caseMeeting::WE:d="W";break;
caseMeeting::TH:d="R";break;
caseMeeting::FR:d="F";break;
caseMeeting::SA:d="S";break;
caseMeeting::SU:d="U";break;
}return
os<<m.title_<<"
"<<d
<<m.start_time_<<""
<<m.finish_time_;
}
structprint_conflicts{
print_conflicts(std::ostream&os)
:os_(os){}
Meetingoperator()(constMeeting&lhs,
constMeeting&rhs){
if(lhs==rhs)
os_<<"CONFLICT:"<<std::endl
<<""
<<lhs<<std::endl
<<""
<<rhs<<std::endl
<<std::endl;
returnlhs;
}std::ostream&os_;
};
Vanderb
iltU
nive
rsity
75
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
template<typenameT>
classargv_iterator:publicstd::iterator<std::forward_iterator_tag,T>{
public:
argv_iterator(void){}
argv_iterator(intargc,char**argv,intincrement)
:argc_(argc),argv_(argv),base_argv_(argv),increment_(increment){}
argv_iteratorbegin(){
return*this;}
argv_iteratorend(){return*this;}
booloperator!=(constargv_iterator&){
returnargv_!=
(base_argv_+
argc_);}
Toperator*(){
returnT(argv_);}
voidoperator++(){
argv_+=increment_;}
private:
intargc_;
char**argv_,**base_argv_;
intincrement_;
};
Vanderb
iltU
nive
rsity
76
The
C+
+S
TL
Dougla
sC
.S
chm
idt
ST
LE
xa
mp
le:
Co
urs
eS
ch
ed
ule
(co
nt’d
)
intmain(intargc,char*argv[]){
std::vector<Meeting>schedule;
std::copy(argv_iterator<Meeting>(argc-1,argv+1,
4),
argv_iterator<Meeting>(),
std::back_inserter(schedule));
std::sort(schedule.begin(),schedule.end(),std::less<Meeting>());
//
Find&
printoutanyconflicts.
std::transform(sched.begin(),sched.end()-
1,
sched.begin()+
1,
sched.begin(),
print_conflicts(std::cout));
//
Printoutschedule,usingSTLoutputstreamiteratoradapter.
std::copy(sched.begin(),sched.end(),
std::ostream_iterator<Meeting>(os,"\n"));
return0;
}Vanderb
iltU
nive
rsity
77
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Su
mm
ary
of
the
Cla
ss
Sch
ed
ulin
gE
xa
mp
le
•S
TL
pro
mo
tes
so
ftwa
rere
use
:w
riting
less,
do
ing
mo
re
–E
ffort
focu
se
do
nth
eMeeting
cla
ss
–S
TL
pro
vid
ed
alg
orith
ms
(e.g
.,so
rting
&co
pyin
g),
co
nta
ine
rs,
itera
tors
,&
fun
cto
rs
•S
TL
isflexib
le,a
cco
rdin
gto
op
en
/clo
se
dp
rincip
le
–std::copy()
alg
orith
mw
itho
utp
utite
rato
rp
rints
sch
ed
ule
–S
ort
ina
sce
nd
ing
(de
fau
ltstd::less
)o
rd
esce
nd
ing
(via
std::greater
)o
rde
r
•S
TL
ise
fficie
nt
–S
TL
inlin
es
me
tho
ds,u
se
ste
mp
late
sexte
nsive
ly
–O
ptim
ize
db
oth
for
pe
rform
an
ce
&fo
rp
rogra
mm
ing
mo
de
lco
mp
lexity
(e.g
.,re
qu
iring<
&==
&n
oo
the
rs)
Vanderb
iltU
nive
rsity
78
The
C+
+S
TL
Dougla
sC
.S
chm
idt
Re
fere
nc
es
:F
or
Mo
reIn
form
atio
no
nS
TL
•D
avid
Mu
sse
r’sS
TL
pa
ge
–h
ttp://w
ww
.cs.rp
i.ed
u/m
usse
r/stl.h
tml
•S
tep
an
ov
&L
ee,“T
he
Sta
nd
ard
Te
mp
late
Lib
rary
”
–h
ttp://w
ww
.cs.rp
i.ed
u/m
usse
r/do
c.p
s
•S
GIS
TL
Pro
gra
mm
er’s
Gu
ide
–h
ttp://w
ww
.sg
i.co
m/T
ech
no
log
y/S
TL
/
•M
usse
r&
Sa
ini,
“ST
LTu
toria
l&
Re
fere
nce
Gu
ide
”
–IS
BN
0-2
01
-63
39
8-1
•A
uste
rn,“G
en
eric
Pro
gra
mm
ing
&th
eS
TL”
–IS
BN
0-2
01
-30
95
6-4
Vanderb
iltU
nive
rsity
79