Hi @xxvms, I'll try for a better explanation.
You initially entered 5 words and they were put into the vector words[] in the initial order:
words[0]="one", words[1]="two", words[2]="three", words[3]="four", words[4]="one".
You called a routine sort(), which rearranged these into "lexicographical order", so that vector words[] HAS BEEN REARRANGED and now holds
words[0]="four", words[1]="one", words[2]="one", words[3]="three", words[4]="two"
(The only quibble I have here is that Stroustrup seems to have created his own sort() routine; the std::sort() algorithm is described at
http://www.cplusplus.com/reference/algorithm/sort/.
Also, sorting order for strings could potentially be non-portable if some started with upper case and some with lower case, though that's not a problem here.)
You have a for loop, containing a single if statement; the latter only prints out a word if it is either the first word or, for subsequent ones, doesn't match its PREDECESSOR. So with your current vector (i.e., after sorting "alphabetically"), which I repeat as
words[0]="four", words[1]="one", words[2]="one", words[3]="three", words[4]="two"
then
- prints out words[0]="four"
- prints out words[1]="one"
- doesn't print words[2] because it is the same as words[1]
- prints out words[3]="three"
- prints out words[4]="two"
I'm currently reading (in snippets, on the train) Stroustrup's book "The C++ Programming Language, 4th ed." and greatly enjoying it. However, if it had been the first book on C++ that I ever read then I would have had huge difficulty. I needed the material on this site to bring myself up to the level where I could follow and benefit from Stroustrup's books. Perhaps it is easier for Computer Science students.