使用父类父级的向量指针通过指向父类的指针访问map成员变量

时间:2017-07-31 20:19:43

标签: c++

我的代码中有四个类,类Data,类Sample,类Node和类Tree。

class Data
{
private:
    map<string, double> m_DataVariables;

public:
    Data();
    Data(const Data &data);

    map<string, double> getDataVariables() const;
    void setDataVariables(const map<string, double> &value);
};

class Sample
{
private:
    Data *m_pData;         // Pointer To The Map Of DataVariables

public:
    virtual ~Sample()
    {
        delete m_pData;
    }

    Sample();
    Sample(const Sample &sample);

    // Data Variables      
    map<string, double> getDataVariables() const;
    void setDataVariables(const map<string, double> &value);
};

class Node
{
private:
    double m_numSamples;
    vector<Sample*> m_NodeSamples;

public:
   virtual ~Node()
   {
   }

   Node();

   // Number of samples for the node
   double getNumSamples() const;
   void setNumSamples(const double &value);

   // List of Samples
   vector<Sample*> getSamples() const;
   void setSamples(const vector<Sample*> &value);
};

class Tree
{
private:
    vector<Sample*> m_Samples;
    vector<Node*> m_nodes;

public:
    Tree(vector<Sample*> &Samples);

    // List of Sample
    vector<Sample*> getSamples() const;
    void setSamples(const vector<Sample*> &value);

    // List of Nodes
    vector<Node*> getNodes() const;
    void setNodes(const vector<Node*> &value);

    // List of Names that were used in building the tree
    vector<string> getPredictorNames() const;
    void setPredictorNames(const vector<string> &value);

    void CalcError(Node *node, const string &Name, double &error);

};

Data::Data()
{
    m_DataVariables = map<string, double>();
}

map<string, double> Data::getDataVariables() const
{
    return m_DataVariables;
}


Sample::Sample(const Sample &sample)
{
    m_pData                     = new Data(); //Map of Variables
    m_pData->getDataVariables() = sample.getDataVariables();
}

map<string, double> Sample::getDataVariables() const
{
    return m_pData->getDataVariables();
}

double Node::getNumSamples() const
{
    return m_numSamples;
}

vector<Sample*> Node::getSamples() const
{
    return m_NodeSamples;
}


void Tree::Tree()
{
    m_Samples = vector<Sample*>();
    m_nodes   = vector<Node*>();
}

vector<Sample*> Tree::getSamples() const
{
    return m_Samples;
}

vector<Node*> Tree::getNodes() const
{
    return m_nodes;
}

在CalcError(Node * node,const string&amp; name,double&amp; error)中,我想在Node类的NodeSamples中的每个样本中,遍历Data类中的DataVariables映射并将传递的名称与键进行比较在地图上。如果name与键匹配,则读取与键关联的值并将其存储在一个集合中。此时我无法使用C ++ 11功能。我可以使用C ++ 98。

在Visual Studio的C#中,这很简单:

List Values = node.Samples.Select(s =&gt; s.DataVariables [name])。OrderBy(v =&gt; v)。ToList();

但在C ++中,我不确定如何实现这一目标。我开始的是:

void Tree::CalcError(Node *node, const string &name, double &error)
{
    vector<double> Values;

    for (vector<TrainingSample*>::iterator SampleIt = node->getTrainingSamples().begin(); SampleIt != node->getTrainingSamples().end(); SampleIt++)
    {
        for (map<string, double>::iterator map_iter = **Not sure how to access the map....** map_iter++)
        {
            if (name.compare(**Not sure how to access the key in the map**) == 0)
            {
                Values.push_back(**Not sure how to access the value in the map**);
            }
        }
    }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

地图迭代器基本上会产生std::pair<KeyType,ValueType>,因此您可以访问以下内容:

for (map<string, double>::iterator map_iter = DataVariables.begin();
     map_iter != DataVariables.end();
     ++map_iter) {
    if (name.compare(map_iter->first) == 0) {
                  // ^^^^^^^^^^^^^^^ Access the key
        Values.push_back(map_iter->second);
                      // ^^^^^^^^^^^^^^^^ Access the value
    }
}

答案 1 :(得分:0)

您可以使用map::find

删除其中一个循环
void Tree::CalcError(Node* node, const string& name, double& error)
{
    vector<double> Values;
    const vector<TrainingSample*>& samples = node->getTrainingSamples();

    for (vector<TrainingSample*>::const_iterator SampleIt = samples.begin();
         SampleIt != samples.end();
         ++SampleIt)
    {
        const TrainingSample& sample = **SampleIt;
        const std::map<string, double>& m = sample.getDataVariables();
        std::map<string, double>::const_iterator map_iter = m.find(name);

        if (map_iter != m.end())
        {
            Values.push_back(map_iter->second);
        }
    }
    // ...
}