"STL from the Ground Up"



Hi

Since "STL from the Ground Up" is avaliable on the shelf of a programmer at
work by Shildt, you-know-who's, never-done-anything-wrong,
motives-always-good author, I thought I would check it out.
Copyright is dated 1999.

1. I was shocked to find on page 227 the wrong return value described in the
event of lower_bound(), upper_bound() and equal_range():

"template <class ForIter, class T> pair <ForIter,
ForIter>equal_range(ForIter start, ForIter end, const T &val);
template <class ForIter, class T> ForIter lower_bound(ForIter start, ForIter
end, const T &val);
template <class ForIter, class T> ForIter upper_bound(ForIter start, ForIter
end, const T &val);

The lower_bound algorithm returns an iterator to the first element that
matchs val, upper_bound returns an iterator one beyond the last matching
element, and equal_range returns a pair of iterators that point to the lower
and upper bounds. If the sought-after element is not in the sequence, these
functions return iterators to end()."

Well that is wrong. Amazingly so.
Given the sequence of integers 1,3,3,3,5,7 in a vector v, no other
elements, searching for 4 using

vector<int>::iterator it1 = lower_bound(v.begin(), v.end(), 4);
vector<int>::iterator it2 = upper_bound(v.begin(), v.end(), 4);

then both it1, it2 will be pointing at 5, the place at which 4 can be
inserted while still maintaing the sorted sequence. it1 and it2 will not be
v.end().
it1 and it2 would be v.end() if the element being searched for was greater
than the maximum element.

2. On page 236, it says

"IN DEPTH

Although the STL supplies a left-rotate algorithm, it does not provide one
that right-rotates.
At first this might seems a serious flaw in the STL's design, or at least a
troublesome omission.
But neither is the case. To perform a right-rotate you simply use the
rotate() algorithm, but call it using reverse iterators.
Since reverse iterators run backwards, the net effect of such a call is that
right-rotate is performed on the sequence!"

He is right here, you can do this. But it completely unnecessary to use
reverse iterators.
He does not seem to be aware that a right-rotate by 2 elements is equivalent
to a left-rotate by all elements - 2.
His

rotate(v.rbegin(), v.rbegin()+2, v.rend());

is equivalent to

rotate(v.begin(), v.begin()+v.size() - 2 , v.end());

Seems like an authors mental block.

I am wondering what other horrors are in the book.

Stephen Howe


.



Relevant Pages

  • Re: "STL from the Ground Up"
    ... template <class ForIter, class T> ForIter lower_bound(ForIter start, ForIter ... and equal_range returns a pair of iterators that point to the lower ... If the sought-after element is not in the sequence, ... To perform a right-rotate you simply use the ...
    (comp.programming)
  • Re: Python syntax in Lisp and Scheme
    ... predicates absolutely HAVE to be finite. ... The first 'sequence' argument COULD syntactically be an ... of predicates applied to a potentially unbounded/infinite sequence. ... As such sequences, in Python, are represented by iterators, we ...
    (comp.lang.python)
  • Re: Python syntax in Lisp and Scheme
    ... predicates absolutely HAVE to be finite. ... The first 'sequence' argument COULD syntactically be an ... of predicates applied to a potentially unbounded/infinite sequence. ... As such sequences, in Python, are represented by iterators, we ...
    (comp.lang.lisp)
  • Re: string upper, string lower, string subthisforthat
    ... | This requirement appears in the public draft standard (which is what ... as 'transform' can be used to modify a sequence in place ... | (i.e. one of the input iterators can be the same as the output ...
    (alt.comp.lang.learn.c-cpp)
  • Re: What about an EXPLICIT naming scheme for built-ins?
    ... Alex's idiom: ... the list comprehension was more intuitive for me. ... My explanation is that iterators and list comprehensions are ... sequence or tuple. ...
    (comp.lang.python)