I don't really understand the code but it compiles with no error but a warning about the '<' in the for loop. I got it from the programming practice and principles using c++ book. I don't understand that reference page
You should read better. If you want to use C++, reference page will become your close friend :)
It first creates vector of strings(which is a container; you can im as an array, but you don't have to specify its size, and has many other functions. As you learn you will also learn other containers, but vector is pretty simple yet useful container).
Then it creates a string, called temp.
Later, we get into loop. It gets input from user, until user provides no valid input, and puts it(input) at the end of vector. It then prints you number of words in vector.
Then, it sorts vector(alphabetically), using std::sort.
Later it prints all words in vector.
You can try it yourself - if you input "hi", "I", "am", "text", you will get "I am hi text" output, each string in other line.("I" will be first, because it's uppercase.
@OP - Too bad. You should learn arrays before vectors, because it's hard to tell you what [] operator is doing when you don't know arrays. I guess you'd better learn something(either online, or from book) about arrays. [] operator works basically the same as in arrays. I can't really explain it to you, as it would take much time, and besides, there are people that could do it better then me :)
'i' allows you to show all elements from vector. It is greater with each loop iteration. It will get clearer if you check arrays, I guess. You use for loop and 'i' quite often with arrays.
words[i] prints the element at index i (which will be the (i-1)th word since indices start at 0).
This code basically grabs a list of words from the user, sorts them, and then prints out all distinct words (so if your input was "hello hello hello there", the output will be "hello there").
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector <std::string> words; // Initialize a vector of strings
std::string temp; // Temp variable to hold our input
while (std::cin >> temp) // While we still have another word to grab from the user
words.push_back(temp); // Add it to our vector
std::cout << "Number of words: " << words.size() << "\n\n";
std::sort (words.begin(), words.end()); // Sorts the entire vector
for (int i = 0; i < words.size(); i++)
if (i == 0 // If we're on the first word (indices start at 0)
|| words[i - 1] != words[i]) // or if the current word is different from the last one
std::cout << words[i] << "\n\n"; // Print it to the screen
return 0;
}
At line 18, you're getting a warning because of a signedness mismatch. This *could* cause an infinite looping condition if 'words' contains enough elements to cause the counter (i) to overflow.
To resolve that warning, change line 18 to:
for(std::vector<std::string>::size_type i = 0; i<words.size(); i++)
This will ensure in a cross-platform, cross-compiler manner that an overflow will never happen, and thus will make your warning disappear.