Finding common words in a file using lexicographical matching [ help ]

I'm supposed to determine and list the 50 most common words in a file.

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 <fstream>
#include <string>

using namespace std;

int main()
{

    string filename;

    // Get the filename.
    cout << "Enter the file you wish to have searched:\n";
    cin >> filename;

    // Open file.
    ifstream file(filename.c_str());

    // Read in all the words.
    string word;

/////??? search for matches lexicographically ????///////////


Now after this, I'm not sure what to do. My teacher wants me to search for matches lexicographically but I don't know how to do that. Could anyone please offer help on how to search for word matches lexicographically. Then I will be outputting the top 50 most used words after finding these matches lexicographically.
> how to search for word matches lexicographically

Use the std::string comparison operators ( ==. < etc. ).
http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp

You might want to use a std::map< std::string, int > to get the word frequencies.
http://www.cprogramming.com/tutorial/stl/stlmap.html
1
2
3
4
5
6
7
8
9
10
11
12
    //Get the filename.
    cout << "Enter the file you wish to have searched:\n";
    cin >> filename;
    // right

    // Open file.
    ifstream file(filename.c_str());
    // right

    // Read in all the words.
    string word;
    // not really, no 


If the words in the file are on different lines, which is usually the case, you read the file line by line and input the words into an array of strings. I propose you use std::vector<std::string> a for reason apparent later.

Now, you create another vector b of strings to hold distinct words, and another vector c that serves as a counter. You iterate over the elements of a, if an element is identical to the one before it, increment the corresponding cell in c, else, push it to b and push 1 to c...

This is the general idea. Try working that out.


HINT: How do you look for a word in the dictionary?
Last edited on
Topic archived. No new replies allowed.