-1

My task is that I don't know number of words in a file and the words are repeating several times,but how many times - It's unknown and I have to find that words. I use classes and vector to work with words,and fstream to work with files. But I cannot find resource or algorithm of finding repeating words and I'm so puzzled. I have vector of variable type and I pushed the words in it. It works successfully,I test it with v.size() output. I made all of things except algorithm of finding repeating words,which solve turned difficult to me.

My full code that I wrote:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <iterator>
using namespace std;
class Wording {
private:
    string word;
    vector <string> v;
public:

    Wording(string Alternateword, vector <string> Alternatev) {
        v = Alternatev;
        word = Alternateword;
    }
};
int main() {
    ifstream ifs("words.txt");
    ofstream ofs("wordresults.txt");
    string word;
    vector <string> v;
    Wording obj(word,v);
    while(ifs >> word) v.push_back(word);
    for(int i=0; i<v.size(); i++) {

        //waiting for algorithm
        //ofs << v[i] << endl;
    }
    return 0;
}
getsadzeg
  • 640
  • 3
  • 9
  • 19
  • Please provide us the code you are testing. – Thealon Apr 23 '15 at 12:13
  • 1
    possible duplicate of [Elegant ways to count the frequency of words in a file](http://stackoverflow.com/questions/4888879/elegant-ways-to-count-the-frequency-of-words-in-a-file) – NathanOliver Apr 23 '15 at 12:14
  • I've posted full my code. @NathanOliver,I see that question,but it's difficult to understand that codes,I think. – getsadzeg Apr 23 '15 at 12:16
  • Do you want to save the repeated words multiple time or just one. – Abdullah Apr 23 '15 at 12:16
  • just one. I want to save repeating words in file "wordresults.txt" just one time. – getsadzeg Apr 23 '15 at 12:18
  • If you don't care about the frequency but just want what words are repeated then use an [`std::unordered_set`](http://www.cplusplus.com/reference/unordered_set/unordered_set/) – NathanOliver Apr 23 '15 at 12:22

2 Answers2

2

Try using a hash map. If you are using gnu c++, it's std::hash_map. In C++11, you could use std::unordered_map, which would give you the same capabilities. Otherwise, hash_map is available from Boost, and probably elsewhere.

Key concept here is hash_map<word, count>.

tauchris
  • 33
  • 4
  • `hash_map` is far better solution but in think its out of scope of asker.Just Saying – Abdullah Apr 23 '15 at 12:23
  • @AbdullahTariq What makes you think it's out of scope? –  Apr 23 '15 at 12:33
  • @AbdullahTariq His profile says he doesn't want to use hash maps??!! –  Apr 23 '15 at 12:41
  • @Alex it says he does't have enough experience in this to understand hash_maps comprehensively, i am not trying to target someone limits here but when help someone you keep in view the understanding of counter person. On the other hand you can argue that he will never understand if he did't try. No Offense dude. Take care bye, – Abdullah Apr 23 '15 at 12:46
  • Yes,Abdullah you're right. I haven't much experience. – getsadzeg Apr 23 '15 at 13:11
0

Is the unique words in input file what you want? If so then you can do this with set (unordered_set if you don't really need them to be sorted) like so:

std::set<std::string> words; //can be changed to unordered_set
std::copy(ifs, std::ifstream(), std::inserter(words, words.begin());
std::copy(words.begin(), words.end(), ostream_iterator<std::string>(ofs));

You can also use vector, but you'll have to sort it and then use unique on it.

I can't compile this code now, so there might be some errors in my code snippet.

If what you want is the number of occurrences of a different words in file then you'll have to use some kind of map, as was already suggested. Of course using vector, sorting it and then counting consecutive words is also an solution, but wouldn't be too clear.

Dino
  • 599
  • 1
  • 9
  • 20