Insert elements into a map container I am inserting elements into the map container. For this I am using the same statements multiple times in the below function. Is there any way I can write a generic function for this? So that even at the later point of time, when required, I can insert new elements.

 bool EMRMgr::GetParams()
    {
        EmIOStruct emIOstructObj;
        WCHAR szValue[MAX_PATH] = { 0 };
        DWORD dwDataSize = sizeof(szValue) / sizeof(WCHAR);
        long lRes = 0;
        McStorageType mcStorageTypeObj = McStorageType::eRegistry;
        std::wstring value;

    // First time I am using the below statements to insert elements into the map container

        mcIOstructObj.lpszValueName = (LPWSTR)ER_ID;
        memset(szValue, 0, MAX_PATH);
        mcIOstructObj.lpData = (LPBYTE)&amp;szValue[0];
        lRes = m_cIOManager.ReadValue(mcStorageTypeObj, mcIOstructObj);
        value.clear();
        if ((LPWSTR)mcIOstructObj.lpData == nullptr)
        {
            value.assign(L"");
        }
        else
        {
            value.assign((LPWSTR)mcIOstructObj.lpData);
        }

        m_fileParams.insert({ (std::wstring) ER_ID, value });

    // Second time I am using the below statements to insert elements into the map container

        mcIOstructObj.lpszValueName = (LPWSTR)CPS;
        memset(szValue, 0, MAX_PATH);
        mcIOstructObj.lpData = (LPBYTE)&amp;szValue[0];
        lRes = m_cIOManager.ReadValue(mcStorageTypeObj, mcIOstructObj);
        value.clear();
        if ((LPWSTR)mcIOstructObj.lpData == nullptr)
        {
            value.assign(L"");
        }
        else
        {
            value.assign((LPWSTR)mcIOstructObj.lpData);
        }
        m_fileParams.insert({ (std::wstring) CPS, value });

        return true;
    }
There is missing code so I'm assuming that ER_ID and CPS are strings. If not, you can easily sub the data type.

Since the only difference between the blocks of code is which string (or other data type) to use (ER_ID vs. CPS), you can make a member function of EMRMgr that has the same block of code but subbing the string to use with a parameter.

Unless I've overlooked something, this should work.

bool EMRMgr::DoTheThing(std::string str)
    {
        mcIOstructObj.lpszValueName = (LPWSTR)str;
        memset(szValue, 0, MAX_PATH);
        mcIOstructObj.lpData = (LPBYTE)&amp;szValue[0];
        lRes = m_cIOManager.ReadValue(mcStorageTypeObj, mcIOstructObj);
        value.clear();
        if ((LPWSTR)mcIOstructObj.lpData == nullptr)
        {
            value.assign(L"");
        }
        else
        {
            value.assign((LPWSTR)mcIOstructObj.lpData);
        }

        m_fileParams.insert({ (std::wstring) str, value });

    }