Use std::map to store responses from a certain function

  aiml, algorithm, c++, ml

using namespace std;


string readFileIntoString(const string& path) {
    ifstream input_file(path);
    if (!input_file.is_open()) {
        cerr << "Could not open the file - '"
             << path << "'" << endl;
        exit(EXIT_FAILURE);
    }
    return string((std::istreambuf_iterator<char>(input_file)), std::istreambuf_iterator<char>());
}
void wordsCount(string str){

      int count = 1;
      std::map<string,int> wordsMap;
      istringstream wordStream(str);
      string word;
       while(wordStream >> word){

             pair<map<string,int>::iterator,bool> retrunValue;


            retrunValue = wordsMap.insert( pair<string,int>(word,count));

            if (retrunValue.second==false)
            {
                  
                  ++retrunValue.first->second; 
            }     
       }
       map<string,int>::iterator itr ;

       for (itr = wordsMap.begin(); itr != wordsMap.end(); ++itr){

               cout << 't' << itr->first ;
               cout<< 't' << itr->second << 'n';
       }
}

int main(){
    string filename=("test.txt");
    string file_contents;
    file_contents = readFileIntoString(filename);
    cout << file_contents << endl;
    wordsCount(file_contents); 
    return 0;

}

I found this code on my college platform , I’m trying to modify this to find intersection between two text file by doing something like so that I can then use this to check for (cosine similarity) https://en.wikipedia.org/wiki/Cosine_similarity

set<int> intersect;
set_intersection(vector1.begin(), vector1.end(), vector2.begin(), vector2.end(),
                 std::inserter(intersect, intersect.begin()));

Source: Windows Questions C++

LEAVE A COMMENT