C++ “向量迭代器不可递减”?
我正在尝试解决 这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
擦除或推入向量后,它的所有迭代器可能会变得无效(如果向量被重新分配)。这就是为什么在 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).
如果
m_pos
等于people.rbegin()
怎么办?因此
--m_pos
不是有效的迭代器,这可能是问题的根源。您也有可能删除了前一行中由
--m_pos
指向的元素:您可以在代码中改进的另一件事是设置
k_pos
和m_pos
迭代器以更有效的方式。相反:你可以写:
What if
m_pos
is equal topeople.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:The other thing you can improve in your code is setting
k_pos
andm_pos
iterators in a more efficient way. Instead:You can write: