简体   繁体   English

从向量中删除重复项,递归C ++

[英]Removing duplicates from a vector, recursion c++

I'm working on a program that is supposed to sort a vector of ints and then get passed to a recursive function to remove any duplicates by having an element check it's neighbor, and remove it if they're the same. 我正在开发一个程序,该程序应该对整数向量进行排序,然后传递给递归函数以通过让元素检查其邻居来删除所有重复项,并在它们相同时将其删除。 Here is my code: 这是我的代码:

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

My issue here is that when it runs, I get the following error after entering values: 我的问题是运行它时,输入值后出现以下错误:

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

It also prints multiple times when it works but I'm not terribly concerned about that. 它在工作时也会多次打印,但是我对此并不十分担心。 Where's the bug? 错误在哪里? Can anybody help me out? 有人可以帮我吗?

UPDATE: 更新:

Here is the code that I am CURRENTLY running with: 这是我目前正在使用的代码:

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

I ran with a debugger, everything was fine, was removing duplicates like it was no big deal, until the size of n/i reached the size of the list. 我和调试器一起运行,一切都很好,正在删除重复项,就像没什么大不了的,直到n / i的大小达到列表的大小。 Then I get that error message listed above. 然后,我得到上面列出的错误消息。 How do I fix that? 我该如何解决?

You have commented out return s completely except the recursive call. 您已完全注释掉return s,但递归调用除外。 The functions go on although it should not. 该功能可以继续,尽管不应该继续。

You should use return checkNum(v, n); 您应该使用return checkNum(v, n); on those lines. 在那些线上。

Edit: 编辑:

Your code is completely flawed and won't do what you want. 您的代码完全有缺陷,不会做您想要的。 Here is a quick ugly code of how you should be doing it while you stick to limitation of probably homework: 这是一个快速丑陋的代码,说明了您在限制可能的家庭作业时应该如何做:

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

Ok got it to work. 好的,它可以正常工作。 You need to add a return after the line, cout << "No duplicates found" like: 您需要在该行之后添加一个返回值,cout <<“未找到重复项”,例如:

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

The reason is that by the time the recursive call reaches this point in the program, you are done checking for duplicates. 原因是,当递归调用到达程序中的这一点时,您已经检查了重复项。 Your recursive function should wrap things up at this point. 此时,您的递归函数应该将所有内容包装好。

Without the return you keep increasing n (below at the line with n++): 没有回报,您就不断增加n(在n ++的下面):

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

but then n is larger than the size of your vector v, causing the error. 但是n大于向量v的大小,从而导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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