我正在研究一个程序,该程序应该对int的向量进行排序,然后传递给递归函数,通过让元素检查它的邻居来删除任何重复项,如果它们是相同的则删除它。这是我的代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void checkNum(vector<int> &v, int n)
{
int i = n;
if (v[i] == '\0')
{
cout << "No duplicates found." << endl;
}
if (v[i]==v[i+1])
{
v.erase(v.begin()+i);
/*return 1 + */checkNum(v, n);
}
else
{
n++;
/*return 0 + */checkNum(v, n);
}
int k;
cout << "Sorted values, no duplicates: " << endl;
for (k=0; k< v.size(); k++)
cout << v[k] << " ";
//return 0;
}
int main()
{
vector<int> numbers;
cout << "Please enter numbers, 0 to quit: " << endl;
bool more = true;
while (more)
{
int num;
cin >> num;
if (num == 0)
more = false;
else
numbers.push_back(num);
}
sort(numbers.begin(),numbers.end());
cout << "The sorted values are: " << endl;
int i;
for (i = 0; i < numbers.size(); i++)
cout << numbers[i] << " ";
checkNum(numbers, 0);
system("pause");
return 0;
}
我的问题是,当它运行时,输入值后会出现以下错误:
Debug Assertion Failed!
Program: C:\Windows\system32\MSVCP110D.dll File: d:\program files (x86)\microsoft visual studio 11.0\vc\include\vector Line: 1140
Expression: vector subscript out of range
当它工作时它也会打印多次,但我并不十分关心它。错误在哪里?有人可以帮帮我吗?
的更新: 的
以下是我目前正在运行的代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int checkNum(vector<int> &v, int n)
{
int i = n;
if (i == v.size())
{
cout << "No duplicates found." << endl;
return 0;
}
if (v[i]==v[i+1])
{
v.erase(v.begin()+i);
return checkNum(v, n);
}
else
{
n++;
return checkNum(v, n);
}
int k;
cout << "Sorted values, no duplicates: " << endl;
for (k=0; k< v.size(); k++)
cout << v[k] << " " << endl;
return 0;
}
int main()
{
vector<int> numbers;
cout << "Please enter numbers, 0 to quit: " << endl;
bool more = true;
while (more)
{
int num;
cin >> num;
if (num == 0)
more = false;
else
numbers.push_back(num);
}
sort(numbers.begin(),numbers.end());
cout << "The sorted values are: " << endl;
int i;
for (i = 0; i < numbers.size(); i++)
cout << numbers[i] << " ";
checkNum(numbers, 0);
system("pause");
return 0;
}
我使用调试器运行,一切都很好,删除重复项就像没什么大不了的,直到n / i的大小达到了列表的大小。然后我收到上面列出的错误消息。我该如何解决这个问题?
答案 0 :(得分:2)
除递归调用外,您已完全注释掉return
。这些功能虽然不应该继续。
您应该在这些行上使用return checkNum(v, n);
。
编辑:
您的代码存在完全缺陷,无法执行您想要的操作。这是一个快速丑陋的代码,说明你应该如何做到这一点,同时你坚持可能的功课限制:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void checkNum(vector<int> &v, vector<int>::iterator& it)
{
if (it == v.end() - 1) //last element
{
cout << "After duplicate removal pass:" << endl;
for (int k=0; k< v.size(); k++)
cout << v[k] << " ";
}
else if (*it == *(it + 1)) //next element is equal to current one, erase current one
{
v.erase(it); //using 'it' after this line is normally not a good practice,
// but we know the vector is stored sequentially and 'it' will point to another element because above we checked if this is the last element or not.
return checkNum(v, it);
}
else //next element is not the same as this one
{
++it;
return checkNum(v, it);
}
}
int main()
{
vector<int> numbers;
cout << "Please enter numbers, 0 to quit: " << endl;
bool more = true;
while (more)
{
int num;
cin >> num;
if (num == 0)
more = false;
else
numbers.push_back(num);
}
sort(numbers.begin(),numbers.end());
cout << "The sorted values are: " << endl;
int i;
for (i = 0; i < numbers.size(); i++)
cout << numbers[i] << " ";
vector<int>::iterator elementToStart = numbers.begin();
checkNum(numbers, elementToStart);
system("pause");
return 0;
}
答案 1 :(得分:1)
好的让它发挥作用。你需要在行后添加一个返回,cout&lt;&lt; “没有发现重复”,如:
if (i == v.size())
{
cout << "No duplicates found." << endl;
return; // add this return
}
原因是,当递归调用到达程序中的这一点时,您已完成检查重复项。你的递归函数应该包装起来。
如果没有返回,你会继续增加n(在n ++行下面):
else
{
n++;
/*return 0 + */checkNum(v, n);
}
但是n大于矢量v的大小,导致错误。