Share via

class, struct, vector, map

Markus Freitag 3,791 Reputation points
Oct 14, 2022, 4:49 AM

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?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,818 questions
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,291 Reputation points Microsoft Vendor
    Oct 19, 2022, 1:49 AM

    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,186 Reputation points
    Oct 14, 2022, 9:09 AM

    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.