gpt4 book ai didi

c++ - Visual Studio 2010 x64 中 map 的运行时错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:17 24 4
gpt4 key购买 nike

我一直在尝试在 Visual Studio 2010 中创建一个示例应用程序。我没有发现问题所在,因为代码可以完美编译,但会出现运行时错误。这是代码:

#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;
}

错误在 main 的第 2 行抛出。

sss->at(0) = "Jan";

请在此处查找错误:

enter image description here

最佳答案

来自 http://en.cppreference.com/w/cpp/container/map/at :

Returns a reference to the mapped value of the element with key equivalent to key.
If no such element exists, an exception of type std::out_of_range is thrown.

你需要:

map <int, string> sss;
sss[ 0 ] = "Jan";

关于c++ - Visual Studio 2010 x64 中 map 的运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14276586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com