0

我正在开发一个程序,该程序应该对整数向量进行排序,然后传递给递归函数以通过元素检查它的邻居来删除任何重复项,如果它们相同则将其删除。这是我的代码:

#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 的大小达到列表的大小。然后我收到上面列出的错误消息。我该如何解决?

4

2 回答 2

2

return除了递归调用之外,您已经完全注释掉了s 。尽管不应该,但功能仍在继续。

你应该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;
}
于 2013-11-08T01:24:16.823 回答
1

好的,让它工作。您需要在该行之后添加一个返回, cout << "No duplicates found" 如:

if (i == v.size())
{
   cout << "No duplicates found." << endl;
   return; // add this return
}

原因是当递归调用到达程序中的这一点时,您已完成检查重复项。你的递归函数应该在这一点上结束。

如果没有回报,你会不断增加 n(在 n++ 行下方):

else
{
    n++;
    /*return 0 + */checkNum(v, n);
}

但随后 n 大于向量 v 的大小,从而导致错误。

于 2013-11-08T01:42:47.773 回答