【问题标题】:how to eliminate the duplicate numbers from the array, and resize the array to the new number of elements in C++如何消除数组中的重复数字,并将数组的大小调整为 C++ 中的新元素数
【发布时间】:2013-12-08 22:01:28
【问题描述】:

我编写了一个程序,它为大小为 17 的整数数组随机生成数字(从 1 到 15)。用这些数字填充数组后,它按降序对数组进行排序。

现在我想从数组中消除重复的数字,并将数组的大小调整为新的元素数。但我真的不知道该怎么做。任何帮助将不胜感激。谢谢。

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    const int arraySize = 17;
    int RandomNumbers[arraySize];
    int insert; // temporary variable to hold element to insert

    srand(time(0));

    cout<<"Before Sorting :"<<endl;
    for (int i=0; i<17; i++)
    {
        RandomNumbers[i]= 1+rand()%15;
        cout<<RandomNumbers[i]<<" , ";
    }
    cout<<endl;
    cout<<endl;

    for (int next = 1; next < arraySize; next++)
    {
      insert = RandomNumbers[next]; // store the value in the current element
      int moveItem = next; //initialize location to place element

      //search for the location in which to put the current element
      while ((moveItem >0) &&(RandomNumbers[moveItem -1]< insert) )
      {
        //shift element one slot to the right
          RandomNumbers[moveItem] = RandomNumbers[moveItem -1];
          moveItem--;
      } //end while
      RandomNumbers[moveItem]= insert; //place inserted element into the array
    } //end for

    cout << "After Sorting :"<<endl;

    // output sorted array
    for (int j =0; j < arraySize; j++)
        {cout<<RandomNumbers[j]<<" , ";
        }
    cout<<endl;
    return 0;
}    

这是我的输出:

排序前: 13 , 14 , 5 , 12 , 7 , 3 , 9 , 15 , 12 , 3 , 3 , 13 , 13 , 3 , 3 , 10 , 4 ,

排序后: 15 , 14 , 13 , 13 , 13 , 12 , 12 , 10 , 9 , 7 , 5 , 4 , 3 , 3 , 3 , 3 , 3 , 按任意键继续 。 . .

【问题讨论】:

标签: c++ arrays sorting random duplicate-removal


【解决方案1】:

最简单的形式是使用std::vectorstd::sortstd::vector::erase+std::unique

#include <algorithm>
#include<vector>
#include<iterator>

template <typename T>
void eliminate_duplicates(std::vector<T>& vec)
{
  std::sort(vec.begin(), vec.end());
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
}

template <typename T>
void display(std::vector<T>& vec)
{
  std::copy(vec.begin(),
           vec.end(), 
           std::ostream_iterator<T>(std::cout," ") );
}

std::vector<int> RandomNumbers;
for (int i=0; i<17; i++)
    RandomNumbers.push_back(1+rand()%15);

display(RandomNumbers);

eliminate_duplicates(RandomNumbers);

display(RandomNumbers);    

【讨论】:

  • 我们还没有学习向量。有没有办法只使用数组来做到这一点?
  • @user2977810 看看std::unique的可能实现,对数组排序后使用。否则只问这个问题C标签,用纯C实现
【解决方案2】:

是的,如果你不想使用std,这是我刚刚写的代码sn-p。它工作正常:

const int length = 5;
int arr[length] = {2,3,2,5,1};

for(int i = 0;i < length;i++)
{
    for(int j = i + 1;j < length;j++)
    {
        if(arr[i] == arr[j] && arr[i] != -1)
            arr[j] = -1;
    }
}

int count = 0;
for(int i = 0;i < length;i++)
{
    if(arr[i] != -1)
        count ++;
}

int *B = new int[count];
int k = 0;
for(int i = 0;i < length;i++)
{
    if(arr[i] != -1)
        B[k++] = arr[i];
}
for(int i = 0;i < count;i++)
{
   cout << B[i] << " ";
}

【讨论】:

  • 我认为我使用不正确。当我使用它时,这段代码似乎只是将重复的数字替换为 -1 而不是消除它。
  • @user2977810:您的最终数组在B 中,而不是您的初始数组(在此过程中创建了一个新数组)。
  • 我要打印什么新数组?
  • @user2977810 :我已将打印部分添加到代码的末尾
【解决方案3】:

您可以尝试使用 std::list 来为您调整大小。 如果它是一个整数向量,您可以通过 std::sort 进行“免费”排序。

std::list

不仅仅是遍历列表并删除所有重复项。

std::list<int>::iterator itPrev = myList.begin();
for(std::vector<int>::iterator itcurr = ++itPrev; itCurr != myList.end(); ++itCurr)
{
    if(*itPrev == *itCurr)
    {
        myList.erase(itCurr);
    }
    ++itPrev;
}

【讨论】:

  • 我们还没有学习向量。有没有办法只使用数组来做到这一点?
猜你喜欢
  • 1970-01-01
  • 2018-11-26
  • 1970-01-01
  • 2013-03-15
  • 2021-12-28
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多