这段代码适合 lambda 函数吗?
Is this code a good candidate for a lambda function?
我的函数中有 27 个:
auto i = m_lbLanguage.AddString(_T("Afrikaans (Afrikaans)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Afrikaans));
i = m_lbLanguage.AddString(_T("Cebuano (Cebuano)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Cebuano));
i = m_lbLanguage.AddString(_T("汉语(简化字)Chinese (Simplified)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::ChineseSimplified));
这种类型的代码适合使用 lambda 函数吗?我知道我可以添加一个常规函数,例如:
void Cxxx::AddLanguageToLB(CString strLanguage, LanguageMSA eLanguage)
{
auto i = m_lbLanguage.AddString(strLanguage);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(eLanguage);
}
以及用途:
AddLanguageToLB(_T("汉语(简化字)Chinese (Simplified)"), LanguageMSA::ChineseSimplified);
这种类型的代码很适合 table 驱动的实现。沿着这些思路,也许:
struct {
LPCTSTR name;
LanguageMSA code;
} languages[] = {
{_T("Afrikaans (Afrikaans)"), LanguageMSA::Afrikaans},
{_T("Cebuano (Cebuano)"), LanguageMSA::Cebuano},
// ...
};
for (auto& lang : languages) {
auto i = m_lbLanguage.AddString(lang.name);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(lang.code));
}
我的函数中有 27 个:
auto i = m_lbLanguage.AddString(_T("Afrikaans (Afrikaans)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Afrikaans));
i = m_lbLanguage.AddString(_T("Cebuano (Cebuano)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::Cebuano));
i = m_lbLanguage.AddString(_T("汉语(简化字)Chinese (Simplified)"));
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(LanguageMSA::ChineseSimplified));
这种类型的代码适合使用 lambda 函数吗?我知道我可以添加一个常规函数,例如:
void Cxxx::AddLanguageToLB(CString strLanguage, LanguageMSA eLanguage)
{
auto i = m_lbLanguage.AddString(strLanguage);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(eLanguage);
}
以及用途:
AddLanguageToLB(_T("汉语(简化字)Chinese (Simplified)"), LanguageMSA::ChineseSimplified);
这种类型的代码很适合 table 驱动的实现。沿着这些思路,也许:
struct {
LPCTSTR name;
LanguageMSA code;
} languages[] = {
{_T("Afrikaans (Afrikaans)"), LanguageMSA::Afrikaans},
{_T("Cebuano (Cebuano)"), LanguageMSA::Cebuano},
// ...
};
for (auto& lang : languages) {
auto i = m_lbLanguage.AddString(lang.name);
m_lbLanguage.SetItemData(i, static_cast<DWORD_PTR>(lang.code));
}