C++ “向量迭代器不可递减”?

发布于 2025-01-01 15:29:32 字数 2900 浏览 2 评论 0原文

我正在尝试解决 这个 UVa 问题

我正在尝试使用 Vector 来解决这个问题。我需要模拟循环链表之类的东西,所以我使用迭代器来访问元素。但经过尝试,我发现 Vector 迭代器在递增和递减方面存在一些问题,并且我无法通过使用反向迭代器作为参数来删除元素。我现在很困惑。我的代码是否有任何问题,因为我错过了一些重要的细节,或者我应该以其他方式解决这个问题?

提前致谢。

这是我的代码

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

vector<int> people;

int main()
{
    int n, k, m;            // k -> counter clockwise, m -> cloclwise
    while (cin >> n >> k >> m)
    {
        if (n == 0 && k == 0 && m == 0)
            return 0;
        for (int i = 1; i <= n; i++)
            people.push_back(i);
        vector<int>::iterator k_pos = people.begin();
        vector<int>::reverse_iterator m_pos = people.rbegin();

        //cout << n << " " << k << " " << m << endl;

        while (!people.empty())
        {
            int k_choose, m_choose;
            for (int i = 1; i < k; i++)
            {
                k_pos++;
                if (k_pos == people.end()) // if reach the end, go to begin
                    k_pos = people.begin();
            }

            k_choose = *k_pos;
            cout << k_choose << endl;

            for (int i = 1; i < m; i++)
            {
                m_pos++;
                if (m_pos == people.rend())
                    m_pos = people.rbegin();
            }

            m_choose = *m_pos;


            if (k_choose == m_choose)
            {
                cout << setw(3) << k_choose << ",";
                people.erase(k_pos);                 // erase the element
            }

            else
            {
                cout << setw(3) << k_choose << setw(3) << m_choose << ",";
                k_pos = people.erase(k_pos);         // erase the element
                //vector<int>::iterator temp;
                //for (temp = people.begin(); *temp != *m_pos; temp++)
                //{
                //}
                //cout << "ok" << endl;
                people.erase(--m_pos.base());*****problem

            }
            vector<int>::iterator temp;
            for (temp = people.begin(); temp != people.end(); temp++)
                cout << *temp << endl;

            k_pos++;                              *****problem
            if (k_pos == people.end())          // point to next
                k_pos = people.begin();

            m_pos++;                              *****problem
            if (m_pos == people.rend())         // point to next
                m_pos = people.rbegin();
        }
    }
    return 0;
}

I am attempting to solve this UVa problem.

And I am trying to use Vector to solve the problem. I need to simulate something like circular linked list, so I use an iterator to access the elements. But after trying, I found Vector iterator having some problem about increment and decrement, and I cannot erase the element by using a reverse_iterator as argument. I am confused now. Is there any wrong with my code because I missed some important details or I should solve this problem in another way??

Thanks in advance.

Here is my code

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

vector<int> people;

int main()
{
    int n, k, m;            // k -> counter clockwise, m -> cloclwise
    while (cin >> n >> k >> m)
    {
        if (n == 0 && k == 0 && m == 0)
            return 0;
        for (int i = 1; i <= n; i++)
            people.push_back(i);
        vector<int>::iterator k_pos = people.begin();
        vector<int>::reverse_iterator m_pos = people.rbegin();

        //cout << n << " " << k << " " << m << endl;

        while (!people.empty())
        {
            int k_choose, m_choose;
            for (int i = 1; i < k; i++)
            {
                k_pos++;
                if (k_pos == people.end()) // if reach the end, go to begin
                    k_pos = people.begin();
            }

            k_choose = *k_pos;
            cout << k_choose << endl;

            for (int i = 1; i < m; i++)
            {
                m_pos++;
                if (m_pos == people.rend())
                    m_pos = people.rbegin();
            }

            m_choose = *m_pos;


            if (k_choose == m_choose)
            {
                cout << setw(3) << k_choose << ",";
                people.erase(k_pos);                 // erase the element
            }

            else
            {
                cout << setw(3) << k_choose << setw(3) << m_choose << ",";
                k_pos = people.erase(k_pos);         // erase the element
                //vector<int>::iterator temp;
                //for (temp = people.begin(); *temp != *m_pos; temp++)
                //{
                //}
                //cout << "ok" << endl;
                people.erase(--m_pos.base());*****problem

            }
            vector<int>::iterator temp;
            for (temp = people.begin(); temp != people.end(); temp++)
                cout << *temp << endl;

            k_pos++;                              *****problem
            if (k_pos == people.end())          // point to next
                k_pos = people.begin();

            m_pos++;                              *****problem
            if (m_pos == people.rend())         // point to next
                m_pos = people.rbegin();
        }
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦中楼上月下 2025-01-08 15:29:32

擦除或推入向量后,它的所有迭代器可能会变得无效(如果向量被重新分配)。这就是为什么在 else 中执行擦除后 m_pos 可能会变得无效。我的建议是使用索引(至少这是我在竞争性编程中所做的)。

After erasing or pushing in a vector all the iterators to it may become invalid(if the vector gets reallocated). That is why after erase is performed in the else m_pos may become invalid. My advise is to use indices(at least that is what I do for competetive programming).

溇涏 2025-01-08 15:29:32

如果m_pos等于people.rbegin()怎么办?
因此 --m_pos 不是有效的迭代器,这可能是问题的根源。

您也有可能删除了前一行中由 --m_pos 指向的元素:

k_pos = people.erase(k_pos);         // erase the element

您可以在代码中改进的另一件事是设置 k_posm_pos 迭代器以更有效的方式。相反:

for (int i = 1; i < k; i++)
{
    k_pos++;
    if (k_pos == people.end()) // if reach the end, go to begin
    k_pos = people.begin();
}

你可以写:

#include <iterator>
std::advance(people.begin(), k % people.size());

What if m_pos is equal to people.rbegin()?
Therefore --m_pos is not a valid iterator and that's can be the source of problem.

Also it is possible, that you erased an element pointed by --m_pos in the line before:

k_pos = people.erase(k_pos);         // erase the element

The other thing you can improve in your code is setting k_pos and m_pos iterators in a more efficient way. Instead:

for (int i = 1; i < k; i++)
{
    k_pos++;
    if (k_pos == people.end()) // if reach the end, go to begin
    k_pos = people.begin();
}

You can write:

#include <iterator>
std::advance(people.begin(), k % people.size());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文