@@ -33,7 +33,7 @@ <h2>The Organ Grinder</h2>
33
33
This is a very great song by < a href ="https://en.wikipedia.org/wiki/Franz_Schubert "> Franz Schubert</ a > but it also
34
34
perfectly reflects what will go on with the course in how I feel.
35
35
The song is called < a href ="https://www.britannica.com/topic/Winterreise "> “The Organ Grinder”</ a > (Der Leiermann).
36
- The singer is < a href ="https://en.wikipedia.org/wiki/Dietrich_Fischer-Dieskau "> Dietrich fischer-dieskau </ a > maybe the greatest
36
+ The singer is < a href ="https://en.wikipedia.org/wiki/Dietrich_Fischer-Dieskau "> Dietrich Fischer-Dieskau </ a > maybe the greatest
37
37
leader, or art song singer of the last 50, 60, or 70 years.
38
38
He started singing in the late forties.
39
39
Let us spend a couple of minutes and listen to it… (< a href ="https://www.youtube.com/watch?v=sIIS-UgixGE "> Video here</ a > )</ p >
@@ -262,26 +262,25 @@ <h2>Rotate</h2>
262
262
< a name ="Rotate-for-bidirectional-iterators "> </ a >
263
263
< h3 > Rotate for bidirectional iterators</ h3 >
264
264
265
- < p > Once we find where it goes, how do we make room for it?
265
+ < p > Once we find where it goes (the element to insert) , how do we make room for it?
266
266
We “rotate” to the right by one.
267
- If it was a bidirectional iterator there is a beautiful algorithm.
268
- Copy is the wrong thing, because it will overwrite everything
269
- with the same value.
270
- What we want is copying from the back.
267
+ If it is a bidirectional iterator there is a beautiful algorithm, copying from the back.
271
268
The algorithm is called: < a href ="https://en.cppreference.com/w/cpp/algorithm/copy_backward "> < code > std::copy_backward</ code > </ a > .</ p >
272
269
273
270
< pre > < code > template <typename I>
274
271
// I is BidirectionalIterator
275
272
void rotate_right_by_one(I first, I last, std::bidirectional_iterator_tag) {
276
273
typedef typename std::iterator_traits<I>::value_type T;
277
- T butlast = last;
274
+ I butlast = last;
278
275
--butlast;
279
276
T x = *butlast;
280
277
std::copy_backward(first, butlast, last);
281
278
*first = x;
282
279
}
283
280
</ code > </ pre >
284
281
282
+ < p > Note that forward copy is the wrong thing, because it will overwrite everything with the same value (namely the first value).</ p >
283
+
285
284
< a name ="Rotate-for-forward-iterators "> </ a >
286
285
< h3 > Rotate for forward iterators</ h3 >
287
286
@@ -355,7 +354,7 @@ <h3>Rotate for forward iterators</h3>
355
354
void rotate_right_by_one(I first, I last, std::forward_iterator_tag) {
356
355
if (first == last) return;
357
356
I current = first;
358
- while (++current != last) std::swap(first, current);
357
+ while (++current != last) std::swap(* first, * current);
359
358
}
360
359
</ code > </ pre >
361
360
@@ -364,8 +363,8 @@ <h3>Rotate for forward iterators</h3>
364
363
365
364
< pre > < code > template <typename I>
366
365
inline
367
- void rotate_right_by_one(I first, I butlast, I last) {
368
- rotate_right_by_one(first, butlast, last, typename std::iterator_traits<I>::iterator_category());
366
+ void rotate_right_by_one(I first, I last) {
367
+ rotate_right_by_one(first, last, typename std::iterator_traits<I>::iterator_category());
369
368
}
370
369
</ code > </ pre >
371
370
0 commit comments