简体   繁体   中英

C++ “vector iterator not decrementable”?

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;
}

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).

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM