Upload
neha
View
269
Download
0
Embed Size (px)
Citation preview
8/11/2019 STL Lectures
1/41
Standard Template Library Page 1
Table of ContentsC++ Overview .....................................................................................................................................3
C++ with Standard Template Library...................................................................................................4
History..................................................................................................................................................
Overview ! STL...................................................................................................................................."Overview #ontainers$ iterators$ algorithms...........................................................................................%
&terators Preview...................................................................................................................................'
&terator Sort ()ample...........................................................................................................................*
&terator................................................................................................................................................1
&terator #ategories..........................................................................................................................1
&terator Classes...................................................................................................................................1,
Primitives.......................................................................................................................................1,
-daptors.........................................................................................................................................1,
Stream &terators..............................................................................................................................1,
/n#tions -daptors........................................................................................................................1,/n#tions Operations.....................................................................................................................13
0ange...............................................................................................................................13
on!member operators..................................................................................................................13
&terators ()ample...........................................................................................................................1
&terator adaptors.............................................................................................................................1"
istream2iterator.........................................................................................................................1"
&terator -daptor ()ample...................................................................................................................1%
Templates ...........................................................................................................................................1'
eining a Template.......................................................................................................................1*
/n#tion Template einition ()ample.........................................................................................,
/n#tion templates ()ample..........................................................................................................,1/n#tion Template 5ariations ()ample.........................................................................................,,
Class templates..............................................................................................................................,4
Class Template ()ample................................................................................................................,
introd/#ing the ve#tor template.....................................................................................................,"
STL #omponents.................................................................................................................................,%
Containers......................................................................................................................................,%
Hierar#hy.......................................................................................................................................,'
Se6/en#es..................................................................................................................................,'
5e#tors..................................................................................................................................,'
Lists......................................................................................................................................,'
e6/es..................................................................................................................................,'Sta#7s and 6/e/es.................................................................................................................,*
-sso#iative #ontainers...............................................................................................................,*
Sets.......................................................................................................................................,*
8/ltisets...............................................................................................................................,*
8aps.....................................................................................................................................,*
8/ltimaps.............................................................................................................................,*
priority 6/e/e$ .....................................................................................................................,*
bit ve#tor$ .............................................................................................................................,*
6/e/e....................................................................................................................................,*
()ample /sing #ontainer algorithms..............................................................................................3
Container S/mmary Table..................................................................................................................31Simple Containers..........................................................................................................................31
Container -daptors........................................................................................................................3,
8/11/2019 STL Lectures
2/41
Standard Template Library Page ,
Containers 9 -sso#iative and other types......................................................................................33
-lgorithms..........................................................................................................................................34
-lgorithms library..........................................................................................................................34
on!modiying se6/en#e operations.............................................................................................34
8odiying se6/en#e operations.....................................................................................................3Partitioning operations...................................................................................................................3"
Sorting operations :on sorted ranges;............................................................................................3"
8/11/2019 STL Lectures
3/41
Standard Template Library Page 3
C++ Overview
#s.brown.ed/
ata abstra#tion separates the intera#e :how the ob>e#t is /sed; rom the implementation :how it
wor7s &nside;.
0elationships between ob>e#ts and types are /ndamental in ob>e#t!oriented programming?
inheritan#e deines a relationship between types #orresponding to @is!aA$ aggregation #orresponds to
@has!aA$ and asso#iations are more general relationships
Beneri# programming is a notion that has gained a lot o pop/larity in the last ew years? originally
/sed in -da$ the Standard Template Library :ro/ghly a s/bset o the standard C++ library; made it
pop/lar in C++.
8/11/2019 STL Lectures
4/41
Standard Template Library Page 4
C++ with Standard Template Library
Containers are ob>e#ts that #ontain other ob>e#ts$ e.g.$ ve#tor$ set$ map.
,. &terators represent lo#ations in a #ontainer. (a#h #ontainer has its own iterator type.
3. -lgorithms are operations on #ontainers$ e.g.$ ind$ sort$ random2sh/le.
4. /n#tors are operations on ob>e#ts$ e.g.$ less$ pl/s.
. -daptors are ob>e#ts that #hange an intera#e$ e.g.$ not1. :Thats a one at the end$ not an ell? there
is also a not,.;". Dtilities are #omponents s/#h as pairs$ operations li7e #omparison$ et#. &n the -S& standard$
allo#ators are in#l/ded in the /tilities se#tion.
%. iagnosti#s are provided to deal with e)#eptions.
'. Lo#ales a#ilitate internationaliEation.
*. /meri#s are #ontainer types that are optimiEed or speed$ less general than #ontainers$ e.g.$
valarray$ #omple).
1. Strings repla#e CFs #hara#ter arrays.
11. Streams are /sed or inp/t and o/tp/t.
1,. -llo#ators #/stomiEe memory allo#ation$ e.g.$ mallo#2allo#.
8/11/2019 STL Lectures
5/41
Standard Template Library Page
History
The ar#hite#t/re o STL is largely the #reation o one person$ -le)ander Stepanov.
&n 1*%* he began wor7ing o/t his initial ideas o generi# programming and e)ploring their potential
or revol/tioniEing sotware development. -ltho/gh avid 8/sser had developed and advo#ated
some aspe#ts o generi# programming already by year 1*%1$ it was limited to a rather spe#ialiEed
area o sotware development :#omp/ter algebra;.
Stepanov re#ogniEed the /ll potential or generi# programming and pers/aded his then!#olleag/es
at Beneral (le#tri# 0esear#h and evelopment :in#l/ding$ primarily$ avid 8/sser and eepa7
Gap/r; that generi# programming sho/ld be p/rs/ed as a #omprehensive basis or sotware
development. -t the time there was no real s/pport in any programming lang/age or generi#programming.
The irst ma>or lang/age to provide s/#h s/pport was -da$ with its generi# /nits eat/re.
8/11/2019 STL Lectures
6/41
Standard Template Library Page "
Overview - STL
The STL provides a ready!made set o #ommon #lasses or C++$
#ontainers and asso#iative arrays$
that #an be /sed with any b/ilt!in type or with any /ser!deined type
s/pports operations s/#h as #opying and assignment
STL algorithms are independent o #ontainers! signii#antly red/#es the #omple)ity o the library.
The STL a#hieves its res/lts thro/gh the /se o templates
This approa#h provides
#ompile!time polymorphism
is oten more ei#ient than traditional r/n!time polymorphism.
The STL was #reated as the irst library o generi# algorithms and data str/#t/res or C++$ with o/rideas in mind
1. generi# programming$
,. abstra#tness witho/t loss o ei#ien#y$
3. the 5on e/mann #omp/tation model$
4. and val/e semanti#s.
http==www.the/llwi7i.org=Standard2Template2Library
http://www.thefullwiki.org/Standard_Template_Libraryhttp://www.thefullwiki.org/Standard_Template_Library8/11/2019 STL Lectures
7/41
Standard Template Library Page %
Overview containers, iterators, algorithms
STL provides a n/mber o #ontainer types$ representing ob>e#ts that #ontain other
ob>e#ts.
One o these #ontainers is a #lass #alled vectorthat behaves li7e an array$ b/t #an
grow itsel as ne#essary. One o the operations on ve#tor is push_back$ whi#h p/shes
an element onto the end o the ve#tor :growing it by one;.
- ve#tor #ontains a blo#7 o contiguousinitialiEed elements !! i element inde) 7 has
been initialiEed$ then so have all the ones with indi#es less than 7.
- ve#tor #an be presized$ s/pplying the siEe at #onstr/#tion$ and yo/ #an as7 a ve#tor
how many elements it has with size. This is the logicaln/mber o elements !! the
n/mber o elements /p to the highest!inde)ed one yo/ have /sed. There is also a
notion o capacity!! the n/mber o elements the ve#tor #an hold beore reallo#ating.http==#s.brown.ed/=I>a7=proglang=#pp=stlt/t=t/t.html
http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.htmlhttp://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html8/11/2019 STL Lectures
8/41
Standard Template Library Page '
Iterators review
&terators provide a way o spe#iying a position in a #ontainer.-n iterator #an be incrementedor dereferenced$ and two iterators #an be compared.
There is a spe#ial iterator val/e #alled Jpast!the!endJ.
Ko/ #an as7 a ve#tor or an iterator that points to the irst element with the message
begin. Ko/ #an get a past!the!end iterator with the message end. The #ode
vector v;
// add some integers to vvector::iterator i1 = v.begin();
vector::iterator i2 = v.end();
will #reate two iterators li7e this
Operations li7e sort ta7e two iterators to spe#iy the so/r#e range.
To get the so/r#e elements$ they in#rement and dereeren#e the irst iterator /ntil it is
e6/al to the se#ond iterator. ote that this is a semi!open range it in#l/des the start
b/t not the end.
Two ve#tor iterators #ompare e6/al i they reer to the same element o the same
ve#tor.
8/11/2019 STL Lectures
9/41
Standard Template Library Page *
Iterator Sort !"ample
P/tting this together$ here is the new program
#include #include
#include #include
#include
main ()
{
vector v; // create an emt! vector o" integers int inut;
hile (cin >> inut) // hile not end o" "ile
v.ush$bac% (inut); // aend to vector
// sort ta%es to random iterators& and sorts the elements beteen
// them. 's is ala!s the case in *& this includes the value // re"erred to b! "irst but not the one re"erred to b! last; indeed&
// this is o"ten the ast+the+end value& and is there"ore not
// dere"erenceable. sort(v.begin()& v.end());
int n = v.si,e();
"or (int i = -; i < n; i) cout
8/11/2019 STL Lectures
10/41
Standard Template Library Page 1
Iterator
iteratorM
&terator deinitions
In C++, an iterator is any object that, pointing to some element in a range of elements (suchas an array or a container), has the ability to iteratethrough the elements of that range using a
set of operators (at least, the increment () and dereference (4) operators).
The most obvious form of iterator is apointer: pointer can point to elements in an array, andcan iterate through them using the increment operator (). !ut other forms of iterators e"ist.
#or e"ample, each containertype (such as a vector) has a specific iteratortype designed to
iterate through its elements in an efficient $ay.
%otice that $hile a pointer is a form of iterator, not all iterators have the same functionality apointer has& To distinguish bet$een the re'uirements an iterator shall have for a specific
algorithm, five different iterator categoriese"ist:
Iterator categories
&terators are #lassiied in ive #ategories depending on the /n#tionality they implement
RandomAccess Bidirectional Forward Input
Output
In this graph, each iterator category implements the functionalities of all categories to its right:
Inputand outputiterators are the most limited types of iterators, specialied in performing only
se'uential input or output operations.
#or$ard iteratorshave all the functionality of inputand outputiterators, although they are
limited to one direction in $hich to iterate through a range.
!idirectional iteratorscan be iterated through in both directions. ll standard
containerssupport at least bidirectional iterators types.
andom access iteratorsimplement all the functionalities of bidirectional iterators, plus, theyhave the ability to access ranges non*se'uentially: offsets can be directly applied to these
iterators $ithout iterating through all the elements in bet$een. This provides these iterators$ith the same functionality as standard pointers (pointers are iterators of this category).
http==www.#pl/spl/s.#om=reeren#e=iterator=
http://www.cplusplus.com/stlhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/vectorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/reference/iterator/http://www.cplusplus.com/stlhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/vectorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/stlhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/reference/iterator/8/11/2019 STL Lectures
11/41
Standard Template Library Page 11
The characteristics of each category of iterators are:
category characteristicvalid
epressions
all #ategories
Can be #opied and #opy!#onstr/#ted5 b(a);b = a;
Can be in#remented
a
a4a
0andom
-##ess
m
O/tp/t Can be dereeren#ed to be the let sideo an assignment operation
4a = t4a = t
Can be dea/lt!#onstr/#ted5 a;5()
Can be de#remented
++a
a++4a++
S/pports arithmeti# operators and +
a n
n aa + n
a + b
S/pports ine6/ality #omparisons
:$ =; between iterators
a < ba > b
a = b
S/pports #ompo/nd assignment
operations =and +=a = n
a += n
S/pports oset dereeren#e operator
:0;an0
here 5is an iterator type, aand bare objects of this iterator type, tis anobject of the type pointed by the iterator type, and nis an integer value.
andom access iteratorshave all characteristics. !idirectional iteratorshave a
subset of random access iteratorss.#or$ard iteratorshave a subsetof bidirectional iteratorss. nd inputand outputhave each their o$n subset offor$ard iterators.
http://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/RandomAccessIteratorhttp://www.cplusplus.com/ForwardIteratorhttp://www.cplusplus.com/BidirectionalIteratorhttp://www.cplusplus.com/InputIteratorhttp://www.cplusplus.com/OutputIteratorhttp://www.cplusplus.com/ForwardIterator8/11/2019 STL Lectures
12/41
Standard Template Library Page 1,
Iterator Classes
Primitives
iteratorthe basi# iterator
:#lass template;
input_iterator_tag
output_iterator_tag
forward_iterator_tag
bidirectional_iterator_tag
random_access_iterator_tag
empty #lass types /sed to indi#ate iterator #ategories
:#lass;
iterator_traitsprovides /niorm intera#e to the properties o an iterator
:#lass template;
Adaptors
reverse_iteratoriterator adaptor or reverse!order traversal
:#lass template;
move_iterator
:C++11;
iterator adaptor whi#h dereeren#es to an rval/e reeren#e
:#lass template;
back_insert_iteratoriterator adaptor or insertion at the end o a #ontainer
:#lass template;
front_insert_iteratoriterator adaptor or insertion at the ront o a #ontainer
:#lass template;
insert_iteratoriterator adaptor or insertion into a #ontainer
:#lass template;
Stream Iterators
istream_iteratorinp/t iterator that reads rom std::basic_istream
:#lass template;
ostream_iteratoro/tp/t iterator that writes to std::basic_ostream
:#lass template;
istreambuf_iteratorinp/t iterator that reads
rom std::basic_streambuf
:#lass template;
ostreambuf_iteratoro/tp/t iterator that writes to std::basic_streambuf
:#lass template;
http://en.cppreference.com/w/cpp/iterator/iteratorhttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_traitshttp://en.cppreference.com/w/cpp/iterator/reverse_iteratorhttp://en.cppreference.com/w/cpp/iterator/move_iteratorhttp://en.cppreference.com/w/cpp/iterator/back_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/front_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/istream_iteratorhttp://en.cppreference.com/w/cpp/io/basic_istreamhttp://en.cppreference.com/w/cpp/iterator/ostream_iteratorhttp://en.cppreference.com/w/cpp/io/basic_ostreamhttp://en.cppreference.com/w/cpp/iterator/istreambuf_iteratorhttp://en.cppreference.com/w/cpp/io/basic_streambufhttp://en.cppreference.com/w/cpp/iterator/ostreambuf_iteratorhttp://en.cppreference.com/w/cpp/io/basic_streambufhttp://en.cppreference.com/w/cpp/io/basic_streambufhttp://en.cppreference.com/w/cpp/iterator/iteratorhttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_tagshttp://en.cppreference.com/w/cpp/iterator/iterator_traitshttp://en.cppreference.com/w/cpp/iterator/reverse_iteratorhttp://en.cppreference.com/w/cpp/iterator/move_iteratorhttp://en.cppreference.com/w/cpp/iterator/back_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/front_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/istream_iteratorhttp://en.cppreference.com/w/cpp/io/basic_istreamhttp://en.cppreference.com/w/cpp/iterator/ostream_iteratorhttp://en.cppreference.com/w/cpp/io/basic_ostreamhttp://en.cppreference.com/w/cpp/iterator/istreambuf_iteratorhttp://en.cppreference.com/w/cpp/io/basic_streambufhttp://en.cppreference.com/w/cpp/iterator/ostreambuf_iteratorhttp://en.cppreference.com/w/cpp/io/basic_streambuf8/11/2019 STL Lectures
13/41
Standard Template Library Page 13
FunctionsAdaptors
make_move_iterator:C++11;
#reates a std::move_iteratoro type inerred rom thearg/ment
:/n#tion template;
front_inserter#reates a std::front_insert_iteratoro type inerred rom
the arg/ment
:/n#tion template;
back_inserter#reates a std::back_insert_iteratoro type inerred rom
the arg/ment
:/n#tion template;
inserter #reates a std::insert_iteratoro type inerred rom thearg/ment:/n#tion template;
Functions Operations
advanceadvan#es an iterator by given distan#e
:/n#tion;
distanceret/rns the distan#e between two iterators
:/n#tion;
next:C++11; in#rement an iterator:/n#tion;
prev:C++11;
de#rement an iterator
:/n#tion;
Range
begin:C++11;
ret/rns an iterator to the beginning o a #ontainer or array
:/n#tion;
end:C++11;
ret/rns an iterator to the end o a #ontainer or array
:/n#tion;
Non-member operators
operator==operator!=operator=
#ompares the /nderlying iterators
:/n#tion template;
operator+advan#es the iterator
:/n#tion template;
operator- #omp/tes the distan#e between two iterator adaptors:/n#tion template;
http://en.cppreference.com/w/cpp/iterator/make_move_iteratorhttp://en.cppreference.com/w/cpp/iterator/move_iteratorhttp://en.cppreference.com/w/cpp/iterator/front_inserterhttp://en.cppreference.com/w/cpp/iterator/front_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/front_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/back_inserterhttp://en.cppreference.com/w/cpp/iterator/back_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/inserterhttp://en.cppreference.com/w/cpp/iterator/insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/advancehttp://en.cppreference.com/w/cpp/iterator/distancehttp://en.cppreference.com/w/cpp/iterator/nexthttp://en.cppreference.com/w/cpp/iterator/prevhttp://en.cppreference.com/w/cpp/iterator/beginhttp://en.cppreference.com/w/cpp/iterator/endhttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator%2Bhttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator-http://en.cppreference.com/w/cpp/iterator/make_move_iteratorhttp://en.cppreference.com/w/cpp/iterator/move_iteratorhttp://en.cppreference.com/w/cpp/iterator/front_inserterhttp://en.cppreference.com/w/cpp/iterator/front_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/back_inserterhttp://en.cppreference.com/w/cpp/iterator/back_insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/inserterhttp://en.cppreference.com/w/cpp/iterator/insert_iteratorhttp://en.cppreference.com/w/cpp/iterator/advancehttp://en.cppreference.com/w/cpp/iterator/distancehttp://en.cppreference.com/w/cpp/iterator/nexthttp://en.cppreference.com/w/cpp/iterator/prevhttp://en.cppreference.com/w/cpp/iterator/beginhttp://en.cppreference.com/w/cpp/iterator/endhttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator%2Bhttp://en.cppreference.com/w/cpp/iterator/move_iterator/operator-8/11/2019 STL Lectures
14/41
Standard Template Library Page 14
operator==operator!=operator=
#ompares the /nderlying iterators
:/n#tion template;
operator+advan#es the iterator
:/n#tion template;
operator-#omp/tes the distan#e between two iterator adaptors
:/n#tion template;
operator==operator!=
#ompares two istream2iterators
:/n#tion template;
operator==
operator!=
#ompares two istreamb/2iterators
:/n#tion template;
http://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator%2Bhttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator-http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istream_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istream_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istreambuf_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istreambuf_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator_cmphttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator%2Bhttp://en.cppreference.com/w/cpp/iterator/reverse_iterator/operator-http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istream_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istream_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istreambuf_iterator/operator_eq&action=edit&redlink=1http://en.cppreference.com/mwiki/index.php?title=cpp/iterator/istreambuf_iterator/operator_eq&action=edit&redlink=18/11/2019 STL Lectures
15/41
Standard Template Library Page 1
Iterators Example
http==www.yolin/).#om=TDTO0&-LS=Lin/)T/torialC++STL.html
#include
#include
#include
using namesace std;
main()
{
vector ;
.ush$bac%(he number is 1-);
.ush$bac%(he number is 2-); .ush$bac%(he number is 7-);
cout
8/11/2019 STL Lectures
16/41
Standard Template Library Page 1"
Iterator adaptors
&n addition to iterating thro/gh #ontainers$ iterators #an iterate over streams$ either toread elements or to write them.
-n inp/t stream li7e cinhas the right /n#tionality or an inp/t iterator
it provides a##ess to a se6/en#e o elements.
The tro/ble is$ it has the wrong intera#e or an iterator
operations that /se iterators e)pe#t to be able to in#rement them and
dereeren#e them.
STL provides adaptors$ types that transorm the intera#e o other types. This is very
m/#h how ele#tri#al adaptors wor7.
One very /se/l adaptor is istream2iterator.
This is a template type? yo/ parameteriEe it by the typeo ob>e#t yo/ want to read
rom the stream. i.e. int$ #har$ loat$ et#..
istream_iterator
&n this #ase we want integers$ so we wo/ld /se an istream2iteratorintM.
&stream iterators are initialiEed by giving them a stream
dereeren#ing the iterator reads an element rom the stream$
in#rementing the iterator has no ee#t.
-n istream iterator that is #reated with the dea/lt #onstr/#tor
has the past!the!end val/e$
as does an iterator whose stream has rea#hed the end o ile.
8/11/2019 STL Lectures
17/41
Standard Template Library Page 1%
Iterator #daptor !"ample
// include librar! "or each "eature used // cre"erence.com
#include // sort#include // vector
#include //cin cout
#include //iterators
int main ()
{
using namesace std;
// create a vector to hold numbers t!ed in vector v;
cout
8/11/2019 STL Lectures
18/41
Standard Template Library Page 1'
Templates
-re a eat/re o the C++ programming lang/age that allow /n#tions and #lasses to operate withgeneri# types.
#har
int
loat
do/ble
str/#t/re
#lass
This allows a /n#tion or #lass to wor7 on many dierent data types witho/t being rewritten or
ea#h one.
Templates are o great /tility to programmers in C++$ espe#ially when #ombined with m/ltiple
inheritan#e and operator overloading.
The C++ Standard Library provides many /se/l /n#tions within a ramewor7 o #onne#ted
templates.
http==www.the/llwi7i.org=Template2:programming;
http://www.thefullwiki.org/Template_(programminghttp://www.thefullwiki.org/Template_(programming8/11/2019 STL Lectures
19/41
Standard Template Library Page 1*
Defining a Template
&magine that we want to write a /n#tion to #ompare two val/es and indi#ate whetherthe irst is less than$ e6/al to$ or greater than the se#ond.
&n pra#ti#e$ weFd want to deine several s/#h /n#tions$ ea#h o whi#h will #ompare
val/es o a given type.
O/r irst attempt might be to deine several overloaded /n#tions
// returns 0 if the values are equal, -1 if v1 is smaller, 1 if v2 is smallerint compare(const string &v1, const string &v2){if (v1 < v2) return -1if (v2 < v1) return 1return !"int compare(const doub#e &v1, const doub#e &v2){if (v1 < v2) return -1if (v2 < v1) return 1return !"
These /n#tions are nearly identi#al The only dieren#e between them is the type o
their parameters.
The /n#tion body is the same in ea#h /n#tion.
Having to repeat the body o the /n#tion or ea#h type that we #ompare is tedio/s
and error!prone.
8ore importantly$ we need to 7now when we write the program all the types that we
might ever want to #ompare. This strategy #annot wor7 i we want to be able to /se
the /n#tion on types that o/r /sers might s/pply.
Lippman, Lajoie, Moo C++11
8/11/2019 STL Lectures
20/41
Standard Template Library Page ,
0ather than deining a new /n#tion or ea#h type$ we #an deine a /n#tion
template!
- /n#tion template is a orm/la rom whi#h we #an generate type"specific
versions o that /n#tion.
The template version o #ompare loo7s li7e
Function Template Definition Example
temp#ate
8/11/2019 STL Lectures
21/41
Standard Template Library Page ,1
Function templates Example
1. #include // cin, cout, endl2.
3. using std::cout;4. using std::endl;5. using std::string;6.7. template ". const ! ma$%const ! $, const ! &'. (1). i*% < $&11. return $;12. return ;13. +14.15. int main%&16. (
17.1". // !is -ill call ma$ % argument deduction&1'. cout
8/11/2019 STL Lectures
22/41
Standard Template Library Page ,,
Function Template ariations Example
// function template variations
#include #include
#include
#include
#include
#include
#include
using std::vector;
using std::cout;
using std::endl;
using std::string;
using std::iter_swap;
template
void myReverse(T input!"
typename T::iterator it;
typename T::iterator et;
for (itinput$%egin(!& etinput$end(!; it< et; it''& et! " iter_swap(it& et)!; *
return;*
template
void show+ontents(T input!"
typename T::iterator it;for (itinput$%egin(!; it , input$end(!; it''!
" cout
8/11/2019 STL Lectures
23/41
Standard Template Library Page ,3
!lass templates
- #lass template provides a spe#ii#ation or generating #lasses based on parameters.
Class templates are #ommonly /sed to implement #ontainers.
- #lass template is instantiated by passing a given set o types to it as template
arg/ments.
The C++ Standard Library #ontains many #lass templates$ in parti#/lar the #ontainers
adapted rom the Standard Template Library$ s/#h as ve#tor.
The basi# synta) or de#laring a templated #lass is as ollows
temp#ate
8/11/2019 STL Lectures
24/41
Standard Template Library Page ,4
!lass Template Example
// class temlates
#include usingnamesacestd;
temlate
classm!air {
a& b; ublic:
m!air ( "irst& second) {a="irst; b=second;3 getma8 ();3;
temlate
m!air::getma8 ()
{
retval; retval = a>bB a : b;
returnretval;3
intmain () {
m!air m!obCect (1--& DE); cout
8/11/2019 STL Lectures
25/41
Standard Template Library Page ,
introducing t"e vector template
// ect1.cpp introducing te ector template
#include #include #include #de*ine 8 5int main%&(using std::ector;using std::string;using std::cin;using std::cout;using std::endl;
ector ratings%8&;
ector titles%8&;cout
8/11/2019 STL Lectures
26/41
Standard Template Library Page ,"
STL components
!ontainersContainers are ob>e#ts that #on#ept/ally #ontain other ob>e#ts.
They /se #ertain basi# properties o the ob>e#ts :ability to #opy$ et#.; b/t otherwise do
not depend on the type o ob>e#t they #ontain.
STL #ontainers may #ontain pointers to ob>e#ts$ tho/gh in this #ase yo/ will need to
do a little e)tra wor7.
ve#tors$ lists$ de6/es$ sets$ m/ltisets$ maps$ m/ltimaps$ 6/e/es$ sta#7s$ and priority
6/e/es$ are all provided.
Perhaps more importantly$
b/ilt!in #ontainers :C arrays; and
/ser!deined #ontainers
#an also be /sed as STL #ontainers?.
This is generally /se/l when applying operations to the #ontainers$ e.g.$ sorting a
#ontainer.
Dsing /ser!deined types as STL #ontainers #an be a##omplished by satisying the
re6/irements listed in the STL #ontainer re6/irements deinition.& this is not easible$ yo/ #an deine an adaptor #lassthat #hanges the intera#e to
satisy the re6/irements.
-ll the types are JtemplatedJ$ o #o/rse$
so yo/ #an have a ve#tor o ints
or Nindows
or a ve#tor o ve#tor o sets o m/ltimaps o strings to St/dents.
Sweat$ #ompiler!writers$ sweat ! 8a7e the #ompiler do the wor7.
http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.htmlhttp://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html#36http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.htmlhttp://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html#368/11/2019 STL Lectures
27/41
Standard Template Library Page ,%
#ierarc"$
$e%uences
Contig/o/s blo#7s o ob>e#ts? yo/ #an insert elements at any point in the se6/en#e$
b/t the perorman#e will depend on the type o se6/en#e and where yo/ are inserting.
&ectors
ast insertion at end$ and allow random a##ess.
'ists
ast insertion anywhere$ b/t provide only se6/ential a##ess.
(e%ues
ast insertion at either end$ and allow random a##ess. 0estri#ted types$ s/#h as sta#7and 6/e/e$ are b/ilt rom these /sing adaptors.
http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html#36http://cs.brown.edu/~jak/proglang/cpp/stltut/tut.html#368/11/2019 STL Lectures
28/41
Standard Template Library Page ,'
$tacks and %ueues
Provide restri#ted versions o these types$ in whi#h some operations are not allowed.
Associative containers
-sso#iative #ontainers are a generaliEation o se6/en#es. Se6/en#es are inde)ed by
integers? asso#iative #ontainers #an be inde)ed by any type.
The most #ommon type to /se as a 7ey is a string? yo/ #an have a set o strings$ or a
map rom strings to employees$ and so orth.
&t is oten /se/l to have other types as 7eys? or e)ample$ i & want to 7eep tra#7 o
the names o all the Nidgets in an appli#ation$ & #o/ld /se a map rom Nidgets to
Strings.
$ets
Sets allow yo/ to add and delete elements$ 6/ery or membership$ and iterate thro/gh
the set.
)ultisets
8/ltisets are >/st li7e sets$ e)#ept that yo/ #an have several #opies o the same
element :these are oten #alled bags;.
)aps
8aps represent a mapping rom one type :the keytype; to another type
:the valuetype;. Ko/ #an asso#iate a val/e with a 7ey$ or ind the val/e asso#iated
with a 7ey$ very ei#iently? yo/ #an also iterate thro/gh all the 7eys.
)ultimaps
8/ltimaps are >/st li7e maps e)#ept that a 7ey #an be asso#iated with several val/es.
other #ontainers
priority %ueue#
bit vector#
%ueue
8/11/2019 STL Lectures
29/41
Standard Template Library Page ,*
Example using container algorit"ms
Here is a program that generates a random perm/tation o the irst n integers$ where nis spe#iied on the #ommand line.
#include
#include
#include #include
main (int argc& char 4argv0){
int n = atoi (argv10); // argument chec%ing removed "or clarit!
vector v;
"or (int i = -; i < n; i) // aend integers - to n+1 to v v.ush$bac% (i);
random$shu""le (v.begin()& v.end()); // shu""le
co! (v.begin()& v.end()& ostream$iterator (cout& n)); // rint
3
This program #reates an empty ve#tor and ills it with the integers rom ton. &t
then sh/les the ve#tor and prints it o/t.
8/11/2019 STL Lectures
30/41
Standard Template Library Page 3
Container S$mmary Table
Simple !ontainers
*ontainer (escription
$imple *ontainers
pair
The pair container is a simple associative container consisting of a -*tupleof dataelements or objects, called first and second, in that fi"ed order. The T/ pair can beassigned, copied and compared. The array of objects allocated in a map or hash0map(described belo$) are of type pair by default, $here all the first elements act as theuni'ue 1eys, each associated $ith their second value objects.
$e%uences+Arrays ,'inked 'ists- " ordered collections
vector
a dynamic array, li1e Carray (i.e., capable of random access) $ith the ability to resie
itself automatically $hen inserting or erasing an object. Inserting and removing anelement to2from bac1 of the vector at the end ta1es amortiedconstant time. Inserting
and erasing at the beginning or in the middle is linear in time. specialiation for type boole"ists, $hich optimies for space by storing boolvalues as
bits.
lista doubly*lin1ed list& elements are not stored in contiguous memory. 3ppositeperformance from a vector. lo$ loo1up and access (linear time), but once a position hasbeen found, 'uic1 insertion and deletion (constant time).
de'ue(doubleended 'ueue)
a vector $ith insertion2erase at the beginning or end in amortied constant time,ho$ever lac1ing some guarantees on iterator validity after altering the de'ue.
http://www.thefullwiki.org/Tuplehttp://www.thefullwiki.org/List_(computing)http://www.thefullwiki.org/List_(computing)http://www.thefullwiki.org/Linked_Listhttp://www.thefullwiki.org/Linked_Listhttp://www.thefullwiki.org/Vector_(STL)http://www.thefullwiki.org/Dynamic_arrayhttp://www.thefullwiki.org/C_(programming_language)http://www.thefullwiki.org/Random_accesshttp://www.thefullwiki.org/Amortizedhttp://www.thefullwiki.org/Boolhttp://www.thefullwiki.org/Boolhttp://www.thefullwiki.org/Boolhttp://www.thefullwiki.org/Boolhttp://www.thefullwiki.org/Doubly_linked_listhttp://www.thefullwiki.org/Linked_listhttp://www.thefullwiki.org/Dequehttp://www.thefullwiki.org/Queue_(data_structure)http://www.thefullwiki.org/Tuplehttp://www.thefullwiki.org/List_(computing)http://www.thefullwiki.org/Linked_Listhttp://www.thefullwiki.org/Vector_(STL)http://www.thefullwiki.org/Dynamic_arrayhttp://www.thefullwiki.org/C_(programming_language)http://www.thefullwiki.org/Random_accesshttp://www.thefullwiki.org/Amortizedhttp://www.thefullwiki.org/Boolhttp://www.thefullwiki.org/Boolhttp://www.thefullwiki.org/Doubly_linked_listhttp://www.thefullwiki.org/Linked_listhttp://www.thefullwiki.org/Dequehttp://www.thefullwiki.org/Queue_(data_structure)8/11/2019 STL Lectures
31/41
Standard Template Library Page 31
!ontainer Adaptors
*ontainer (escription
*ontainer adaptors
'ueue
4rovides #I#3'ueueinterface in terms of pus/pop/front/backoperations.
ny se'uence supporting operations front(), back(), pus_back(),
and pop_front()can be used to instantiate 'ueue (e.g. list and de'ue).
priority0'ueue
4rovides priority 'ueueinterface in terms of pus/pop/topoperations (the element $ith
the highest priority is on top).ny random*accessse'uence supporting operations front(), pus_back(),
andpop_back()can be used to instantiate priority0'ueue (e.g. vector and de'ue).
5lements should additionally support comparison (to determine $hich element has ahigher priority and should be popped first).
stac1
4rovides /I#3stac1interface in terms of pus/pop/topoperations (the last*insertedelement is on top).ny se'uence supporting operations back(), pus_back(), and pop_back()can be used
to instantiate stac1 (e.g. vector, list, and de'ue).
http://www.thefullwiki.org/Queue_(data_structure)http://www.thefullwiki.org/FIFO_(computing)http://www.thefullwiki.org/Queue_(data_structure)http://www.thefullwiki.org/Priority_queuehttp://www.thefullwiki.org/Priority_queuehttp://www.thefullwiki.org/Priority_queuehttp://www.thefullwiki.org/Random-accesshttp://www.thefullwiki.org/Stack_(data_structure)http://www.thefullwiki.org/LIFO_(computing)http://www.thefullwiki.org/Stack_(data_structure)http://www.thefullwiki.org/Queue_(data_structure)http://www.thefullwiki.org/FIFO_(computing)http://www.thefullwiki.org/Queue_(data_structure)http://www.thefullwiki.org/Priority_queuehttp://www.thefullwiki.org/Priority_queuehttp://www.thefullwiki.org/Random-accesshttp://www.thefullwiki.org/Stack_(data_structure)http://www.thefullwiki.org/LIFO_(computing)http://www.thefullwiki.org/Stack_(data_structure)8/11/2019 STL Lectures
32/41
Standard Template Library Page 3,
!ontainers % Associative and ot"er t$pes
*ontainer (escription
Associative containers" unordered collections
set
a mathematical set& inserting2erasing elements in a set does not invalidate iterators
pointing in the set. 4rovides set operations union, intersection, difference, symmetricdifferenceand test of inclusion. Type of data must implement comparison operator 6 or
custom comparator function must be specified. Implemented using a self*balancingbinary search tree.
multiset same as a set, but allo$s duplicate elements.
mapan associative array& allo$s mapping from one data item (a 1ey) to another (a value).Type of 1ey must implement comparison operator 6 or custom comparator function mustbe specified. Implemented using a self*balancing binary search tree.
multimap same as a map, but allo$s duplicate 1eys.
hash0sethash0multisethash0maphash0multimap
similar to a set, multiset, map, or multimap, respectively, but implemented using a hashtable& 1eys are not ordered, but a hash functionmust e"ist for the 1ey type.Thesecontainersare not part of the C++ tandard /ibrary, but are included in 7Is T/e"tensions, and are included in common libraries such as the 7%8 C++ /ibrary inthe00gnu0c"" namespace. These are scheduled to be added to the C++ standard as partofT9, $ith the slightly different names of unordered0set,unordered0multiset,unordered0mapand unordered0multimap.
Other types of containers
bitsetstores series of bits similar to a fi"ed*sied vector of bools. Implements bit$iseoperations and lac1s iterators. %ot a e'uence.
valarray
another C*li1e array li1e vector, but is designed for high speed numerics at the e"pense
of some programming ease and general purpose use. It has many features that ma1e itideally suited for use $ith vector processors in traditional vector supercomputers and
I; units in consumer*level scalar processors, and also ease vector mathematicsprogramming even in scalar computers.
http://www.thefullwiki.org/Associative_arrayhttp://www.thefullwiki.org/Set_(computer_science)http://www.thefullwiki.org/Set_(computer_science)http://www.thefullwiki.org/Union_(set_theory)http://www.thefullwiki.org/Intersection_(set_theory)http://www.thefullwiki.org/Set_differencehttp://www.thefullwiki.org/Symmetric_differencehttp://www.thefullwiki.org/Symmetric_differencehttp://www.thefullwiki.org/Symmetric_differencehttp://www.thefullwiki.org/Self-balancing_binary_search_treehttp://www.thefullwiki.org/Self-balancing_binary_search_treehttp://www.thefullwiki.org/Map_(C%2B%2B)http://www.thefullwiki.org/Associative_arrayhttp://www.thefullwiki.org/Hash_tablehttp://www.thefullwiki.org/Hash_tablehttp://www.thefullwiki.org/Hash_functionhttp://www.thefullwiki.org/Container_(data_structure)http://www.thefullwiki.org/Technical_Report_1http://www.thefullwiki.org/Unordered_map_(C%2B%2B_class)http://www.thefullwiki.org/Associative_arrayhttp://www.thefullwiki.org/Set_(computer_science)http://www.thefullwiki.org/Set_(computer_science)http://www.thefullwiki.org/Union_(set_theory)http://www.thefullwiki.org/Intersection_(set_theory)http://www.thefullwiki.org/Set_differencehttp://www.thefullwiki.org/Symmetric_differencehttp://www.thefullwiki.org/Symmetric_differencehttp://www.thefullwiki.org/Self-balancing_binary_search_treehttp://www.thefullwiki.org/Self-balancing_binary_search_treehttp://www.thefullwiki.org/Map_(C%2B%2B)http://www.thefullwiki.org/Associative_arrayhttp://www.thefullwiki.org/Hash_tablehttp://www.thefullwiki.org/Hash_tablehttp://www.thefullwiki.org/Hash_functionhttp://www.thefullwiki.org/Container_(data_structure)http://www.thefullwiki.org/Technical_Report_1http://www.thefullwiki.org/Unordered_map_(C%2B%2B_class)8/11/2019 STL Lectures
33/41
Standard Template Library Page 33
#lgorithms
algorithm: Routines to find, count, sort, search, ... elements in container
classes- large n/mber o algorithms to perorm operations s/#h as sear#hing and sorting are provided in
the STL$
ea#h implemented to re6/ire a #ertain level o iterator :and thereore will wor7 on any #ontainer
whi#h provides an intera#e by iterators;.
Algorit"ms librar$
C++
Algorithm library
The algorithms library defines functions for a variety of purposes (e.g.searching, sorting, counting, manipulating) that operate on ranges of
elements. Note that a range is defined as @*irst, last&where lastrefers tothe element pastthe last element to inspect or modify.
Non-modif$ing se&uence operations
eined in header
all_of
any_ofnone_of:C++11;
:C++11;
:C++11;
#he#7s i a predi#ate is trueor all$ any or none o the elements
in a range
:/n#tion template;
for_eachapplies a /n#tion to a range o elements
:/n#tion template;
countcount_if
ret/rns the n/mber o elements satisying spe#ii# #riteria
:/n#tion template;
mismatch inds the irst position where two ranges dier:/n#tion template;
equaldetermines i two sets o elements are the same
:/n#tion template;
findfind_iffind_if_not
:C++11;
inds the irst element satisying spe#ii# #riteria
:/n#tion template;
find_end inds the last se6/en#e o elements in a #ertain range:/n#tion template;
find_first_of sear#hes or any one o a set o elements
http://en.cppreference.com/w/cpphttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/all_any_none_ofhttp://en.cppreference.com/w/cpp/algorithm/all_any_none_ofhttp://en.cppreference.com/w/cpp/algorithm/all_any_none_ofhttp://en.cppreference.com/w/cpp/algorithm/for_eachhttp://en.cppreference.com/w/cpp/algorithm/counthttp://en.cppreference.com/w/cpp/algorithm/counthttp://en.cppreference.com/w/cpp/algorithm/mismatchhttp://en.cppreference.com/w/cpp/algorithm/equalhttp://en.cppreference.com/w/cpp/algorithm/findhttp://en.cppreference.com/w/cpp/algorithm/findhttp://en.cppreference.com/w/cpp/algorithm/findhttp://en.cppreference.com/w/cpp/algorithm/find_endhttp://en.cppreference.com/w/cpp/algorithm/find_first_ofhttp://en.cppreference.com/w/cpphttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/all_any_none_ofhttp://en.cppreference.com/w/cpp/algorithm/all_any_none_ofhttp://en.cppreference.com/w/cpp/algorithm/all_any_none_ofhttp://en.cppreference.com/w/cpp/algorithm/for_eachhttp://en.cppreference.com/w/cpp/algorithm/counthttp://en.cppreference.com/w/cpp/algorithm/counthttp://en.cppreference.com/w/cpp/algorithm/mismatchhttp://en.cppreference.com/w/cpp/algorithm/equalhttp://en.cppreference.com/w/cpp/algorithm/findhttp://en.cppreference.com/w/cpp/algorithm/findhttp://en.cppreference.com/w/cpp/algorithm/findhttp://en.cppreference.com/w/cpp/algorithm/find_endhttp://en.cppreference.com/w/cpp/algorithm/find_first_of8/11/2019 STL Lectures
34/41
Standard Template Library Page 34
:/n#tion template;
adacent_findinds two identi#al :or some other relationship; items ad>a#ent to
ea#h other
:/n#tion template;
searchsear#hes or a range o elements
:/n#tion template;
search_nsear#hes or a n/mber #onse#/tive #opies o an element in a
range
:/n#tion template;
'odif$ing se&uence operations
eined in header
copycopy_if
:C++11;
#opies a range o elements to a new lo#ation
:/n#tion template;
copy_n:C++11;
#opies a n/mber o elements to a new lo#ation
:/n#tion template;
copy_backward#opies a range o elements in ba#7wards order
:/n#tion template;
move:C++11;
moves a range o elements to a new lo#ation
:/n#tion template;
move_backward:C++11;
moves a range o elements to a new lo#ation in ba#7wards order:/n#tion template;
fillassigns a range o elements a #ertain val/e
:/n#tion template;
fill_nassigns a val/e to a n/mber o elements
:/n#tion template;
transformapplies a /n#tion to a range o elements
:/n#tion template;
generatesaves the res/lt o a /n#tion in a range
:/n#tion template;
generate_nsaves the res/lt o appli#ations o a /n#tion
:/n#tion template;
removeremove_if
removes elements satisying spe#ii# #riteria
:/n#tion template;
remove_copyremove_copy_if
#opies a range o elements omitting those that satisy spe#ii#
#riteria
:/n#tion template;
replace
replace_if
repla#es all val/es satisying spe#ii# #riteria with another val/e
:/n#tion template;replace_copyreplace_copy_if
#opies a range$ repla#ing elements satisying spe#ii# #riteria
with another val/e
http://en.cppreference.com/w/cpp/algorithm/adjacent_findhttp://en.cppreference.com/w/cpp/algorithm/searchhttp://en.cppreference.com/w/cpp/algorithm/search_nhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/copyhttp://en.cppreference.com/w/cpp/algorithm/copyhttp://en.cppreference.com/w/cpp/algorithm/copy_nhttp://en.cppreference.com/w/cpp/algorithm/copy_backwardhttp://en.cppreference.com/w/cpp/algorithm/movehttp://en.cppreference.com/w/cpp/algorithm/move_backwardhttp://en.cppreference.com/w/cpp/algorithm/fillhttp://en.cppreference.com/w/cpp/algorithm/fill_nhttp://en.cppreference.com/w/cpp/algorithm/transformhttp://en.cppreference.com/w/cpp/algorithm/generatehttp://en.cppreference.com/w/cpp/algorithm/generate_nhttp://en.cppreference.com/w/cpp/algorithm/removehttp://en.cppreference.com/w/cpp/algorithm/removehttp://en.cppreference.com/w/cpp/algorithm/remove_copyhttp://en.cppreference.com/w/cpp/algorithm/remove_copyhttp://en.cppreference.com/w/cpp/algorithm/replacehttp://en.cppreference.com/w/cpp/algorithm/replacehttp://en.cppreference.com/w/cpp/algorithm/replace_copyhttp://en.cppreference.com/w/cpp/algorithm/replace_copyhttp://en.cppreference.com/w/cpp/algorithm/adjacent_findhttp://en.cppreference.com/w/cpp/algorithm/searchhttp://en.cppreference.com/w/cpp/algorithm/search_nhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/copyhttp://en.cppreference.com/w/cpp/algorithm/copyhttp://en.cppreference.com/w/cpp/algorithm/copy_nhttp://en.cppreference.com/w/cpp/algorithm/copy_backwardhttp://en.cppreference.com/w/cpp/algorithm/movehttp://en.cppreference.com/w/cpp/algorithm/move_backwardhttp://en.cppreference.com/w/cpp/algorithm/fillhttp://en.cppreference.com/w/cpp/algorithm/fill_nhttp://en.cppreference.com/w/cpp/algorithm/transformhttp://en.cppreference.com/w/cpp/algorithm/generatehttp://en.cppreference.com/w/cpp/algorithm/generate_nhttp://en.cppreference.com/w/cpp/algorithm/removehttp://en.cppreference.com/w/cpp/algorithm/removehttp://en.cppreference.com/w/cpp/algorithm/remove_copyhttp://en.cppreference.com/w/cpp/algorithm/remove_copyhttp://en.cppreference.com/w/cpp/algorithm/replacehttp://en.cppreference.com/w/cpp/algorithm/replacehttp://en.cppreference.com/w/cpp/algorithm/replace_copyhttp://en.cppreference.com/w/cpp/algorithm/replace_copy8/11/2019 STL Lectures
35/41
Standard Template Library Page 3
:/n#tion template;
swapswaps the val/es o two ob>e#ts
:/n#tion template;
swap_ranges swaps two ranges o elements:/n#tion template;
iter_swapswaps the elements pointed to by two iterators
:/n#tion template;
reversereverses the order elements in a range
:/n#tion template;
reverse_copy#reates a #opy o a range that is reversed
:/n#tion template;
rotaterotates the order o elements in a range
:/n#tion template;
rotate_copy#opies and rotate a range o elements
:/n#tion template;
random_shuffleshuffle
:C++11;
randomly re!orders elements in a range
:/n#tion template;
uniqueremoves #onse#/tive d/pli#ate elements in a range
:/n#tion template;
unique_copy#reates a #opy o some range o elements that #ontains no
#onse#/tive d/pli#ates:/n#tion template;
Partitioning operations
eined in header
is_partitioned:C++11;
determines i the range is partitioned by the given predi#ate
:/n#tion template;
partitiondivides a range o elements into two gro/ps
:/n#tion template;
partition_copy:C++11;
#opies a range dividing the elements into two gro/ps
:/n#tion template;
stable_partitiondivides elements into two gro/ps while preserving their relative
order
:/n#tion template;
partition_point:C++11;
lo#ates the partition point o a partitioned range
:/n#tion template;
Sorting operations (on sorted ranges)
eined in header is_sorted:C++11;
#he#7s whether a range is sorted into as#ending order
:/n#tion template;
http://en.cppreference.com/w/cpp/algorithm/swaphttp://en.cppreference.com/w/cpp/algorithm/swap_rangeshttp://en.cppreference.com/w/cpp/algorithm/iter_swaphttp://en.cppreference.com/w/cpp/algorithm/reversehttp://en.cppreference.com/w/cpp/algorithm/reverse_copyhttp://en.cppreference.com/w/cpp/algorithm/rotatehttp://en.cppreference.com/w/cpp/algorithm/rotate_copyhttp://en.cppreference.com/w/cpp/algorithm/random_shufflehttp://en.cppreference.com/w/cpp/algorithm/random_shufflehttp://en.cppreference.com/w/cpp/algorithm/uniquehttp://en.cppreference.com/w/cpp/algorithm/unique_copyhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/is_partitionedhttp://en.cppreference.com/w/cpp/algorithm/partitionhttp://en.cppreference.com/w/cpp/algorithm/partition_copyhttp://en.cppreference.com/w/cpp/algorithm/stable_partitionhttp://en.cppreference.com/w/cpp/algorithm/partition_pointhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/is_sortedhttp://en.cppreference.com/w/cpp/algorithm/swaphttp://en.cppreference.com/w/cpp/algorithm/swap_rangeshttp://en.cppreference.com/w/cpp/algorithm/iter_swaphttp://en.cppreference.com/w/cpp/algorithm/reversehttp://en.cppreference.com/w/cpp/algorithm/reverse_copyhttp://en.cppreference.com/w/cpp/algorithm/rotatehttp://en.cppreference.com/w/cpp/algorithm/rotate_copyhttp://en.cppreference.com/w/cpp/algorithm/random_shufflehttp://en.cppreference.com/w/cpp/algorithm/random_shufflehttp://en.cppreference.com/w/cpp/algorithm/uniquehttp://en.cppreference.com/w/cpp/algorithm/unique_copyhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/is_partitionedhttp://en.cppreference.com/w/cpp/algorithm/partitionhttp://en.cppreference.com/w/cpp/algorithm/partition_copyhttp://en.cppreference.com/w/cpp/algorithm/stable_partitionhttp://en.cppreference.com/w/cpp/algorithm/partition_pointhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/is_sorted8/11/2019 STL Lectures
36/41
Standard Template Library Page 3"
is_sorted_until:C++11;
inds the largest sorted s/brange
:/n#tion template;
sortsorts a range into as#ending order
:/n#tion template;
partial_sortsorts the irst elements o a range
:/n#tion template;
partial_sort_copy#opies and partially sorts a range o elements
:/n#tion template;
stable_sortsorts a range o elements while preserving order between e6/al
elements
:/n#tion template;
nth_elementpartially sorts the given range ma7ing s/re that it is partitioned
by the given element
:/n#tion template;
*inar$ searc" operations (on sorted ranges)
eined in header
lower_boundret/rns an iterator to the irst element not lessthan the given
val/e
:/n#tion template;
upper_boundret/rns an iterator to the irst elementgreaterthan a #ertain
val/e
:/n#tion template;
binary_searchdetermines i an element e)ists in a #ertain range
:/n#tion template;
equal_rangeret/rns range o elements mat#hing a spe#ii# 7ey
:/n#tion template;
Set operations (on sorted ranges)
eined in header
merge
merges two sorted ranges
:/n#tion template;
inplace_mergemerges two ordered ranges in!pla#e
:/n#tion template;
includesret/rns tr/e i one set is a s/bset o another
:/n#tion template;
set_difference#omp/tes the dieren#e between two sets
:/n#tion template;
set_intersection#omp/tes the interse#tion o two sets
:/n#tion template;
set_symmetric_difference
#omp/tes the symmetri# dieren#e between two sets:/n#tion template;
http://en.cppreference.com/w/cpp/algorithm/is_sorted_untilhttp://en.cppreference.com/w/cpp/algorithm/sorthttp://en.cppreference.com/w/cpp/algorithm/partial_sorthttp://en.cppreference.com/w/cpp/algorithm/partial_sort_copyhttp://en.cppreference.com/w/cpp/algorithm/stable_sorthttp://en.cppreference.com/w/cpp/algorithm/nth_elementhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/lower_boundhttp://en.cppreference.com/w/cpp/algorithm/upper_boundhttp://en.cppreference.com/w/cpp/algorithm/binary_searchhttp://en.cppreference.com/w/cpp/algorithm/equal_rangehttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/mergehttp://en.cppreference.com/w/cpp/algorithm/inplace_mergehttp://en.cppreference.com/w/cpp/algorithm/includeshttp://en.cppreference.com/w/cpp/algorithm/set_differencehttp://en.cppreference.com/w/cpp/algorithm/set_intersectionhttp://en.cppreference.com/w/cpp/algorithm/set_symmetric_differencehttp://en.cppreference.com/w/cpp/algorithm/set_symmetric_differencehttp://en.cppreference.com/w/cpp/algorithm/is_sorted_untilhttp://en.cppreference.com/w/cpp/algorithm/sorthttp://en.cppreference.com/w/cpp/algorithm/partial_sorthttp://en.cppreference.com/w/cpp/algorithm/partial_sort_copyhttp://en.cppreference.com/w/cpp/algorithm/stable_sorthttp://en.cppreference.com/w/cpp/algorithm/nth_elementhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/lower_boundhttp://en.cppreference.com/w/cpp/algorithm/upper_boundhttp://en.cppreference.com/w/cpp/algorithm/binary_searchhttp://en.cppreference.com/w/cpp/algorithm/equal_rangehttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/mergehttp://en.cppreference.com/w/cpp/algorithm/inplace_mergehttp://en.cppreference.com/w/cpp/algorithm/includeshttp://en.cppreference.com/w/cpp/algorithm/set_differencehttp://en.cppreference.com/w/cpp/algorithm/set_intersectionhttp://en.cppreference.com/w/cpp/algorithm/set_symmetric_differencehttp://en.cppreference.com/w/cpp/algorithm/set_symmetric_difference8/11/2019 STL Lectures
37/41
Standard Template Library Page 3%
set_union#omp/tes the /nion o two sets
:/n#tion template;
#eap operations
eined in header
is_heap#he#7s i the given range is a heap
:/n#tion template;
is_heap_until:C++11;
inds the largest s/brange that is heap
:/n#tion template;
make_heap#reates a heap o/t o a range o elements
:/n#tion template;
push_heapadds an element to a heap
:/n#tion template;
pop_heapremoves the largest element rom a heap
:/n#tion template;
sort_heapt/rns a heap into a sorted range o elements
:/n#tion template;
'inimum+maximum operations
eined in header
maxret/rns the larger o two elements
:/n#tion template;
max_elementret/rns the largest element in a range
:/n#tion template;
minret/rns the smaller o two elements
:/n#tion template;
min_elementret/rns the smallest element in a range
:/n#tion template;
minmax:C++11;
ret/rns the larger and the smaller o two elements
:/n#tion template;
minmax_element:C++11; ret/rns the smallest and the largest element in a range:/n#tion template;
lexicographical_compare
ret/rns tr/e i one range is le)i#ographi#ally less than another
:/n#tion template;
is_permutation:C++11;
determines i a se6/en#e is a perm/tation o another se6/en#e
:/n#tion template;
next_permutationgenerates the ne)t greater le)i#ographi# perm/tation o a range
o elements
:/n#tion template;
prev_permutationgenerates the ne)t smaller le)i#ographi# perm/tation o a range
o elements
:/n#tion template;
http://en.cppreference.com/w/cpp/algorithm/set_unionhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/is_heaphttp://en.cppreference.com/w/cpp/algorithm/is_heap_untilhttp://en.cppreference.com/w/cpp/algorithm/make_heaphttp://en.cppreference.com/w/cpp/algorithm/push_heaphttp://en.cppreference.com/w/cpp/algorithm/pop_heaphttp://en.cppreference.com/w/cpp/algorithm/sort_heaphttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/maxhttp://en.cppreference.com/w/cpp/algorithm/max_elementhttp://en.cppreference.com/w/cpp/algorithm/minhttp://en.cppreference.com/w/cpp/algorithm/min_elementhttp://en.cppreference.com/w/cpp/algorithm/minmaxhttp://en.cppreference.com/w/cpp/algorithm/minmax_elementhttp://en.cppreference.com/w/cpp/algorithm/lexicographical_comparehttp://en.cppreference.com/w/cpp/algorithm/lexicographical_comparehttp://en.cppreference.com/w/cpp/algorithm/is_permutationhttp://en.cppreference.com/w/cpp/algorithm/next_permutationhttp://en.cppreference.com/w/cpp/algorithm/prev_permutationhttp://en.cppreference.com/w/cpp/algorithm/set_unionhttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/is_heaphttp://en.cppreference.com/w/cpp/algorithm/is_heap_untilhttp://en.cppreference.com/w/cpp/algorithm/make_heaphttp://en.cppreference.com/w/cpp/algorithm/push_heaphttp://en.cppreference.com/w/cpp/algorithm/pop_heaphttp://en.cppreference.com/w/cpp/algorithm/sort_heaphttp://en.cppreference.com/w/cpp/header/algorithmhttp://en.cppreference.com/w/cpp/algorithm/maxhttp://en.cppreference.com/w/cpp/algorithm/max_elementhttp://en.cppreference.com/w/cpp/algorithm/minhttp://en.cppreference.com/w/cpp/algorithm/min_elementhttp://en.cppreference.com/w/cpp/algorithm/minmaxhttp://en.cppreference.com/w/cpp/algorithm/minmax_elementhttp://en.cppreference.com/w/cpp/algorithm/lexicographical_comparehttp://en.cppreference.com/w/cpp/algorithm/lexicographical_comparehttp://en.cppreference.com/w/cpp/algorithm/is_permutationhttp://en.cppreference.com/w/cpp/algorithm/next_permutationhttp://en.cppreference.com/w/cpp/algorithm/prev_permutation8/11/2019 STL Lectures
38/41
Standard Template Library Page 3'
Numeric operations
eined in header
iota:C++11;
ills a range with s/##essive in#rements o the starting val/e:/n#tion template;
accumulates/ms /p a range o elements
:/n#tion template;
inner_product#omp/tes the inner prod/#t o two ranges o elements
:/n#tion template;
adacent_difference#omp/tes the dieren#es between ad>a#ent elements in a range
:/n#tion template;
partial_sum#omp/tes the partial s/m o a range o elements
:/n#tion template;
! librar$
eined in header
qsortsorts a range o elements with /nspe#iied type
:/n#tion;
bsearchsear#hes an array or an element o /nspe#iied type
:/n#tion;
http://en.cppreference.com/w/cpp/header/numerichttp://en.cppreference.com/w/cpp/algorithm/iotahttp://en.cppreference.com/w/cpp/algorithm/accumulatehttp://en.cppreference.com/w/cpp/algorithm/inner_producthttp://en.cppreference.com/w/cpp/algorithm/adjacent_differencehttp://en.cppreference.com/w/cpp/algorithm/partial_sumhttp://en.cppreference.com/w/cpp/header/cstdlibhttp://en.cppreference.com/w/cpp/algorithm/qsorthttp://en.cppreference.com/w/cpp/algorithm/bsearchhttp://en.cppreference.com/w/cpp/header/numerichttp://en.cppreference.com/w/cpp/algorithm/iotahttp://en.cppreference.com/w/cpp/algorithm/accumulatehttp://en.cppreference.com/w/cpp/algorithm/inner_producthttp://en.cppreference.com/w/cpp/algorithm/adjacent_differencehttp://en.cppreference.com/w/cpp/algorithm/partial_sumhttp://en.cppreference.com/w/cpp/header/cstdlibhttp://en.cppreference.com/w/cpp/algorithm/qsorthttp://en.cppreference.com/w/cpp/algorithm/bsearch8/11/2019 STL Lectures
39/41
8/11/2019 STL Lectures
40/41
Standard Template Library Page 4
C++ Standard Library
"++ tandard,ibrary ios
iostream iomanip *stream sstream
http://www.thefullwiki.org/C%2B%2B_Standard_Libraryhttp://www.thefullwiki.org/C%2B%2B_Standard_Libraryhttp://www.thefullwiki.org/Ios_(C%2B%2B)http://www.thefullwiki.org/Iostreamhttp://www.thefullwiki.org/Iomaniphttp://www.thefullwiki.org/Fstreamhttp://www.thefullwiki.org/Sstreamhttp://www.thefullwiki.org/C%2B%2B_Standard_Libraryhttp://www.thefullwiki.org/C%2B%2B_Standard_Libraryhttp://www.thefullwiki.org/Ios_(C%2B%2B)http://www.thefullwiki.org/Iostreamhttp://www.thefullwiki.org/Iomaniphttp://www.thefullwiki.org/Fstreamhttp://www.thefullwiki.org/Sstream8/11/2019 STL Lectures
41/41
Standard Template Library Page 41
!,, Standard ibrar$
tandardemplate,ibrary
.,, wx/idgets "lasses
ector deDue list map set
stac Dueue
itset
algoritm *unctional
iterator
$Eector
-$Fist
-$as8ap-$asGet-$Gtac
http://docs.wxwidgets.org/trunk/annotated.htmlhttp://www.thefullwiki.org/Vector_(C%2B%2B)http://www.thefullwiki.org/List_(C%2B%2B)http://www.thefullwiki.org/Map_(C%2B%2B)http://www.thefullwiki.org/Algorithm_(C%2B%2B)http://www.thefullwiki.org/Iterator_(C%2B%2B)http://docs.wxwidgets.org/trunk/classwx_vector_3_01_t_01_4.htmlhttp://docs.wxwidgets.org/trunk/classwx_list_3_01_t_01_4.htmlhttp://docs.wxwidgets.org/trunk/classwx_hash_map.htmlhttp://docs.wxwidgets.org/trunk/classwx_hash_set.htmlhttp://docs.wxwidgets.org/trunk/classwx_stack_3_01_t_01_4.htmlhttp://docs.wxwidgets.org/trunk/annotated.htmlhttp://www.thefullwiki.org/Vector_(C%2B%2B)http://www.thefullwiki.org/List_(C%2B%2B)http://www.thefullwiki.org/Map_(C%2B%2B)http://www.thefullwiki.org/Algorithm_(C%2B%2B)http://www.thefullwiki.org/Iterator_(C%2B%2B)http://docs.wxwidgets.org/trunk/classwx_vector_3_01_t_01_4.htmlhttp://docs.wxwidgets.org/trunk/classwx_list_3_01_t_01_4.htmlhttp://docs.wxwidgets.org/trunk/classwx_hash_map.htmlhttp://docs.wxwidgets.org/trunk/classwx_hash_set.htmlhttp://docs.wxwidgets.org/trunk/classwx_stack_3_01_t_01_4.html