2017-07-31 58 views
-2

我在我的代碼中,類Data,類Sample,類節點和類樹中有四個類。通過指向父類的指針訪問映射成員變量使用父類的父類的向量指針

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(節點*節點,常量字符串&名,雙&誤差),我想爲在類節點的NodeSamples每個樣品,通過DataVariables地圖類數據迭代和傳遞的名稱進行比較在地圖上的關鍵。如果名稱與密鑰匹配,則讀取與該密鑰關聯的值並將其存儲在一個集合中。目前我無法使用C++ 11功能。 C++ 98是我可以使用的。

在根據視覺工作室此C#是簡單使用:

列表中值= node.Samples.Select(S => s.DataVariables [名稱])的OrderBy(V => 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**); 
      } 
     } 
    } 
} 

任何幫助將不勝感激。

+0

你消氣應該返回避免副本的const引用(以及使用相同容器的迭代器)。 – Jarod42

+0

您不必顯式構造'vector'或'map',它們的默認構造函數就足夠了。 – Jarod42

回答

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 
    } 
} 
+0

@ Jarod42固定。 – user0042

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); 
     } 
    } 
    // ... 
}