Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:29:46+00:00 2026-05-29T05:29:46+00:00

I am attempting to solve this UVa problem . And I am trying to

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:29:46+00:00Added an answer on May 29, 2026 at 5:29 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to solve the Project Euler Problem 8 with C++, and the problem
I have sort of a tricky problem I'm attempting to solve. First of all,
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to solve the Code Golf: Build Me an Arc problem. My solution's
I have been pulling my hair out trying to solve this. What I am
I've spent the better part of my day trying to solve this message while
I'm attempting to solve what I thought would be a very simple problem. I
I am having some real difficulty attempting to solve a JQuery $.each() iteration This
I'm attempting to solve the producer-consumer problem using pthreads and semaphores, but it looks
Here is the problem I am attempting to solve. I have 3 database tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.