map<CString, Component> // Dictionary // to find keys and values // the best way

Markus Freitag 3,791 Reputation points
2023-12-07T18:18:03.4166667+00:00

Hello,

I have a dictionary and need to find a specific element. What options do I have in C++?

A) I would like to find the key, filter with key

  • string, string
  • string, component

B) I would like to find the keys by specifying the value

  • string, string
  • string, component

C) TrueOfAll() Check are all Components in the list/map are true? (IsReported)

I would like to change the Component.Unit based on the key found. Permanently, by reference Temporary, by value

Do you have any examples of how I can reach this goal? Thank you in advance.


struct Component
{
	CString Name;
	CString Value;
	CString Unit;
    bool IsReported;
};

map<CString, CString> m_DicConfigMaterials;
map<CString, Component> m_DicProcessdata;	

// The goal is 
//if key == "TestKey" then return Value;

const auto it = std::find_if(m_DicConfigMaterials.begin(), m_DicConfigMaterials.end(), [](const Component& d) { return d.Name; });

if (it == m_DicConfigMaterials.end())
{
	// not found
	// . . .
}
else
{
	// found
	var t1 = it - m_DicConfigMaterials.begin();
	// . . .
}

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. Minxin Yu 12,351 Reputation points Microsoft Vendor
    2023-12-11T08:21:25.31+00:00

    Hi, @Markus Freitag

    Eg.

    data

        std::map<std::string, Component> m_DicProcessdata = {
            {"TableMotorPosY", {"TableMotorPosY", "80", "mm", false}},
            {"TableMotorPosA", {"TableMotorPosA", "90", "mm", true}},
            {"TableMotorPosB", {"TableMotorPosB", "50", "mm", true}},
            {"TableMotorPosC", {"TableMotorPosC", "150", "mm", true}},
            {"EnvironmentalTemperature", {"EnvironmentalTemperature", "50", "°C", false}}
        };
    
    

    ProcessKeys whose value is >80

    use std::stoi

       const int value = 80;
        for (const auto& it : m_DicProcessdata) {
            if (std::stoi(it.second.Value) > value) {
                std::cout << "ProcessKey: " << it.first << ", Value: " << it.second.Value << std::endl;
            }
        }
    
    

    std::map::find()

    const std::string substringToFind = "TableMotorPos";
    
       for (const auto& it : m_DicProcessdata) {
           if (it.first.find(substringToFind) != std::string::npos) {
               std::cout << "ProcessKey: " << it.first << ", is: " << it.second.Value << std::endl;
           }
       }
    

    The value should be able to update using operator [].

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly 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.

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.