.the program is running .display the frequency of words and sort them from highest count to lowest.
my problem is about the duplication of the counts or value of the words.if the value of the word are same it only display the first word&value ...can some one help

here is the program

#include <iostream>
#include <map>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <iterator>
#include <set>

using namespace std;
struct sortPairSecond
{
bool operator()(const pair<string, int> &lhs, const pair<string, int> &rhs)
{
return rhs.second < lhs.second;
}
};

typedef map<string,int> word_count_list;

int main()
{

word_count_list word_count;
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;

while (file >> word)
{
// Remove punctuation.
int index;
while ((index = word.find_first_of(".,!?\\;-*+")) != string::npos)
{
word.erase(index, 1);
}

++word_count[word];
}

// Print out the word counts.

set<pair<string,int>, sortPairSecond > mySet;
for(map<string, int>::const_iterator it = word_count.begin(); it != word_count.end(); ++it)
{
mySet.insert(*it);
}

cout << "\nSet Order:\n--------------\n";
for(set<pair<string, int> >::const_iterator it = mySet.begin(); it != mySet.end(); ++it)
{
cout << it->first << " = " << it->second << "\n";
}
system("pause");
}

Recommended Answers

All 5 Replies

You didn't use code-tags, even though there's a big button saying code.
Put more effort into making your question presentable, and I'll take a look at the code for you.

im sorry .im a newbie here .sorry sorry. now i fixed it up

commented: No you didn't -6

ok ok i got it now right i copy it to vector and sort it..... here is the code

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>

using namespace std;

typedef map<string,int> word_count_list;

struct val_lessthan : binary_function < pair<string,int>, pair<string,int>, bool >
{
  bool operator() (const pair<string,int>& x, const pair<string,int>& y) const
    {return x.second<y.second;}
}val_lt;

int main()
{
    word_count_list word_count;
    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;

    while (file >> word){
        // Remove punctuation.
        int index;
        while ((index = word.find_first_of(".,!?;-*+")) != string::npos)
            word.erase(index, 1);

        ++word_count[word];
    }

    //copy pairs to vector
    vector<pair<string,int> > wordvector;
    copy(word_count.begin(), word_count.end(), back_inserter(wordvector));

    //sort the vector by second (value) instead of key
    sort(wordvector.begin(), wordvector.end(), val_lt);

    for(int i=0; i<wordvector.size(); ++i)
        cout << wordvector[i].first << " = " << wordvector[i].second << endl;

    return 0;
}

to finish the porgram. i need to output the 20 most common or used words....

I'm wondering: Why did you, again, post code without using code-tags? William Hemsworth specifically asked you in post #2?

Also: marking your thread 'solved' when you still have a question won't get you replies. You should start a new thread with your new question. And this time use code-tags when posting code.

im sorry im new here i dnt know how things work out here ...sorry again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.