Why???

Hi there

I have bit of puzzle with this code, (again from book Programming Principles and Practise Using C++)

In short this code should check for repeating words, my understanding is that
it checks words that next to each other words[i - 1] != words[i]
and if they are not the same it adds to the counter plus only prints words that don't repeat.

However when I enter following sequence

one two three four one (then CTRL Z an Enter)

I got answer:
Number of words:5 (this is correct)
Then:
four
one
three
two

Why in such order?????

thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "std_lib_facilities.h"

int main()
{
	vector<string>words;
	for (string temp; cin >> temp;) // read white space separated words
		words.push_back(temp); // puts word into vector
	cout << "Number of words: " << words.size() << '\n';

	sort(words); // sort the words

	for (int i = 0; i < words.size(); ++i)
		if (i == 0 || words[i - 1] != words[i]) // is this a new word?
			cout << words[i] << "\n";

	system("pause"); // press CTRL Z then enter to stop

   return 0;
}
After a big of thinking I figured out why words are in such order :D
sort(words);

but one remaining question is, does this code search across all words and skips repeated word or just words that are next to each other?

thanks :)
Hi @xxvms

When you enter your example sequence "one two three four one" (I'm assuming that) sort() puts them in "lexicographical order" - roughly dictictionary order - for character strings, making the sequence
"four one one three two"

The "one"s are now next to each other, so the second one won't be printed as a duplicate. From its position in your code, and as a consequence of the prior sorting, the "if" statement will just skip words that are next to each other.
Last edited on
Hi Lastchance,


I was actually thinking about this and wondered if that would be the case.

so what you say is that sort would put words in alphabetical order, in that case if you check 1st and 2nd word, if this is same word (one[0], one[1]) that would eliminate that one word "one", then second pair of words one[1] and two[2] is not the same and it would be skipped. (i am not following alphabetic order now)

were as number in brackets is like vector/array number of the word in string.

if that makes sense ;)
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.
Last edited on
Hi Lastchance

thanks for detailed explanation!

I was doing more exercises with code from the book and yes, your words are 100% true :)

My book (2nd edition)
https://goo.gl/IJPpWw
I think is best from all books I have, but I agree with you, Mr Bjarne sometimes put bar a bit to high and at the time might be difficult to follow. (ie my post here)
I have started back in December last year, and I am following on-line course, plus reading articles books etc and with this I can slowly move along in the book.

:)
Topic archived. No new replies allowed.