I have been trying to create a sample application in Visual Studio 2010. I am not getting what is the problem as the code is perfectly compiling, but is giving run-time error. Here is the code:
#include <Map>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[])
{
map <int, string> *sss = new map<int, string>;
sss->at(0) = "Jan";
sss->at(1) = "Feb";
sss->at(2) = "Mar";
sss->at(3) = "Apr";
sss->at(4) = "May";
sss->at(5) = "Jun";
string current = NULL;
ofstream myfile;
myfile.open("daily_work.txt");
myfile << "***** DAILY WORK *****" << endl;
for(int i = 0; i < 6; i++)
{
string month = sss->at(i);
for(int j = 1; j < 31; j++)
{
stringstream ss;
ss << j;
current = ss.str();
current.append(" ");
current.append(month);
current.append(" = ");
myfile << current << endl;
}
current = "";
}
printf("Completed!");
myfile.close();
sss->clear();
delete sss;
sss = NULL;
return 0;
}
The error is thrown at line no 2 in main.
sss->at(0) = "Jan";
Please find the error here:

Method
map.atdoes access at the element 0. You have just created the map, so you element 0 is empty. Useinsertand read this documentation about maps.c++ reference map
I suggest you also to avoid
for 1 ... 6, iterator is the preferred way to cycle a map.In this way, if you add element to your map, you don’t need to to anything else, the loop will automatically adjust itself.
Use this sample: