将此代码从cstringarray转换为std :: vector< cstring>?
给定此代码:
void CSelectNamesDlg::ShuffleArray(CString strName, CStringArray *pAryStrNames)
{
if (pAryStrNames == nullptr)
return;
const auto iSize = pAryStrNames->GetSize();
if (iSize > 1)
{
// First, we must locate strName in the array
auto i = CSelectNamesDlg::LocateText(strName, pAryStrNames);
if (i != -1)
{
const auto iName = i;
// We must now shuffle the names from the bottom to the top
const auto iCount = gsl::narrow<int>(iSize) - iName;
for (i = 0; i < iCount; i++)
{
CString strTemp = pAryStrNames->GetAt(iSize-1);
pAryStrNames->RemoveAt(iSize-1);
pAryStrNames->InsertAt(0, strTemp);
}
}
}
}
int CSelectNamesDlg::LocateText(CString strText, const CStringArray *pAryStrText)
{
bool bFound = false;
int i{};
if (pAryStrText != nullptr)
{
const auto iSize = pAryStrText->GetSize();
for (i = 0; i < iSize; i++)
{
if (pAryStrText->GetAt(i) == strText)
{
// Found him!
bFound = true;
break;
}
}
}
if (!bFound)
i = -1;
return (int)i;
}
如果我将我的cstringarray
转换为std :: vector&lt; cString
实现相同的corme> perfermansshuffle
和locateText
方法?
我应该只留下cstringarray
吗?
Given this code:
void CSelectNamesDlg::ShuffleArray(CString strName, CStringArray *pAryStrNames)
{
if (pAryStrNames == nullptr)
return;
const auto iSize = pAryStrNames->GetSize();
if (iSize > 1)
{
// First, we must locate strName in the array
auto i = CSelectNamesDlg::LocateText(strName, pAryStrNames);
if (i != -1)
{
const auto iName = i;
// We must now shuffle the names from the bottom to the top
const auto iCount = gsl::narrow<int>(iSize) - iName;
for (i = 0; i < iCount; i++)
{
CString strTemp = pAryStrNames->GetAt(iSize-1);
pAryStrNames->RemoveAt(iSize-1);
pAryStrNames->InsertAt(0, strTemp);
}
}
}
}
int CSelectNamesDlg::LocateText(CString strText, const CStringArray *pAryStrText)
{
bool bFound = false;
int i{};
if (pAryStrText != nullptr)
{
const auto iSize = pAryStrText->GetSize();
for (i = 0; i < iSize; i++)
{
if (pAryStrText->GetAt(i) == strText)
{
// Found him!
bFound = true;
break;
}
}
}
if (!bFound)
i = -1;
return (int)i;
}
If I convert my CStringArray
into a std::vector<CString
is it going to be simpler to achieve the same PerformShuffle
and LocateText
methods?
Should I just stay with CStringArray
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道MFC数组的1(一个!)优于
std :: vector
- 它们支持MFC风格的序列化。如果您使用它 - 您可能会被卡住。但是,如果您不这样做 - 我将使用
std :: vector&lt; cstring&gt;
。您的locateText
(那是冗长的)将变得过时 - 只需使用查找另外,您的
shufflearray
非常效率(一次删除/插入一个项目)。使用vector
将允许您执行从向量提取子向量的最佳方法?I know of 1 (ONE!) benefit of MFC array over
std::vector
- they support MFC-style serialization. If you use it - you may be stuck.However, if you don't - I would use
std::vector<CString>
. YourLocateText
(that is overly verbose) will become obsolete - just use findAlso, your
ShuffleArray
is very inefficient (remove/insert one item at a time). Using avector
will allow you to do something like Best way to extract a subvector from a vector?