C++ class, struct, map pointer, reference

Markus Freitag 3,786 Reputation points
2022-10-26T18:51:48.18+00:00

Hello,
I am looking for a good solution to query by reference changes in a dictionary map and change them if necessary.
After my research, the following practical solution has come up.

From other inquiry
However, one thing that may be wise, would be to define your map as having a reference to a string rather than a pointer

// The fourth solution.  
 string b3 = "20221019-0127";  
 map<string, string*> m3333;  
 m3333.insert(std::make_pair("abc", &b3));  
 b3 = "change to 20221019 - 0127 ";  

OK, you mean this example, right?

Or simple this way? I think that is the best and easiest way. How is your opinion?

map<string, string> m22;  
m22.insert(std::make_pair("abc", "20221019-0127"));  
m22.insert(std::make_pair("abd", "20221019-0128"));  
m22.insert(std::make_pair("abe", "20221019-0129"));  
  
string &retM22 = m22["abd"];  
retM22 = "new";  

Everything so complicated, very many ways, what is the right, the best way?

If I have a object for the value, I can go this way, right?
I think that is it.

map<int, CInfos> DicIndexInfos;  
map<int, CInfos> DicIndexInfosCurrentPanel;  
  
CInfos& infos = DicIndexInfos[index];  
infos.ProductID.Trim();  
infos.MaterialSN.Trim();  
infos.AssemblySN.Trim();  
infos.State.Trim();  
if (infos.State.MakeLower() != "used" && isCurrentPanelFilled == false)  
{  
// *** Fill current panel  
counterIndexCurrentPanel++;  
infos.State = "used";  
  

Why the question? Am I right, is this the right and best way? For me looks good. The simplest and most understandable way.

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,757 questions
0 comments No comments
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,291 Reputation points Microsoft Vendor
    2022-10-27T02:53:36.843+00:00

    Hi @Markus Freitag ,

    The method you mentioned is indeed a good idea.

    It does work when the amount of data is small. When the amount of data is large, you may need to define many reference variables. We should avoid this situation.

    When there is a lot of data, you could first use the contains() to confirm whether the key you want to change exists in the map, and the value of the map can be changed by m2["abc"] = "change to 20221019 - 0127";.

    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.


0 additional answers

Sort by: Most 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.