mardi 3 juillet 2018

Function that returns an unordered_map

I have an assignment where I'm supposed to make a function that takes a single string filename input, and returns an unordered_map. I basically know what to do inside of my function, but I don't know what variable type to make my function in order for me to be able to return an unordered_map. In order just to be able to write stuff in my function, I just made it an int variable type, but I obviously can't return an unordered_map. Would really appreciate some help! I'll attach my code.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>

using namespace std;

int count_words(string filename);

int main() {

    count_words("trainneg.txt");

    return 0;
}

int count_words(string filename) {

    ifstream ifs(filename);

    string line;
    unordered_map<string, int> word_counter;

    while (getline(ifs, line)) {
        istringstream iss(line);
        string word;
        int count;

            while (getline(iss, word, ' ')) {
                word_counter[word] += 1;
            }

    }

    return word_counter;
    }

Aucun commentaire:

Enregistrer un commentaire