2015-04-06 36 views
0

裏面我有一個類名爲Client女巫是這樣如何訪問類的元素映射C++

class Client 
{ 
    public: 
     Client(unsigned int id, std::string nom, std::string prenom, float solde); 

     unsigned int getId() const; 
     std::string getNom() const; 
     std::string getPrenom() const; 
     float getSolde() const; 

     bool operator< (Client& client); 
     friend std::ostream& operator<< (std::ostream& os, const Client& client); 

     bool diminutionSolde(float diminution); 

    private: 
     unsigned int id_; 
     std::string nom_; 
     std::string prenom_; 
     float solde_; 
}; 

,我也有一類文章認爲,去像這樣

class Article 
{ 
public: 
    Article(unsigned int id, std::string nom, float prix); 

    unsigned int getId() const; 
    std::string getNom() const; 
    float getPrix() const; 

    bool operator< (Article& article); 
    friend std::ostream& operator<< (std::ostream& os, const Article& article); 

private: 
    unsigned int id_; 
    std::string nom_; 
    float prix_; 
}; 

我也有另一類(我不認爲有必要表明)作爲地圖作爲屬性,

std::map<Client , BasketArticles* > mapClientPanier_; 

其中basketArticles作爲列表< T> as attribut;

首先,我需要在客戶端上使用指針嗎?

那麼,如何訪問客戶端的文章,一個一個地流在屏幕上,比如std ::法院< <

和/或噘嘴客戶的名字和文章,他在一個新的地圖(這將是按字母順序),然​​後std :: cout < <所有。

回答

0

是否需要指針完全取決於如何構造類實例:什麼是實例化和銷燬它們的責任。如果您的BasketArticles實例在其他地方實例化,並且您希望地圖引用它們,那麼顯然您需要一個指針。否則,如果BasketArticles的實例應該只存在於映射中,那麼最好避免使用指針,並且負責實例化它們。

+0

- >你會幫我解決問題,這對我來說很重要,因爲我需要在其他幾種方法中做到這一點()。 – 2015-04-06 01:57:03

+0

您的問題「我如何訪問客戶文章」並不意味着什麼。你有一個std :: map。訪問std :: map的內容與訪問任何其他std :: map的內容沒有區別。使用迭代器begin(),end();或find();或任何其他常用的std :: map方法。\ – 2015-04-06 03:44:52