将此代码从cstringarray转换为std :: vector< cstring>?

发布于 2025-01-26 22:34:11 字数 1550 浏览 3 评论 0原文

给定此代码:

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> perfermansshufflelocateText方法?

我应该只留下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再可℃爱ぅ一点好了 2025-02-02 22:34:11

我知道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>. Your LocateText (that is overly verbose) will become obsolete - just use find

Also, your ShuffleArray is very inefficient (remove/insert one item at a time). Using a vector will allow you to do something like Best way to extract a subvector from a vector?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文