-6

I am a new here. I do not understand the if statement: i==0, It eliminates repetition. How it works? Thanks.

vector<string>words;
for (string temp; cin >> temp;)
words.push_back(temp);
cout << "Number of words:" << words.size() << '\n';
sort(words);
for (int i = 0; i < words.size(); ++i)
    if (i == 0 || words[i - 1] != words[i])
        cout << words[i] << '\n';
zen
  • 11
  • 1
  • it prints word only if `words[i - 1] != words[i]` - it is different from the previous one. `i ==0` is just protection - since index 0 has no previous one. – Artemy Vysotsky Sep 02 '17 at 13:26
  • 4
    Please pick up [a good beginner C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). And [`using namespace std;` is a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)! – tambre Sep 02 '17 at 13:27
  • Run it in a debugger with a set of known input strings (including some words used more than once), and see what happens while you single step though the last loop. – WhozCraig Sep 02 '17 at 13:29

3 Answers3

0

If i equals 0, then you can't look words[i-1] because you can't do words[-1]

Furthermore, when you use || operator, if the first expression is true, the second expression is not checked

With i == 0 || words[i - 1] != words[i] you can print your first words because i equals 0 and the expression words[i - 1] != words[i] isn't checked and doesn't crash your program ! then with i different from 0, the first expresion isn't true and the second is checked.


For the unrepetition part :

Your array is sorted, so same words are one after another. Then you have to check if the previous word isn't the same, you can print the word


How words[i - 1] != words[i] works :

for std::string, operators == and != look for the length of each string, and each character in the string

Comparison operator for std::string

Moreover, words[i-1] look for the previous words, and words[i] for the current one, to compare them.

So here, the expression is true if the two consecutives words aren't the same, in length and letters. if you have words dog cat cat cat_ in your array, dog is printed first (because of the i == 0 part), the second word cat is printed, then the epression is false because the words are identical ("cat" == "cat"), and finaly, cat_is printed because different from cat

Jérémy Blain
  • 249
  • 2
  • 14
0

This program is first sorting all the words which are a vector of strings, and prints only unique word.

i==0 means the first word, Since you can't compare the first word with any previous so it will always be unique(from its previous words which doesn't exist)

word[i-1]!=word[i] check if the current word is different from previous the print that word.

|| is a Logical Or operator.

Ishpreet
  • 5,230
  • 2
  • 19
  • 35
-1

looks like your program prints the first word, and every other word that isn't repeated in a sorted list of words. If you're trying to look for unique words, try using std::unique.

user0042
  • 7,917
  • 3
  • 24
  • 39
SagunKho
  • 985
  • 10
  • 26