stl - C++ for_each string iterators: loops out of range -


for c++ class @ university, have implement simple word counter using algorithms , containers of standard template library (stl).

the functionality has encapsulated in class "wordcounter" provides public method "map(string input)". class has have 2 private data components storing frequencies of words contained within input string, named "intermediatemap" , "resultmap". data structures must of stl type "map".

the input string has mapped intermediatemap. string read word word. each (!) word functor class wordadder - respectively operator - called. operator reads matching entry map , appends "1" vector of entry.

so, after processing sample string "hello hello world" intermediatemap should have following state:

"hello" <1, 1> "world" <1> 

i demonstrated in our class @ university, using for_each algorithm of stl 2 iterators range definitions , wordadder functor function object.

my problem is, for_each loop not stop iterating after processing each word , crashes segfault.

what doing wrong?

header:

#include <cctype>        // ispunct, isdigit, tolower  #include <iostream>      // cout, cerr, .... #include <fstream>       // ifstream #include <iomanip> #include <string>        // string #include <sstream>       // stringstrstream  #include <map>           // map #include <vector>        // vector #include <algorithm>     // copy, transform, unique #include <iterator> using namespace std;  typedef map<string, vector<int> > wordmap;  class wordcounter{       private:      //private data components     wordmap intermediatemap;     wordmap resultmap;      //private, auxilary methods          bool ispunct(char ch);         bool isdigit(char ch);         char tolower(char ch);         string normalize(string word);       public:         void map(string input);         void reduce();    };//wordcounter 

implementation:

#include <wordcounter.h> using namespace std;     class wordadder {    wordmap& words;                                public:         wordadder(wordmap& words) : words(words) {}          void operator()(string word) {          if (word.size() == 0)            return;          //read reference         //of value list of map element         //satisfies 'word' search condition         vector<int>& occurences = words[word];         occurences.push_back(1);    } };     void wordcounter::map(string input){  std::istringstream iss (input); /* problematic zone*/ for_each (                istream_iterator<string>(iss),                 istream_iterator<string>(),                 wordadder(intermediatemap)             );     return;       } 


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -