首页 > 解决方案 > 我可以简化这个 std::list 的数量吗?

问题描述

我可以简化std::list代码的填充:

void CMeetingScheduleAssistantApp::InitBrowserRegistryLookupList(RegistryPathList& rListRegPaths)
{
    S_REGISTRY_PATH  sRegPath;

    // Reset the list
    rListRegPaths.clear();

    // These will be "native" 32bit or native 64bit browsers (i.e. the Operating System bitness)
    sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe");
    sRegPath.strBrowser = _T("Firefox");
    rListRegPaths.push_back(sRegPath);

    sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE");
    sRegPath.strBrowser = _T("Internet Explorer");
    rListRegPaths.push_back(sRegPath);

    sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe");
    sRegPath.strBrowser = _T("Google Chrome");
    rListRegPaths.push_back(sRegPath);

    sRegPath.hRootKey = HKEY_CURRENT_USER;
    sRegPath.strKeyPath = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\opera.exe");
    sRegPath.strBrowser = _T("Opera Internet Browser");
    rListRegPaths.push_back(sRegPath);

    // These will be 32 bit browsers (on a 64 bit Operating System)
    if (IsOS(OS_WOW6432))
    {
        sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
        sRegPath.strKeyPath = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe");
        sRegPath.strBrowser = _T("Firefox");
        rListRegPaths.push_back(sRegPath);

        sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
        sRegPath.strKeyPath = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE");
        sRegPath.strBrowser = _T("Internet Explorer");
        rListRegPaths.push_back(sRegPath);

        sRegPath.hRootKey = HKEY_LOCAL_MACHINE;
        sRegPath.strKeyPath = _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe");
        sRegPath.strBrowser = _T("Google Chrome");
        rListRegPaths.push_back(sRegPath);
    }
}

更新

的定义RegPathlist

typedef struct tagRegistryPath
{
    HKEY hRootKey;
    CString strBrowser;
    CString strKeyPath;

} S_REGISTRY_PATH;
using RegistryPathList = list<S_REGISTRY_PATH>;

标签: c++visual-c++stdlist

解决方案


可能你会想要emplace_back你的元素,这可以加速和“简化”(主观术语)你的代码。例子:

rListRegPaths.emplace_back(
      HKEY_LOCAL_MACHINE, 
      _T("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe"),
      _T("Firefox"));

推荐阅读