我可以简化这个 std::list 的人口吗?
Can I simplify the population of this 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>;
您可以使用初始值设定项列表构造函数和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")});
// ...
}
}
请注意,我用TCHAR const*
替换了CString
以避免内存分配和复制字符串。
您可能希望emplace_back
元素,这可以加快和"简化"(主观术语(您的代码。例:
rListRegPaths.emplace_back(
HKEY_LOCAL_MACHINE,
_T("SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"),
_T("Firefox"));
是的,你可以简化它。最好的方法是提供包含此数据的配置文件。
如果你不想使用文件,只需使用初始化列表(参见std::list::insert的版本5(:
void CMeetingScheduleAssistantApp::InitBrowserRegistryLookupList(RegistryPathList& rListRegPaths)
{
rListRegPaths.insert(rListRegPaths.end(),
{
{
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")
},
{
....
}
....
});
}
您可以通过使用参数构造来缩短它。
typedef struct tagRegistryPath
{
HKEY hRootKey;
CString strBrowser;
CString strKeyPath;
} S_REGISTRY_PATH;
rListRegPaths.push_back(S_REGISTRY_PATH {
HKEY_LOCAL_MACHINE,
_T("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"),
_T("Firefox")
});
您可以使用不同的浏览器创建数组,如下所示:
S_REGISTRY_PATH native[] = {
{HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"), _T("Firefox")},
//...
}
S_REGISTRY_PATH wow64[] = {
{HKEY_LOCAL_MACHINE, _T("SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe"), _T("Firefox")},
//...
}
这甚至可以是一个单独的文件,甚至是自动生成的,你只需将其包含在实现该方法的文件中。然后在该方法中,您要做的就是:
void CMeetingScheduleAssistantApp::InitBrowserRegistryLookupList(RegistryPathList& rListRegPaths)
{
rListRegPaths.clear();
for (auto && it : native) {
rListRegPaths.push_back(*it);
}
if (IsOS(OS_WOW6432)) {
for (auto && it : wow64) {
rListRegPaths.push_back(*it);
}
}
}
这将基本上只是数据的内容与代码本身分开,这使得阅读、更改和一般管理变得更加容易。
相关文章:
- 使用std::multimap迭代器创建std::list
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 如何在 C 中正确使用 libiconv 使其不会报告"Arg list too long"?
- C++中带有List类的迭代器Segfault
- 使用"std::unordereded_map"映射到"std::list"对象
- GCC对可能有效的代码抛出init list生存期警告
- 使用std::list创建循环链表
- 重载Singly Linked List中的赋值运算符
- '[](std::list& list)<int>{return std::move(list)}(list)' 是否保证将 'list' 留空?
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- "std::list::splice(std::const_iterator pos, std::list&& other)"是否保证将"其他"留空?
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- QStringList vs list<shared_ptr<QString>> 性能比较C++
- 包含 std::list 的结构体的 C++ 初始化
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- C++ assigment std::list:<typename>:itrator 在 main 中工作,但在方法中它不起作用
- 在基于范围的 for 循环期间插入 std::list 的后面
- 循环挂起迭代的 std::擦除 on std::list
- 我可以简化这个 std::list 的人口吗?