简体   繁体   English

向量的无序映射C ++

[英]Unordered map of vectors c++

I´m trying for several hours now to get this right. 我现在尝试了几个小时才能解决这个问题。 What i want to do is have a map of vectors (the vectors contain pointers to objects) stored in an unnamed namespace. 我想做的是在未命名的命名空间中存储一个向量映射(向量包含指向对象的指针)。 Classes are then allowed to access the data in the vector via a function call. 然后允许类通过函数调用访问向量中的数据。

//DataHandler.cpp

#include "DataHandler.h"

using std::vector;
using std::unordered_map;

namespace DataHandler
{
  namespace
  {
    unordered_map< unsigned int, vector<MyClass*> > data_map_;
  }

  void receiveData(unsigned int key, vector< MyClass*>& data_vec)
  {
    //check if key already exists:
    unordered_map<unsigned int, vector<MyClass*> >::iterator it;

    it = data_map_.find(key);
    if (it != data_map_.end())
    {
      data_vec = it->second;
      return;
    }

    //generate new key with new data
    data_map_.insert(make_pair(key, vector< MyClass*>()));

    data_map_[key].push_back(new MyClass(/*some constructor arguments*/));
    // ... push back some more instances of MyClass ...

    //finally:
    data_vec = data_map_[key];
    return;
  }
}

In some other class I do this for getting the vector: 在其他一些类中,我这样做是为了获得向量:

//SomeOtherClass.cpp

#include "SomeOtherClass.h"
#include "DataHandler.h"

using std::vector;

void SomeOtherClass::aMethod()
{
  vector<MyClass*> first;
  vector<MyClass*> second;
  vector<MyClass*> first_again;

  DataHandler::receiveData(1, first); //works fine
  DataHandler::receiveData(2, second); //works fine
  DataHandler::receiveData(1, first_again); // <- strange behavior or segfault here
}

So everything works as expected, besides if I want to access a vector which had previously been filled with MyClass instances - then I get strange behavior or a segmentation fault. 因此,所有事情都按预期工作,此外,如果我想访问以前用MyClass实例填充的向量,那么我会得到奇怪的行为或分段错误。 Can someone explain to me why this is so and how this is done right? 有人可以向我解释为什么会这样以及如何正确执行吗?

Thanks!! 谢谢!!

I can't see any specific problem, so this isn't an answer but is too long for a comment. 我看不到任何具体问题,因此这不是答案,但评论太久。 I'll mark it community wiki. 我将其标记为社区Wiki。 Anyway, why not have a std::unordered_map<unsigned, std::vector<MyClass>> and get rid of all the pointers and lifetime issues? 无论如何,为什么不拥有std::unordered_map<unsigned, std::vector<MyClass>>并摆脱所有指针和生命周期问题? SomeOtherClass::aMethod() can do an insert , which returns an std::pair<iterator,bool> - just save that iterator if you want access to the MyClass elements later in aMethod . SomeOtherClass::aMethod()可以执行insert ,该操作返回std::pair<iterator,bool> -如果以后要在aMethod访问MyClass元素,则只需保存该迭代器aMethod As is, if you have some pointers in first_again , they're clobbered inside receiveData() with whatever pointers are in first : there are lots of ways that could be a memory leak, but I can't say for sure without seeing more of your code. 由于是,如果你有一些三分球first_again ,他们正在遭受重挫内receiveData()与任何指针在first :有很多的方式,可能是内存泄漏,但没有看到更多的,我不能肯定地说您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM