gpt4 book ai didi

c++ - 我可以简化这个 std::list 的人口吗?

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:52 24 4
gpt4 key购买 nike

我可以简化我的 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>;

最佳答案

您可以使用初始化列表构造函数和push_back:

struct RegistryPath {
HKEY hRootKey;
TCHAR const* strBrowser;
TCHAR const* strKeyPath;
};

int main() {
std::list<RegistryPath> sRegPath = {
{HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe"), _T("Firefox")},
{HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE"), _T("Internet Explorer")}
// ...
};
if(IsOS(OS_WOW6432)) {
sRegPath.push_back({HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe"), _T("Firefox")});
// ...
}
}

请注意,我将 CString 替换为 TCHAR const* 以避免内存分配和复制字符串。

关于c++ - 我可以简化这个 std::list 的人口吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56523541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com