Vector

closed account (iAk3T05o)
Please can someone explain this code. I got it from the programming practice and principles book.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
std::vector <std::string> words;
std::string temp;

while (std::cin >> temp)
words.push_back(temp);

std::cout << "Number of words: " << words.size() << "\n\n";

std::sort (words.begin(), words.end());

for (int i =  0; i<words.size(); i++)
if (i == 0 || words[i - 1]!= words[i])
std::cout << words[i] << "\n\n";

return 0;
}

Thanks.
Last edited on
I recommend you checking reference site ( http://www.cplusplus.com/reference/vector/vector/ ) and check function for vector, and read examples. This code won't teach you much.
Last edited on
closed account (iAk3T05o)
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
@MatthewRock: ¿what? ¿did the code changed in those 2 minutes?

@OP: indent your code.
¿what you don't understand?
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.
Last edited on
closed account (iAk3T05o)
@matthew: the book teaches vectors before arrays so i don't know what arrays are . . . yet.

I don't understand what 'i' does in the code. Why is there a for loop and why is it 'std::cout<<words[i];' and not 'std::cout<<words;'.
Last edited on
@ne555 - yes :)

@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.
closed account (iAk3T05o)
Hmm. I hope the book explains arrays more clearly than this site's pdf. It's confusing.
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").

It's like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
}
closed account (3hM2Nwbp)
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.
closed account (iAk3T05o)
Ok. Let me learn arrays. Thanks.
Topic archived. No new replies allowed.