effective stl notes

77
UTTAM GANDHI, SOFTWARE DEVELOPER Effective STL Notes

Upload: uttam-gandhi

Post on 10-May-2015

1.021 views

Category:

Technology


0 download

DESCRIPTION

Effective STL Notes, a presentation I prepared for training at Synerzip.

TRANSCRIPT

Page 1: Effective stl notes

U T T A M G A N D H I , S O F T W A R E D E V E L O P E R

Effective STL Notes

Page 2: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Pre-requisites

�  C++ �  Data Structure �  Templates �  Smart Pointers �  Constant Time, Logarithmic, Linear

Page 3: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Acknowledgments

�  This presentation is based on Scott Meyers well known book ‘Effective STL’

Page 4: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Why STL

�  Contains most useful algorithms and data structures �  Established programming techniques, idioms and

styles �  Usually very-well tested �  Usually highly optimized (might use optimizations

that are not available to ordinary programmer) �  Ref-- http://www.cs.helsinki.fi/u/tpkarkka/alglib/

k06/lectures/stl_intro.html

Page 5: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

STL Intro duction

�  STL consists of ¡  Containers ¡  Algorithms ¡  Iterators

Page 6: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Containers

�  Types of container ¡  Sequence containers

÷ vector ÷  list ÷ string ÷ deque

¡  Associative Containers ÷ set ÷ multiset ÷ map ÷ multimap ÷ unordered_map (C++ 11)

Page 7: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Containers

�  Container type based on allocation ¡  Contiguous-memory containers

÷  Insertion and deletion causes movement of elements ÷ vector, string, deque

¡  Node-based containers ÷  Insertion and deletion changes pointers, not nodes ÷  list and all associative containers

Page 8: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

How to choose your container

�  Insert element at an arbitrary position �  Ordering of elements in the container �  Category of iterators �  Is it important to avoid movement of existing

container elements when insertions or erasures take place?

�  Layout compatible with C �  container uses reference counting

Page 9: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Containers do Copy

�  Containers make copies of object ¡  Storing ¡  Retrieving ¡  Insert/Delete causes movement of objects by copying ¡  Sorting algorithms

�  Copying is done using copy constructor and copy assignment operator

�  Copying should be efficient �  Copying can lead to slicing

Page 10: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Making Copy Efficient

�  Use pointers ¡  Efficient, as bitwise copy will be made ¡  No slicing ¡  But possibility of memory leak ¡  Use smart pointers (self managed/shared/intrusive) ¡  Ref http://onlamp.com/pub/a/onlamp/2006/05/04/smart-

pointers.html?page=5

�  Still STL containers are better than C data structure ¡  Widget w[maxNumWidgets]; ¡  vector<widget> vw; ¡  vw.reserve(maxNumWidgets)

Page 11: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use empty than size==0

�  Use empty, rather than checking size == 0 ¡  For some list implementation, size takes linear time ¡  Why

¢  Splice operation moves elements from one list to other without copying elements

¢  If, size operation has to be constant then every list operation should update size

¢  This means, splice operation will take linear time ¢  One of the two can be constant but not both

¡  empty() will always be constant

Page 12: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer range member functions 1/3

�  vector<Widget> v1, v2; for ( vector<Widget>::const_iterator ci = v2.begin(); ci != v2.end(); ++ci) v1.push_back(*ci);

¡  lengthy code ¡  for loop

�  copy(v2.begin(), v2.end(), back_inserter(v1 )); ¡  For loop is inside copy implementation

�  v1 .insert(v1 .end(), v2.begin(),v2.end()); ¡  Short code ¡  Clear to understand

Page 13: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer range member functions 2/3

�  Efficiency Hits ¡  Inserting one element at a time, makes numValues calls of

insert ÷ Range function will make single insert call, so numValues-1 less

calls ÷  Inlining could have saved, but its not guaranteed ÷ Range insert will always be efficient

¡  Inserting element will cause movement of elements by copying them ÷ Every element will be shifted at least numValues times ÷ Shifting of primitive will require bitwise copy ÷ For user defined type, calls to assignment operator or copy

constructor

Page 14: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer range member functions 3/3

�  Efficiency Hits ¡  Inserting element in vector can cause reallocations

÷  In case of range insert, reallocation would be 1 at max ÷  inserting numValues new elements could result in new memory

being allocated up to log2numValues ÷  inserting 1000 elements one at a time can result in 10 new

allocations

Page 15: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use delete for new element in container 1/3

void doSomething() { vector<Widget*> vwp; for (int i = 0; i < SOME_MAGIC_NUMBER; ++i) vwp.push_back(new Widget); … // use vwp } This is a leak

Page 16: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use delete for new element in container 2/3

void doSomething() { vector<Widget*> vwp; … // as before for (vector<Widget*>::iterator i = vwp.begin(); i != vwp.end(), ++i) { delete *i; } still there can be leak because of exception

Page 17: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use delete for new element in container 3/3

void doSomething() { typedef boost::shared_ ptr<Widget> SPW; vector<SPW> vwp; for (int i = 0; i < SOME_MAGIC_NUMBER; ++i) vwp.push_back(SPW new Widget) … // use vwp } // no Widgets are leaked here, not

// even if an exception is thrown //in the code above

Page 18: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Choosing Erasing Options

�  Erase remove for sequence containers ¡  c.erase( remove(c.begin(), c.end(), 1963), c.end()); ¡  remove function being receives only iterator (no container) ¡  remove takes linear time

�  List ¡  c. remove(1963);

�  Associative container ¡  c.erase(1963); ¡  Takes logarithmic time

Page 19: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Choosing Erasing Options

�  Erase remove for sequence containers ¡  c.erase( remove_if(c.begin(), c.end(),badValue), c.end()); ¡  badValue is the predicate

�  List ¡  c. remove_if(1963);

�  Associative container ¡  simple but does not work due to iterator invalidation ¡  The iterator pointing to erase becomes invalid, after execution of erase

AssocContainer<int> c; ...... for (AssocContainer<int>::iterator i = c.begin(); i!= c.end(); ++i) { if (badValue(*i)) c.erase(i); }

Page 20: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Choosing Erasing Options

AssocContainer<int> c; for (AssocContainer<int>::iterator i = c.begin(); i != c.end(); ){ if (badValue(*i)) c.erase(i++); else ++i; }

¡  If there is a match erase is done using i ¡  i is incremented as a side-effect (before erase is executed) ¡  Effectively i is incremented before invalidating

Page 21: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer vector and string

�  Dynamic Allocation ¡  Ensure delete is called for every new, else leak ¡  Ensure delete[] is called for array ¡  Ensure delete called only once

�  Vector and string advantages ¡  Manage own memory ¡  Offer useful member functions like size, iterators, erase ¡  Can be used with legacy C code using array and char*

�  Drawback ¡  string uses ref counting ¡  Causes overhead of concurrency control in multithreaded

environment ¡  http://www.gotw.ca/publications/optimizations.htm

Page 22: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer vector and string

�  Solution ¡  Disable ref counting by changing pre-processor variable, may

not be portable ¡  Find alternative string implementation ¡  Use vector<char>

÷ string functions will not be available ÷ But STL algorithms will serve the purpose J

�  Avoiding reallocation in vector ¡  Use reserve

Page 23: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Pass vector and string to legacy API

�  Vector ¡  void doSomething(const int* pInts, size_t numlnts); ¡  doSomething(&v[0], v.size()); ¡  Frankly, if you're hanging out with people who tell you to use

v.begin() instead of &v[0], you need to rethink your social circle.

¡  v can be modified but no new element should be added ¡  size_t fillArray(double *pArray, size_t arraySize); ¡  vector<double> vd(maxNumDoubles); ¡  vd.resize(fillArray(&vd[0], vd.size())); ¡  list<double> l(vd.begin(), vd.end());

Page 24: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Pass vector and string to legacy API

�  String ¡  void doSomething(const char *pString); ¡  doSomething(s.c_str());

Page 25: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use swap to trim excess capacity

�  class Contestant {...}; �  vector<Contestant> contestants; �  vector<Contestant>(contestants).swap(contestants);

¡  Temporary vector gets created using copy constructor ¡  It is created by the existing size (shrink) ¡  Swap happens, now temporary holds excess capacity ¡  At the end of statement temporary is destroyed

Page 26: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Equality and Equivalence 1/3

�  Equality is based on ==, “x==y” ¡  Widget w1, w2 ¡  w1 == w2 returns true

�  Equivalence is based on operator < ¡  !(w1 < w2) && !(w2 < w1) ¡  Equivalence is based on the relative ordering of object values

in a sorted range ¡  Used in storing, retrieving in associative container ¡  two values are equivalent (with respect to some ordering

criterion) if neither precedes the other (according to that criterion)

Page 27: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Equality and Equivalence 2/3

�  !c.key_comp()(x, y) && !c.key_comp()(y, x) struct CiStrCom:public binary_function<string, string, bool> { bool operator()(const string& lhs, const string& rhs) const {

return ciStringCompare(lhs, rhs); }

} set<string, CiStrCom> ciStringSet;

Page 28: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Equality and Equivalence 3/3

�  ciss.insert("Persephone"); // a new element is added to the set

�  ciss.insert("persephone"); // no new element is added to the set

�  if (ciss.find("persephone") != ciss.end())... // this test will succeed ¡  Uses equivalence, so works

�  if (find( ciss.begin(), ciss.end(), "persephone") != ciss.end())... // this test will fail ¡  Uses equality, fails in this case

Page 29: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Overriding default comp function

�  When you declare set of string pointer ¡  set<string*> ssp; ¡  ssp.insert(newstring("Anteater")); ¡  ssp.insert(newstring("Wombat")); ¡  ssp.insert(new string(“Lemur")); ¡  ssp.insert(newstring("Penguin"));

�  Default sort function is provided by STL ¡  In this case default function will do pointer value comparison ¡  For string comparison, you must provide own functor ¡  set<string*, less<string*> > ssp;

Page 30: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Functor for string dereference

struct StringPtrLess: public binary_function<const string*, const string*, bool> { bool operator()(const string *ps1, const string *ps2) const { return *ps1 < *ps2; } }; typedef set<string*, StringPtrLess> StringPtrSet ; StringPtrSet ssp;

Page 31: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Templatized functor for de-reference

struct DereferenceLess { template <typename PtrType> bool operator()(PtrType pT1, PtrType pT2) const value, because we expect them { return *pT1 < *pT2; } };

Page 32: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Comp should return false for equal value 1/2

�  set<int, less_equal<int> > s; // s is sorted by "<=" �  s.insert(10); //insert the value 10 �  Now try inserting 10 again: �  s.insert(10); �  !(10A<= 10B)&&!(10B<= 10A) //test 10Aand 10B for

equivalence �  set concludes that 10A and 10B are not equivalent

Page 33: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Comp should return false for equal value 2/2

�  multiset<int, less_equal<int> > s; // s is still sorted by "<="

�  s.insert(10); //insert 10A �  s.insert(10); // insert 10B

¡  equal_range on it, we'll get back a pair of iterators that define a range containing both copies

¡  equal_range identifies a range of equivalent values

Page 34: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

In-place modification of set/multiset value1/2

�  Map/multimap key modification will not compile �  Set object is not const because you still want to

modify other properties of object �  If property/value used for sorting is modified, the

container is corrupt

Page 35: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

In-place modification of set/multiset value2/2

Page 36: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Sorted vector v/s associative container 1/3

�  Standard associative container ¡  Typically implemented as balanced binary trees ¡  optimized for mix of lookup, insertion and deletion ¡  No way to predict the next operation

�  Typical application usage of container ¡  Setup

÷ All operation are insert/delete, hardly any lookup

¡  Lookup ÷ Almost all operations are lookup

¡  Reorganize ÷ Modify, insert/delete, sort

Page 37: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Sorted vector v/s associative container 2/3

�  For such usage, sorted vector can provide better performance ¡  Size

÷ Less size per element in vector, so storing takes less space ÷ Will lead to less page fault ÷ As vector offer better locality of reference ÷  Insertion/deletion can be costly, so not useful if application

requires too many of these

Page 38: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Sorted vector v/s associative container 3/3

Page 39: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

map::operator[] and map-insert 1/2

�  map::operator[] ¡  Provides add/update functionality ¡  Returns reference to value ¡  If object does not exists,

÷ creates default constructed object ÷  insert in map ÷ Destructor of temporary object ÷ Assignment

¡  If insert is called directly, 3 function calls are saved

Page 40: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

map::operator[] and map-insert 2/2

�  Update ¡  m[k] = v;

÷ Requires no object creation, deletion ¡  m.insert( IntWidgetMap::value_type(k, v)).first->second = v;

÷ Temporary object of Widget is create, destroyed

Page 41: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Iterators

�  Prefer iterator over const_iterator ¡  Many functions use iterator ¡  iterator insert(iterator position, const T& x); ¡  iterator erase(iterator position); ¡  iterator erase(iterator rangeBegin, iterator rangeEnd);

�  Conversions

Page 42: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

const_iterator to iterator 1/2

Page 43: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

const_iterator to iterator 2/2

�  It won’t compile ¡  Distance(InputIterator it1, InputIterator it2) ¡  Cannot accept two different types ¡  Need to specify type of template ¡  advance(i, distance<ConstIter>(i, ci));

�  Efficiency ¡  For random access iterators in vector, string, deque – const ¡  For others its linear

�  Read unformatted char data ifstream inputFile("interestingData.txt"); string fileData((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());

Page 44: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Destination Range should be big enough 1/3

Page 45: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Destination Range should be big enough 2/3

Page 46: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Destination Range should be big enough 3/3

�  back_inserter, calls push_back ¡  Works with sequence container (vector, list, string deque)

�  front_inserter can also be used ¡  List, deque

�  Inserter ¡  Insert at specified position

Page 47: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Sorting Options 1/3

�  Applicable on sequence containers ¡  vector, deque, string, array

�  sort and stable_sort ¡  stable_sort maintains position of equivalent elements

�  partial_sort ¡  e.g. Get best 20 widgets sorted

�  nth element ¡  e.g. Get best 20 widgets, in any order

�  partition/stable_partition ¡  Partition on a criterion

Page 48: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Sorting Options 2/3

Page 49: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Sorting Options 3/3

Page 50: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Remove algorithm on container of pointers

Page 51: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

After remove is called

Page 52: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

After erase is called

Page 53: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Algorithms expecting sorted ranges

�  binary search algorithms ¡  binary_search ¡  lower_bound ¡  upper_bound ¡  equal_range

�  Set Operations ¡  set_union ¡  set_intersection ¡  set_difference ¡  set_symmetric_difference

�  Mege Sorted Ranges ¡  merge ¡  implace_merge

�  Includes ¡  Check if range is present in other range

�  unique �  unique_copy

Page 54: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use same algo for sort and search

Page 55: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Copy_if implemented

Page 56: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Function Objects

�  Passed by value ¡  Should be small ¡  Monomorphic (non-polymorphic)

Page 57: Effective stl notes
Page 58: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Make functors adaptable

Page 59: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

ptr_fun

Page 60: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Adaptable Functors

Page 61: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

not1 and bind2nd

Page 62: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

ptr_fun, mem_fun and mem_fun_ref

�  3 ways to call C++ function

Page 63: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

for_each usage

Page 64: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

for_each implementation

�  for_each is written in a way, its only compatible with call#1

Page 65: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

mem_fun example

Page 66: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer algorithm to hand-written loop 1/2

�  Efficiency ¡  Algorithms are often more efficient than the loops

programmers produce.

�  Correctness ¡  Writing loops is more subject to errors than is calling

algorithms.

�  Maintainability ¡  Algorithm calls often yield code that is clearer and more

straightforward than the corresponding explicit loops.

Page 67: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer algorithm to hand-written loop 2/2

�  Efficiency ¡  Check of end() is done only once ¡  Call to begin, end are generally inlined inside for_each ¡  Have knowledge about internal implementation of container

÷ E.g. using pointers for vector traversal

¡  Implementers use more sophisticated algo than avg c++ programmer

Page 68: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer member function to algorithm 1/2

�  Why ¡  Member functions are faster ¡  They integrate better with container(especially associative)

�  Example

Page 69: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Prefer member function to algorithm 2/2

¡  Member find: About 40 (worst case) to about 20 (average case) ¡  Algorithm find: 1 ,000,000 (worst case) to 500,000 (average

case)

�  Equivalence and eqality ¡  std::find uses equality, set::find uses equivalence

�  list functions ¡  remove, remove_if. unique, sort, merge, and reverse ¡  each std version copy objects ¡  list members do not copy objects ¡  remove_erase is needed if std version used, list::remove works

directly

Page 70: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Search options 1/2

�  Logarithmic complexity ¡  binary_search ¡  lower_bound ¡  upper_bound ¡  equal_range ¡  Uses equivalence

�  Linear Search Complexity ¡  find ¡  count ¡  Use equality

Page 71: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Search options 2/2

�  find v/s count ¡  find stops at match of value ¡  count looks till end ¡  find is efficient for check of existence

�  binary_search ¡  Widget w; //value to search for ¡  if (binary_search(vw.begin(), vw.end(), w))

�  lower_bound ¡  Returns iterators to first match, if not found returns position,

where the element should be inserted

Page 72: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

equal_range

�  equal_range ¡  Returns pair of iterator pointing to first and last occurrence of

match ¡  If not found, both iterator return same position, where the

element should be inserted

Page 73: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

lower_bound v/s upper_bound

Page 74: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Use functor over functions

Page 75: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Function v/s Functor performance

�  Function v/s functor performance ¡  Functor version was 50% to 160% efficient than function ¡  This is because of inlining ¡  std::sort beats C qsort

÷ Sorting of a vector of million doubles was 670% faster

Page 76: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Write – only code

�  get rid of all the elements in the vector whose value is less than x, except that elements preceding the last occurrence of a value at least as big as y should be retained

Page 77: Effective stl notes

Content last revised on 1/4/13 Uttam Gandhi, Software Developer http://www.linkedin.com/pub/uttam-gandhi/21/7aa/24

Make it simple

�  v.f1 (f2(f3(v.f4(), v.f5(), f6(f7(), y)),.f8(), v.f9(), f6(f10(), x)), v.f9());