【问题标题】:Merge unique elements from two arrays [closed]合并来自两个数组的唯一元素[关闭]
【发布时间】:2014-07-17 12:21:38
【问题描述】:

我有两个数组

array1 = { 2 , 4 , 1 , 5 , 7 , 10 } ;

array2 = { 3 , 1 , 6 , 8 , 9 , 4 };

我的结果数组应该是两个数组中的唯一元素,即

Result = { 2 , 5 , 7 , 10 , 3 , 6 , 8 , 9 };

条件:数组将是未排序的整数值。

【问题讨论】:

  • 这个问题似乎离题了,因为 SO 不是代码编写服务,请展示想法、尝试以及您的代码在哪里不起作用
  • @EdChum 如果他没有想法,他如何展示想法?!:)

标签: c++ ios objective-c c arrays


【解决方案1】:

对数组进行排序,然后对它们执行std::set_symmetric_difference

【讨论】:

    【解决方案2】:

    这是一种使用标准算法的简单方法

    #include <iostream>
    #include <algorithm>
    #include <iterator>
    
    int main() 
    {
        int a1[] = { 2 , 4 , 1 , 5 , 7 , 10 } ;
        int a2[] = { 3 , 1 , 6 , 8 , 9 , 4 };
    
        for ( int x : a1 ) std::cout << x << ' ';
        std::cout << std::endl;
    
        for ( int x : a2 ) std::cout << x << ' ';
        std::cout << std::endl;
    
        size_t n = std::count_if( std::begin( a1 ), std::end( a1 ),
            [&]( int x )
            {
                return
                std::find( std::begin( a2 ), std::end( a2 ), x ) == std::end( a2 );
            });
    
        n += std::count_if( std::begin( a2 ), std::end( a2 ),
            [&]( int x )
            {
                return
                std::find( std::begin( a1 ), std::end( a1 ), x ) == std::end( a1 );
            });
    
        int *result = new int[n];
        int *p = result;
    
        p = std::copy_if( std::begin( a1 ), std::end( a1 ), p,
            [&]( int x )
            {
                return
                std::find( std::begin( a2 ), std::end( a2 ), x ) == std::end( a2 );
            });
    
        std::copy_if( std::begin( a2 ), std::end( a2 ), p,
            [&]( int x )
            {
                return
                std::find( std::begin( a1 ), std::end( a1 ), x ) == std::end( a1 );
            });
    
        for ( p = result; p != result + n; ++p ) std::cout << *p << ' ';
        std::cout << std::endl;
    
        delete [] result;
    
        return 0;
    }
    

    输出是

    2 4 1 5 7 10 
    3 1 6 8 9 4 
    2 5 7 10 3 6 8 9 
    

    您可以简单地写成 a1a1 + 6 而不是 std::begin( a1 )std::end( a1 ),其中 6 是 a1 的大小。

    如果允许您对原始数组进行排序,那么程序可能如下所示

    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    int main() 
    {
        int a1[] = { 2 , 4 , 1 , 5 , 7 , 10 } ;
        int a2[] = { 3 , 1 , 6 , 8 , 9 , 4 };
    
        for ( int x : a1 ) std::cout << x << ' ';
        std::cout << std::endl;
    
        for ( int x : a2 ) std::cout << x << ' ';
        std::cout << std::endl;
    
        std::sort( std::begin( a1 ), std::end( a1 ) );
        std::sort( std::begin( a2 ), std::end( a2 ) );
    
        size_t n = std::count_if( std::begin( a1 ), std::end( a1 ),
            [&]( int x )
            {
                return
                !std::binary_search( std::begin( a2 ), std::end( a2 ), x );
            } );
    
        n += std::count_if( std::begin( a2 ), std::end( a2 ),
            [&]( int x )
            {
                return
                !std::binary_search( std::begin( a1 ), std::end( a1 ), x );
            } );
    
        int *result = new int[n];
    
        std::set_symmetric_difference( std::begin( a1 ), std::end( a1 ),
                                       std::begin( a2 ), std::end( a2 ),
                                       result );
    
        for ( int *p = result; p != result + n; ++p ) std::cout << *p << ' ';
        std::cout << std::endl;
    
        delete [] result;
    
        return 0;
    }
    

    输出是

    2 4 1 5 7 10 
    3 1 6 8 9 4 
    2 3 5 6 7 8 9 10 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多