【问题标题】:c++ identical element removal from two arrays which do not contain the number itself inc++ 从两个不包含数字本身的数组中删除相同的元素
【发布时间】:2020-07-24 16:49:10
【问题描述】:

问题陈述:有两个数组。一些元素在这些数组中很常见。像 arr1={5,2,6};和 arr2 = {7,9,5,2,3}。因此,5,2 是公共元素,因为它们存在于两个数组中。根据问题,我需要从数组中消除常见元素。所以结果数组包含 arr1={5} 和 arr2={7,9,3}。

我的方法是首先将两个数组合并到第三个数组中,然后使用集合操作消除重复元素。但是在这种方法中,第二次出现的元素被删除,但列表中的元素本身。另外,我得到一个列表而不是两个单独的列表。

我的代码:

#include <iostream>
#include <string>
#include<unordered_set>

int main()
{
    std::unordered_set<int> remove_duplicate;
    int arr[3]={5,2,6};
    int arr1[5]={7,9,5,2,3};
    int m= sizeof(arr)/sizeof(arr[0]);
    int n= sizeof(arr1)/sizeof(arr1[0]);
    int mergearr[m+n];
    std::copy(arr,arr+m,mergearr);
    std::copy(arr1,arr1+n,mergearr+m);
  for(int i=0;i<(m+n);i++){
  std::cout<<mergearr[i]<< ' ';
  }
  std::cout<< '\n';


 for(int j=0;j<(m+n);j++){
  remove_duplicate.insert(mergearr[j]);
 }
 for(auto it=remove_duplicate.begin();it!=remove_duplicate.end();++it){

  std::cout<<' '<<*it;
  }
  return 0;
}

输出:

5 2 6 7 9 5 2 3 
  3 9 7 6 2 5 

我该如何解决这个问题?提前谢谢你。

【问题讨论】:

  • 我该如何解决这个问题? -- 对两个数组进行排序后使用std::set_intersection。其次,int mergearr[m+n];——你需要将mn设为const int,否则该声明是无效的C++。
  • 另外,您不能从数组中删除元素,因为数组大小是固定的。
  • 另外,为什么arr 的答案等于5?不应该是6吗?

标签: c++ arrays duplicates set


【解决方案1】:

正如 PaulMcKenzie 的评论所说,您希望删除列表的集合交集中的所有项目。使用 std::set_intersection 或遍历第二个数组中的元素并检查它们是否在第一个中(使用集合)。

【讨论】:

    【解决方案2】:

    首先,数组的大小是固定的,因此无法从数组中擦除元素,除非您定义了“从数组中擦除”的含义。因此更适合使用的容器是std::vector&lt;int&gt;

    鉴于,如果允许对两个序列进行排序,则可以使用std::set_intersection 首先找到共同元素。然后使用生成的公共元素集在两个向量上使用std::remove_if

    这是一个例子:

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    #include <unordered_set>
    
    int main()
    {
        std::vector<int> arr = {5,2,6};
        std::vector<int> arr1 = {7,9,5,2,3};
    
        // First, sort both containers
        std::sort(arr.begin(), arr.end());
        std::sort(arr1.begin(), arr1.end());
    
        // Next, call std::set_intersection to collect the common elements into 
        // the unordered_set
        std::unordered_set<int> common;
        std::set_intersection(arr.begin(), arr.end(), arr1.begin(), arr1.end(), 
                              std::inserter(common, common.begin()));
    
        // Now erase the items in each vector that matches the items in the set
        arr.erase(std::remove_if(arr.begin(), arr.end(), 
                                 [&](int n) { return common.count(n); }), arr.end());
        arr1.erase(std::remove_if(arr1.begin(), arr1.end(), 
                                 [&](int n) { return common.count(n); }), arr1.end());
    
        // Output results
        for (auto i : arr)
           std::cout << i << " ";
        std::cout << "\n";
        for (auto i : arr1)
           std::cout << i << " ";
    }
    

    输出:

    6 
    3 7 9 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 2016-12-27
      • 1970-01-01
      • 2014-01-31
      • 2023-03-18
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      相关资源
      最近更新 更多