使用 std:map 时崩溃

发布于 2024-08-11 21:01:02 字数 693 浏览 2 评论 0原文

在我的 SDL 程序中,我使用映射构造来模拟某个类中的“无限”对象数组。代码设法编译得很好,但是当我运行程序时,一旦触发使用映射的函数之一,程序就会崩溃,返回值 3。

所以,这正是我正在做的:

class MyClass
{
     public:
           int MyFunction();
     protected:
           std::map< int, MyObject > MyMap;
}

int MyClass::MyFunction()
{
     ...
     int i;

     if( MyMap.empty() )
     {
          i = 1;
     }
     else
     {
         i = MyMap.size() + 1;
     }

     MyMap[ i ] = PreviouslyDefinedObject;

     return i;

}

当 MyFunction( ) 从 MyClass 对象调用,发生崩溃。每当使用 MyMap 完成任何使用时,似乎都会发生这种情况:如果您注释掉倒数第二行并尝试返回 i,它就会崩溃;如果您只是设置 i = 1 然后将一个对象分配给 MyMap[i],它就会

崩溃这是我第一次使用地图,所以我不确定我是否正确使用了它们。这是某个地方的基本错误吗?有人能指出我正确的方向吗?干杯。

In my SDL program, I am using a map construct to simulate an "infinite" array of objects within a certain class. The code manages to compile fine, but when I run the program, as soon as one of the functions using the maps is trigger, the program crashes, returning a value of 3.

So, here's exactly what I'm doing:

class MyClass
{
     public:
           int MyFunction();
     protected:
           std::map< int, MyObject > MyMap;
}

int MyClass::MyFunction()
{
     ...
     int i;

     if( MyMap.empty() )
     {
          i = 1;
     }
     else
     {
         i = MyMap.size() + 1;
     }

     MyMap[ i ] = PreviouslyDefinedObject;

     return i;

}

When MyFunction() is called from a MyClass object, the crash occurs. It seems to happen whenever anything of use is done with MyMap: it crashes if you comment out the penultimate line and just try to return i, and it crashes if you just set i = 1 and then assign an object to MyMap[i]

This is the first time I've ever used a map, so I'm not certain I'm using them right. Is this a basic mistake somewhere? Can anyone point me in the right direction? Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南七夏 2024-08-18 21:01:02

也许您正在从未初始化的指针调用该函数,如下所示:

MyClass *obj;
obj->MyFunction();

Maybe you are calling the function from an uninitialized pointer, like this:

MyClass *obj;
obj->MyFunction();
素罗衫 2024-08-18 21:01:02

映射用于将键与值关联起来。如果您正在寻找数组,则应该使用 向量 。与映射相比,它可以更好地模拟“无限数组”,因为映射不是数组。

请注意,通常可以使用向量分配很多元素。如果你真的想模拟一个大数组,我建议包装一个向量向量。通过一些数学计算,您可以为其创建一个运算符[] 来索引到正确的数组和正确的元素。

至于你的代码,确实没有足够的信息来确定它为什么会崩溃,你必须尝试创建一个最小的程序供我们编译或查看。

Maps are used to associate a key with a value. If you're looking for an array, you should use a vector. It will do a better job of simulating an "infinite array" than a map, because a map isn't an array.

Note you can allocate a lot of elements with a vector, usually. If you're really trying to simulate a large array, I'd recommend wrapping up a vector of vectors. With some math, you could create an operator[] for it that indexes into the correct array, to the correct element.

As for your code, there really isn't enough information to determine why it should be crashing, you'd have to try to create a minimal program for us to compile or look at.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文