Write map to file using for_each












0















The following code compiles and executes, but nothing is written to the file and I cannot for the life of me figure out why. The file is being created, but the file is empty. I made sure to delete the file each time just in case but that shouldn't make a difference



#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <map>
#include <set>
using namespace std;

int main(){

set<string> exclusionSet;
ifstream stopWords("stopwords.txt");
copy(istream_iterator<string>(stopWords),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()));

stopWords.close();

//for_each(exclusionSet.begin(),
// exclusionSet.end(),
// (const string& s){cout<<s<<endl;});

map<string, int> wordMap;
ifstream words("sample_doc.txt");
copy_if(istream_iterator<string>(words),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()),
[=](const string& s){return exclusionSet.find(s)!=exclusionSet.end();});

ofstream out("frequency.txt");

for_each(begin(wordMap),
end(wordMap),
[&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
out.close();
return 0;
}









share|improve this question


















  • 3





    You never insert anything into wordMap.

    – Miles Budnek
    Nov 22 '18 at 5:48
















0















The following code compiles and executes, but nothing is written to the file and I cannot for the life of me figure out why. The file is being created, but the file is empty. I made sure to delete the file each time just in case but that shouldn't make a difference



#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <map>
#include <set>
using namespace std;

int main(){

set<string> exclusionSet;
ifstream stopWords("stopwords.txt");
copy(istream_iterator<string>(stopWords),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()));

stopWords.close();

//for_each(exclusionSet.begin(),
// exclusionSet.end(),
// (const string& s){cout<<s<<endl;});

map<string, int> wordMap;
ifstream words("sample_doc.txt");
copy_if(istream_iterator<string>(words),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()),
[=](const string& s){return exclusionSet.find(s)!=exclusionSet.end();});

ofstream out("frequency.txt");

for_each(begin(wordMap),
end(wordMap),
[&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
out.close();
return 0;
}









share|improve this question


















  • 3





    You never insert anything into wordMap.

    – Miles Budnek
    Nov 22 '18 at 5:48














0












0








0








The following code compiles and executes, but nothing is written to the file and I cannot for the life of me figure out why. The file is being created, but the file is empty. I made sure to delete the file each time just in case but that shouldn't make a difference



#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <map>
#include <set>
using namespace std;

int main(){

set<string> exclusionSet;
ifstream stopWords("stopwords.txt");
copy(istream_iterator<string>(stopWords),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()));

stopWords.close();

//for_each(exclusionSet.begin(),
// exclusionSet.end(),
// (const string& s){cout<<s<<endl;});

map<string, int> wordMap;
ifstream words("sample_doc.txt");
copy_if(istream_iterator<string>(words),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()),
[=](const string& s){return exclusionSet.find(s)!=exclusionSet.end();});

ofstream out("frequency.txt");

for_each(begin(wordMap),
end(wordMap),
[&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
out.close();
return 0;
}









share|improve this question














The following code compiles and executes, but nothing is written to the file and I cannot for the life of me figure out why. The file is being created, but the file is empty. I made sure to delete the file each time just in case but that shouldn't make a difference



#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <map>
#include <set>
using namespace std;

int main(){

set<string> exclusionSet;
ifstream stopWords("stopwords.txt");
copy(istream_iterator<string>(stopWords),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()));

stopWords.close();

//for_each(exclusionSet.begin(),
// exclusionSet.end(),
// (const string& s){cout<<s<<endl;});

map<string, int> wordMap;
ifstream words("sample_doc.txt");
copy_if(istream_iterator<string>(words),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()),
[=](const string& s){return exclusionSet.find(s)!=exclusionSet.end();});

ofstream out("frequency.txt");

for_each(begin(wordMap),
end(wordMap),
[&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
out.close();
return 0;
}






c++ stdmap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 5:45









Shane MurphyShane Murphy

142




142








  • 3





    You never insert anything into wordMap.

    – Miles Budnek
    Nov 22 '18 at 5:48














  • 3





    You never insert anything into wordMap.

    – Miles Budnek
    Nov 22 '18 at 5:48








3




3





You never insert anything into wordMap.

– Miles Budnek
Nov 22 '18 at 5:48





You never insert anything into wordMap.

– Miles Budnek
Nov 22 '18 at 5:48












2 Answers
2






active

oldest

votes


















0














I would suggest the following version of the fixes



int main()
{
map<string, int> wordMap;

set<string> exclusionSet;
ifstream stopWords("stopwords.txt");
copy(istream_iterator<string>(stopWords), istream_iterator<string>(), inserter(exclusionSet, exclusionSet.end()));
stopWords.close();

vector<string> strings;
ifstream words("sample_doc.txt");
copy_if(istream_iterator<string>(words), istream_iterator<string>(), inserter(strings, strings.end()),
[=](const string& s)
{
return exclusionSet.find(s) == exclusionSet.end();
});
words.close();

for(auto &a : strings)
wordMap[a] = wordMap[a] + 1;

ofstream out("frequency.txt");

for_each(begin(wordMap), end(wordMap),
[&out](const pair<const string, int>& e)
{
out << e.first << " " << e.second << endl;
});

out.close();
return 0;
}


Main differences:




  1. reading the data from the file "sample_doc" in the vector strings, checking whether they are in exclusionSet (fix yours find_if call)


  2. forming words frequency statistics with:



    for(auto &a : strings)
    wordMap[a] = wordMap[a] + 1;



And.... I'd rather replace for_each to range-based for, like this:



for(const auto &e: wordMap)
out << e.first << " " << e.second << endl;





share|improve this answer


























  • Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

    – Shane Murphy
    Nov 23 '18 at 5:55



















0














I don't have much to add on top of @snake_style's answer, but I would like to point out that you can easily drop the complexity/expense of using copy_if to filter the words in sample_doc.txt by using a simple while loop:



#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <map>
#include <set>
using namespace std;

int main(){
set<string> exclusionSet;
ifstream stopWords("stopwords.txt");
copy(istream_iterator<string>(stopWords),
istream_iterator<string>(),
inserter(exclusionSet, exclusionSet.end()));

stopWords.close();

map<string, int> wordMap;
ifstream words("sample_doc.txt");
string s;

while(words >> s) if (exclusionSet.count(s)==0) ++wordMap[s];

ofstream out("frequency.txt");

for_each(begin(wordMap),
end(wordMap),
[&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
out.close();
return 0;
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424576%2fwrite-map-to-file-using-for-each%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I would suggest the following version of the fixes



    int main()
    {
    map<string, int> wordMap;

    set<string> exclusionSet;
    ifstream stopWords("stopwords.txt");
    copy(istream_iterator<string>(stopWords), istream_iterator<string>(), inserter(exclusionSet, exclusionSet.end()));
    stopWords.close();

    vector<string> strings;
    ifstream words("sample_doc.txt");
    copy_if(istream_iterator<string>(words), istream_iterator<string>(), inserter(strings, strings.end()),
    [=](const string& s)
    {
    return exclusionSet.find(s) == exclusionSet.end();
    });
    words.close();

    for(auto &a : strings)
    wordMap[a] = wordMap[a] + 1;

    ofstream out("frequency.txt");

    for_each(begin(wordMap), end(wordMap),
    [&out](const pair<const string, int>& e)
    {
    out << e.first << " " << e.second << endl;
    });

    out.close();
    return 0;
    }


    Main differences:




    1. reading the data from the file "sample_doc" in the vector strings, checking whether they are in exclusionSet (fix yours find_if call)


    2. forming words frequency statistics with:



      for(auto &a : strings)
      wordMap[a] = wordMap[a] + 1;



    And.... I'd rather replace for_each to range-based for, like this:



    for(const auto &e: wordMap)
    out << e.first << " " << e.second << endl;





    share|improve this answer


























    • Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

      – Shane Murphy
      Nov 23 '18 at 5:55
















    0














    I would suggest the following version of the fixes



    int main()
    {
    map<string, int> wordMap;

    set<string> exclusionSet;
    ifstream stopWords("stopwords.txt");
    copy(istream_iterator<string>(stopWords), istream_iterator<string>(), inserter(exclusionSet, exclusionSet.end()));
    stopWords.close();

    vector<string> strings;
    ifstream words("sample_doc.txt");
    copy_if(istream_iterator<string>(words), istream_iterator<string>(), inserter(strings, strings.end()),
    [=](const string& s)
    {
    return exclusionSet.find(s) == exclusionSet.end();
    });
    words.close();

    for(auto &a : strings)
    wordMap[a] = wordMap[a] + 1;

    ofstream out("frequency.txt");

    for_each(begin(wordMap), end(wordMap),
    [&out](const pair<const string, int>& e)
    {
    out << e.first << " " << e.second << endl;
    });

    out.close();
    return 0;
    }


    Main differences:




    1. reading the data from the file "sample_doc" in the vector strings, checking whether they are in exclusionSet (fix yours find_if call)


    2. forming words frequency statistics with:



      for(auto &a : strings)
      wordMap[a] = wordMap[a] + 1;



    And.... I'd rather replace for_each to range-based for, like this:



    for(const auto &e: wordMap)
    out << e.first << " " << e.second << endl;





    share|improve this answer


























    • Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

      – Shane Murphy
      Nov 23 '18 at 5:55














    0












    0








    0







    I would suggest the following version of the fixes



    int main()
    {
    map<string, int> wordMap;

    set<string> exclusionSet;
    ifstream stopWords("stopwords.txt");
    copy(istream_iterator<string>(stopWords), istream_iterator<string>(), inserter(exclusionSet, exclusionSet.end()));
    stopWords.close();

    vector<string> strings;
    ifstream words("sample_doc.txt");
    copy_if(istream_iterator<string>(words), istream_iterator<string>(), inserter(strings, strings.end()),
    [=](const string& s)
    {
    return exclusionSet.find(s) == exclusionSet.end();
    });
    words.close();

    for(auto &a : strings)
    wordMap[a] = wordMap[a] + 1;

    ofstream out("frequency.txt");

    for_each(begin(wordMap), end(wordMap),
    [&out](const pair<const string, int>& e)
    {
    out << e.first << " " << e.second << endl;
    });

    out.close();
    return 0;
    }


    Main differences:




    1. reading the data from the file "sample_doc" in the vector strings, checking whether they are in exclusionSet (fix yours find_if call)


    2. forming words frequency statistics with:



      for(auto &a : strings)
      wordMap[a] = wordMap[a] + 1;



    And.... I'd rather replace for_each to range-based for, like this:



    for(const auto &e: wordMap)
    out << e.first << " " << e.second << endl;





    share|improve this answer















    I would suggest the following version of the fixes



    int main()
    {
    map<string, int> wordMap;

    set<string> exclusionSet;
    ifstream stopWords("stopwords.txt");
    copy(istream_iterator<string>(stopWords), istream_iterator<string>(), inserter(exclusionSet, exclusionSet.end()));
    stopWords.close();

    vector<string> strings;
    ifstream words("sample_doc.txt");
    copy_if(istream_iterator<string>(words), istream_iterator<string>(), inserter(strings, strings.end()),
    [=](const string& s)
    {
    return exclusionSet.find(s) == exclusionSet.end();
    });
    words.close();

    for(auto &a : strings)
    wordMap[a] = wordMap[a] + 1;

    ofstream out("frequency.txt");

    for_each(begin(wordMap), end(wordMap),
    [&out](const pair<const string, int>& e)
    {
    out << e.first << " " << e.second << endl;
    });

    out.close();
    return 0;
    }


    Main differences:




    1. reading the data from the file "sample_doc" in the vector strings, checking whether they are in exclusionSet (fix yours find_if call)


    2. forming words frequency statistics with:



      for(auto &a : strings)
      wordMap[a] = wordMap[a] + 1;



    And.... I'd rather replace for_each to range-based for, like this:



    for(const auto &e: wordMap)
    out << e.first << " " << e.second << endl;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 '18 at 7:17

























    answered Nov 22 '18 at 7:06









    snake_stylesnake_style

    1,170410




    1,170410













    • Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

      – Shane Murphy
      Nov 23 '18 at 5:55



















    • Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

      – Shane Murphy
      Nov 23 '18 at 5:55

















    Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

    – Shane Murphy
    Nov 23 '18 at 5:55





    Very nice thanks! Unfortunately for the purpose of this assignment I cant use a range based for, only stl container functions.

    – Shane Murphy
    Nov 23 '18 at 5:55













    0














    I don't have much to add on top of @snake_style's answer, but I would like to point out that you can easily drop the complexity/expense of using copy_if to filter the words in sample_doc.txt by using a simple while loop:



    #include <algorithm>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iterator>
    #include <map>
    #include <set>
    using namespace std;

    int main(){
    set<string> exclusionSet;
    ifstream stopWords("stopwords.txt");
    copy(istream_iterator<string>(stopWords),
    istream_iterator<string>(),
    inserter(exclusionSet, exclusionSet.end()));

    stopWords.close();

    map<string, int> wordMap;
    ifstream words("sample_doc.txt");
    string s;

    while(words >> s) if (exclusionSet.count(s)==0) ++wordMap[s];

    ofstream out("frequency.txt");

    for_each(begin(wordMap),
    end(wordMap),
    [&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
    out.close();
    return 0;
    }





    share|improve this answer




























      0














      I don't have much to add on top of @snake_style's answer, but I would like to point out that you can easily drop the complexity/expense of using copy_if to filter the words in sample_doc.txt by using a simple while loop:



      #include <algorithm>
      #include <iostream>
      #include <fstream>
      #include <string>
      #include <iterator>
      #include <map>
      #include <set>
      using namespace std;

      int main(){
      set<string> exclusionSet;
      ifstream stopWords("stopwords.txt");
      copy(istream_iterator<string>(stopWords),
      istream_iterator<string>(),
      inserter(exclusionSet, exclusionSet.end()));

      stopWords.close();

      map<string, int> wordMap;
      ifstream words("sample_doc.txt");
      string s;

      while(words >> s) if (exclusionSet.count(s)==0) ++wordMap[s];

      ofstream out("frequency.txt");

      for_each(begin(wordMap),
      end(wordMap),
      [&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
      out.close();
      return 0;
      }





      share|improve this answer


























        0












        0








        0







        I don't have much to add on top of @snake_style's answer, but I would like to point out that you can easily drop the complexity/expense of using copy_if to filter the words in sample_doc.txt by using a simple while loop:



        #include <algorithm>
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <iterator>
        #include <map>
        #include <set>
        using namespace std;

        int main(){
        set<string> exclusionSet;
        ifstream stopWords("stopwords.txt");
        copy(istream_iterator<string>(stopWords),
        istream_iterator<string>(),
        inserter(exclusionSet, exclusionSet.end()));

        stopWords.close();

        map<string, int> wordMap;
        ifstream words("sample_doc.txt");
        string s;

        while(words >> s) if (exclusionSet.count(s)==0) ++wordMap[s];

        ofstream out("frequency.txt");

        for_each(begin(wordMap),
        end(wordMap),
        [&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
        out.close();
        return 0;
        }





        share|improve this answer













        I don't have much to add on top of @snake_style's answer, but I would like to point out that you can easily drop the complexity/expense of using copy_if to filter the words in sample_doc.txt by using a simple while loop:



        #include <algorithm>
        #include <iostream>
        #include <fstream>
        #include <string>
        #include <iterator>
        #include <map>
        #include <set>
        using namespace std;

        int main(){
        set<string> exclusionSet;
        ifstream stopWords("stopwords.txt");
        copy(istream_iterator<string>(stopWords),
        istream_iterator<string>(),
        inserter(exclusionSet, exclusionSet.end()));

        stopWords.close();

        map<string, int> wordMap;
        ifstream words("sample_doc.txt");
        string s;

        while(words >> s) if (exclusionSet.count(s)==0) ++wordMap[s];

        ofstream out("frequency.txt");

        for_each(begin(wordMap),
        end(wordMap),
        [&out](const pair<const string, int>& e){ out<<e.first<<" "<<e.second<<endl;});
        out.close();
        return 0;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 7:27









        teltel

        6,78421430




        6,78421430






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424576%2fwrite-map-to-file-using-for-each%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen