Using a function to remove duplicates from an array in C++

懵懂的女人 提交于 2019-12-07 07:05:07

问题


I'm writing a program that has a user input integers into an array, calls a function that removes duplicates from that array, and then prints out the modified array. When I run it, it lets me input values into the array, but then gives me a "Segmentation fault" error message when I'm done inputing values. What am I doing wrong?

Here is my code:

#include <iostream>

using namespace std;

void rmDup(int array[], int& size)
{
        for (int i = 0; i < size; i++)
        {
            for (int j = i + 1; j < size; j++)
            {
                if (array[i] == array[j])
                {
                    array[i - 1 ] = array[i];
                    size--;
                }
            }
        }
}
int main()
{
    const int CAPACITY = 100;
    int values[CAPACITY], currentSize = 0, input;

    cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";

    while (cin >> input)
    {
       if (currentSize < CAPACITY)   
       {
           values[currentSize] = input;
           currentSize++;
       }
    }

    rmDup(values, currentSize);

    for (int k = 0; k < currentSize; k++)
    {
            cout << values[k];
    }

    return 0;
}

Thank you.


回答1:


for (int i = 0; i < size; i++)
{
    for (int j = i + 1; j < size; j++)
    {
        if (array[i] == array[j])
        {
            array[i - 1 ] = array[i]; /* WRONG! array[-1] = something */
            size--;
        }
    }
}

If array[0] and array[1] are equal, array[0-1] = array[0], meaning that array[-1] = array[0]. You are not supposed to access array[-1].




回答2:


I wouldn't make it even possible to create duplicates:

int main()
{
    const int CAPACITY = 100;

    cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";

    std::set<int> myInts;
    int input;
    while (std::cin >> input && input != 'q' && myInts.size() <= CAPACITY) //note: 113 stops the loop, too!
       myInts.insert(input);

    std::cout << "Count: " << myInts.size();
}

And do yourself a favour and don't use raw arrays. Check out the STL.




回答3:


#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;
int main()
{
    vector<int> vec = {1,1,2,3,3,4,4,5,6,6};
    auto it = vec.begin();

    while(it != vec.end())
    {
        it = adjacent_find(vec.begin(),vec.end());
        if(it != vec.end())
            vec.erase(it);
            continue;
    }

    for_each(vec.begin(),vec.end(),[](const int elem){cout << elem;});
    return 0;
}

This code compiles with C++11.




回答4:


#include<iostream>
#include<stdio.h>
using namespace std;

int arr[10];
int n;

void RemoveDuplicates(int arr[]);
void Print(int arr[]);


 int main()
{

cout<<"enter size of an array"<<endl;
cin>>n;
 cout<<"enter array elements:-"<<endl;
for(int i=0;i<n ;i++)
{
    cin>>arr[i];
}
RemoveDuplicates(arr);
Print(arr);
}

 void RemoveDuplicates(int arr[])
{
   for(int i=0;i<n;i++)
{
    for(int j=i+1;j<n;)
    {
        if(arr[i]==arr[j])
        {
            for(int k=j;k<n;k++)
            {
                arr[k]=arr[k+1];

            }
            n--;
        }
        else
            j++;
    }
}
}

void Print(int arr[])
{
   for(int i=0;i<n;i++)
   {
      cout<<arr[i]<<"  ";
    }
 }


来源:https://stackoverflow.com/questions/20501982/using-a-function-to-remove-duplicates-from-an-array-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!