通过emplace()将对象指针插入到地图的地图中不起作用

问题描述 投票:4回答:5

我试图通过map将指针对象插入emplace(),但它不起作用。

我在下面创建了一个简单的问题表示。我正在尝试插入newFooList指针对象类型Foo*

我似乎找不到在FooMap*中为std::map<int, FooMap*> m_fooMapList创建类型的方法。应该在地图的第二个区域使用new吗?

#include <iostream>
#include <utility>
#include <stdint.h>
#include <cstdlib>
#include <map>

class Foo
{
    private:
        int m_foobar;
    public:
        Foo(int value)
        {
            m_foobar = value;
        }
        void setfoobar(int value);
        int getfoobar();
};

class FooMap
{
    private:
        std::map<int, Foo*> m_newFoo;

    public:
        FooMap() = default;
};

class FooMapList
{
    private:
        std::map<int, FooMap*> m_fooMapList;
    public:
        FooMapList() = default;
        void insertFoo(Foo* newFooObj);
};

int Foo::getfoobar(void)
{
    return(m_foobar);
}

void FooMapList::insertFoo(Foo* newFooObj)
{
    if(m_fooMapList.empty())
    {
        std::cout << "m_fooMapList is empty" << std::endl ;
    }

    //m_fooMapList.emplace( newFooObj->getfoobar(), newFooObj  );
    // Need to find a way to insert newFooObj  to m_fooMapList
    m_fooMapList.second = new FooMap;
}

int main() {
    FooMapList newFooList;

    for (auto i=1; i<=5; i++)
    {
        Foo *newFoo = new Foo(i);
        newFoo->getfoobar();
        newFooList.insertFoo(newFoo);
    }

    return 0;
}

在g ++(GCC)4.8.5 20150623(Red Hat 4.8.5-28)

$  g++ -std=c++11 -Wall map_of_map.cpp 
map_of_map.cpp: In member function ‘void FooMapList::insertFoo(Foo*)’:
map_of_map.cpp:51:18: error: ‘class std::map<int, FooMap*>’ has no member named ‘second’
     m_fooMapList.second = new FooMap;
c++ c++11 pointers stdmap emplace
5个回答
3
投票

我不确定你是否需要一个地图结构,其中值是指向另一个地图的指针。 FooMapList课程可能很简单

std::map<int, FooMap> m_fooMapList;

另一方面,使用行指针的整个游戏将带给你除了脖子上的痛苦。

如果需要使用std::map<int, FooMap*> m_fooMapList;std::map<int, Foo*>,我会选择smartpointers。

以下是使用std::unique_ptr替换行指针的示例代码,并显示如何插入Foo的地图以就地映射。 See live here

#include <iostream>
#include <utility>
#include <map>
#include <memory>

class Foo
{
private:
    int m_foobar;
public:
    Foo(int value): m_foobar(value) {}
    void setfoobar(int value) noexcept { m_foobar = value; }
    int getfoobar() const noexcept { return m_foobar; }
};

class FooMap
{
private:
    std::map<int, std::unique_ptr<Foo>> m_newFoo;
    //            ^^^^^^^^^^^^^^^^^^^^
public:
    FooMap() = default;
#if 0 // optional
    // copy disabled
    FooMap(const FooMap&) = delete;
    FooMap& operator=(const FooMap&) = delete;

    // move enabled
    FooMap(FooMap&&) = default;
    FooMap& operator=(FooMap&&) = default;
#endif
    // provide a helper function to insert new Foo to the map of Foo s
    void insertFoo(std::unique_ptr<Foo> newFooObj)
    //             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    {
        std::cout << "inserting to FooMap..." << std::endl;
        m_newFoo.emplace(newFooObj->getfoobar(), std::move(newFooObj)); // construct in place
    }
};

class FooMapList
{
private:
    std::map<int, std::unique_ptr<FooMap>> m_fooMapList;
    //            ^^^^^^^^^^^^^^^^^^^^^^^
public:
    FooMapList() = default;

    void insertFooMap(std::unique_ptr<Foo> newFooObj)
    //               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    {
        if (m_fooMapList.empty())
        {
            std::cout << "m_fooMapList is empty" << std::endl;
        }
        // create FooMap and insert Foo to it.
        FooMap fooMap;
        const auto key = newFooObj->getfoobar();
        fooMap.insertFoo(std::move(newFooObj));

        // finally insert the FooMap to m_fooMapList
        std::cout << "inserting to fooMapList..." << std::endl;
        m_fooMapList.emplace(key, std::make_unique<FooMap>(std::move(fooMap))); // construct in place
    }
};

int main() 
{
    FooMapList newFooList;

    for (auto i = 1; i <= 5; i++)
    {
        auto newFoo = std::make_unique<Foo>(i);
        std::cout << newFoo->getfoobar() << std::endl;
        newFooList.insertFooMap(std::move(newFoo));
    }

    return 0;
}

输出:

1
m_fooMapList is empty
inserting to FooMap...
inserting to fooMapList...
2
inserting to FooMap...
inserting to fooMapList...
3
inserting to FooMap...
inserting to fooMapList...
4
inserting to FooMap...
inserting to fooMapList...
5
inserting to FooMap...
inserting to fooMapList...

5
投票

m_fooMapList定义为

    std::map<int, FooMap*> m_fooMapList;

所以要插入它,你需要一个int和指向FooMap的指针:

    m_fooMapList.emplace(newFooObj->getfoobar(), new FooMap);

话虽如此,你应该使用C ++值语义,并减少对原始指针的依赖:

    std::map<int, FooMap> m_fooMapList; // no pointers

    m_fooMapList.emplace(newFooObj->getfoobar(), {}); // construct objects in-place

也就是说,FooMap的实例可以直接驻留在地图本身中。

这样您可以获得更好的性能并避免内存泄漏。

如果你真的想使用指针,它也值得研究智能指针(例如unique_ptr)。


2
投票

你可以扔掉你的do-nothing地图类,停止使用指针,只是

#include <iostream>
#include <utility>
#include <stdint.h>
#include <cstdlib>
#include <map>

class Foo
{
private:
    int m_foobar;
public:
    Foo(int value) : m_foobar(value) { }
    void setfoobar(int value) { m_foobar = value; }
    int getfoobar() const { return m_foobar; }

    // or more simply
    // int foobar;
};

using FooMap = std::map<int, Foo>;

using FooMapMap = std::map<int, FooMap>;

int main() {
    FooMapMap foos;

    for (auto i=1; i<=5; i++)
    {
        foos[i][i] = Foo(i);
    }

    return 0;
}

请注意,内部地图在此阶段完全没有意义,因为它们只有一个条目


1
投票

除非你有充分的理由这样做,否则要避免像这样混淆Java这样的东西,并尽可能地利用STL。为此,您可以使用类型别名

using FooMap = std::map<int, Foo*>; // Maybe use a smart pointer instead here?
using FooMapList = std::map<int, FooMap>; // Maybe List is not an appropriate name for a map

现在,您有一个刚创建的Foo元素,并希望将其插入到地图列表中,为此您需要一种方法来选择要插入的列表中的哪个地图。我假设您将插入列表中的第一个地图:

auto FooMap::emplace(int key, Foo* value)
{
    return m_newFoo.emplace(key, value);
}

void FooMapList::insertFoo(Foo* newFooObj)
{
    // If the map for `getfoobar` does not exist yet, operator[] will create it
    auto& mapPtr = m_fooMapList[newFooObj->getfoobar()];
    if (nullptr == mapPtr)
        mapPtr = new FooMap();

    mapPtr->emplace(
        newFooObj->getfoobar(),
        newFooObj
    );
}

请注意,我没有处理内存清理。我建议你在适用的时候尝试使用智能指针(std::unique_ptrstd::shared_ptr


0
投票

我已经考虑了每个答案中的有效点来删除指针并删除无用的双级地图表示。但是真实世界的抽象是一个非常复杂的问题,它涉及数千个动态对象,需要动态创建和销毁。使用指针似乎是一种有效的方法,但JeJo' approach似乎好多了。

我试图重复使用他的尝试,但有对象指针,下面似乎工作。具有以下插入功能

FooMap类中,函数将是

void FooMap::insertFoo(Foo* newFooObj)
{
    m_newFoo.emplace(newFooObj->getfoobar(), newFooObj);
}

const std::map<int, Foo*> FooMap::getList()
{
    return m_newFoo;
}

FooMapList它会

void FooMapList::insertFooList(Foo* newFooObj)
{
    std::map <int, FooMap*>::iterator iter;
    FooMap *localFooMap = NULL;
    iter = m_fooMapList.find( newFooObj->getfoobar() );

    if( iter == m_fooMapList.end() )
    {
        localFooMap = new FooMap;
        localFooMap->insertFoo(newFooObj);
        m_fooMapList.emplace(newFooObj->getfoobar(), localFooMap );
    }
    else
    {    
        localFooMap = iter->second;
        localFooMap->insertFoo(newFooObj);
        m_fooMapList.emplace(newFooObj->getfoobar(), localFooMap );
    }
}

const std::map<int, FooMap*> FooMapList::getList()
{
    return m_fooMapList;
}

我也很感激这种方法的反馈。我将添加对析构函数的调用来清理创建的对象

© www.soinside.com 2019 - 2024. All rights reserved.