c++ - Exception: STATUS_ACCESS_VIOLATION - pointer referencing -
i starting c++ (that after 10 years of java!). following examples stroupstrup book.
i put following code segments book.
#include <iostream> #include <map> #include <string> #include <iterator> using namespace std; map<string, int>histogram; void record (const string &s) { histogram[s]++; //record frequency of "s" cout<<"recorded:"<<s<<" occurence = "<<histogram[s]<<"\n"; } void print (const pair<const string, int>& r) { cout<<r.first<<' '<<r.second<<'\n'; } bool gt_42(const pair<const string, int>& r) { return r.second>42; } void f(map<string, int>& m) { typedef map<string, int>::const_iterator mi; mi = find_if(m.begin(), m.end(), gt_42); cout<<i->first<<' '<<i->second; } int main () { istream_iterator<string> ii(cin); istream_iterator<string> eos; cout<<"input end\n"; for_each(ii, eos, record); //typedef pair <string, int> string_int_pair; //histogram.insert(string_int_pair("42", 1)); //histogram.insert(string_int_pair("44", 1)); //for_each(histogram.begin(), histogram.end(), print); f(histogram); }
i error - exception: status_access_violation, think in referencing i->first, i->second, guess. can me out find out issue might be. if can suggest alternate c++ forums helpful well.
in void f(map<string, int>& m)
function not check whether found element looking for, .e.g:
void f(map<string, int>& m) { typedef map<string, int>::const_iterator mi; mi = find_if(m.begin(), m.end(), gt_42); if(i != m.end()) cout<<i->first<<' '<<i->second; else cout << "not found" << endl; }
error occures when accesing i->first
when i
points "past end" of map container.
Comments
Post a Comment