相关疑难解决方法(0)

指针作为地图C++ STL中的键

我有一个问题,当在地图中用作键时,如何处理指向自定义对象的指针.更具体地说,如果我定义

std::map< CustomClass*, int > foo;
Run Code Online (Sandbox Code Playgroud)

默认的C++实现是否可以处理这些指针?或者我是否需要定义自定义比较器函数来处理它?通常,使用指向对象的指针作为键是一种好习惯吗?

c++ pointers stl map

38
推荐指数
5
解决办法
2万
查看次数

C++将shared_ptr提升为hash_map键

我正在创建一个神经网络,并希望使用hash_map来保持每个神经元的输出神经元的权重参考:

class Neuron; //forward declaration was there (sorry I forgot to show it earlier)
typedef double WEIGHT;
typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT> NeuronWeightMap;
class Neuron
{
private:
    NeuronWeightMap m_outputs;
    //...
public:

    Neuron();
    ~Neuron();
    //...
    WEIGHT GetWeight(const boost::shared_ptr<Neuron>& neuron) const
    {
        NeuronWeightMap::const_iterator itr = m_outputs.find(neuron);
        if( itr != m_outputs.end() )
        {
            return itr->second;
        }
        return 0.0f;
    }
};
Run Code Online (Sandbox Code Playgroud)

我意识到我不能使用boost :: shared_ptr作为stdext :: hash_map的键,那么另一个建议是什么呢?是否有任何解决方法或是使用不同密钥或切换到std :: map的唯一选择?谢谢!

这是错误:

1>c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(61) : error C2440: 'type cast' : cannot convert from 'const boost::shared_ptr<T>' to …
Run Code Online (Sandbox Code Playgroud)

c++ boost hashmap shared-ptr neural-network

2
推荐指数
2
解决办法
7658
查看次数

标签 统计

c++ ×2

boost ×1

hashmap ×1

map ×1

neural-network ×1

pointers ×1

shared-ptr ×1

stl ×1