class, struct, vector, map

Markus Freitag 3,791 Reputation points
2022-10-14T04:49:46.273+00:00

Hello,

class PanelData  
{  
	 // **   
	 CString Price;  
	 CString IdentNo;  
  
	 bool    PanelReported;  
};  
vector<PanelData>      m_dequePanelData;  
  
// ******** OR *******  
  
struct PanelData  
{  
	 // **   
	 CString Price;  
	 CString IdentNo;  
  
	 bool    PanelReported;  
  
};  
vector<PanelData>      m_dequePanelData;  

If I have a vector a map, and create the elements with class, is that by reference or by value?
Which is generally preferable?

Developer technologies | C++
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2022-10-19T01:49:46.707+00:00

    Hi @Markus Freitag ,

    There are several ways to initialize the map:

    map<string, int> m1;  
    m1[string("abc")] = 1;  
    m1[string("defg")] = 2;  
       
      
    map<string, int> m2;  
    m2.insert({ string("abc"), 1 });  
    m2.insert(make_pair(string("defg"), 2));  
    m2.insert(pair<string, int>(string("hijk"), 3));  
    

    References are often used for function parameter lists and function return values.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Guido Franzke 2,191 Reputation points
    2022-10-14T09:09:36.853+00:00

    Hello,
    in the class your members are private. In the struct, your members are public. That's all.
    What do you mean with "by reference" or "by value" in a map or vector? The elements are totally stored in both containers.
    Regards, Guido

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.