在2D平面中找到重复的点

时间:2017-09-06 20:59:23

标签: c++ arrays struct

我试图找到给定10个点的重复点,其中每个点都有x和y值。我写了下面的代码,但无法得到正确的结果。输出应为{3,5},{4,2},{2,4},{7,8}

add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee');
function custom_applied_fee() {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

// Set HERE your categories (can be an ID, a slug or the name… or an array of this)
$category1 = 'plain';
$category2 = 'plywood';

// variables initialisation
$fee = 0;

// Iterating through each cart item
foreach(WC()->cart->get_cart() as $cart_item){
    // Get the product object
    $product = new WC_Product( $cart_item['product_id'] );
    $quantiy = $value['quantity']; //get quantity from cart

    // Initialising variables (in the loop)
    $cat1 = false; $cat2 = false;

    // ## CALCULATIONS ## (Make here your conditional calculations)
    $quantity = GET TOTAL QUANTITY OF ALL ITEMS IN PREVIOUSLY SPECIFIED CATEBORIES
    if($quantity <= 1){
        $fee += 10 * $quanity;
    } elseif($quantity > 1 && $dimention <= 9){
        $fee += 5 * $quanity;
    } elseif($dimention > 10){
        $fee += 1 * $quanity;
    }
}

// Adding the fee
if ( $fee != 0 )
    WC()->cart->add_fee( $fee_text, $fee, true );


}

3 个答案:

答案 0 :(得分:0)

我已经调整了您的distinctPoints方法,因此即使重复次数超过两次,它也不会多次打印重复项。请参阅以下编辑:

void distinctPoints(point arr[], int size)
{
  point dups[size];
  cout<<"Distinct Points"<<endl;
  cout<<"x, y"<<endl;
  for(int i = 0; i < size; i++)
    for(int j = 0; j < size; j++) {
        if ((arr[i].x==arr[j].x) && (arr[i].y==arr[j].y)) {
            if(j < i) {
                break;
            }
            else if( j == i) {
                continue;
            }
            else {
                cout<<arr[i].x <<", "<<arr[i].y<<endl;
                break;
            }
        }
    }

}

答案 1 :(得分:0)

你的方法(一旦纠正,正如VHS的回答那样)对于少数几个点可能没问题,但是,如果数据集更大,则为O(N 2 )算法可能效率太低。

您可以利用在std::unordered_set中插入元素所需的平均时间,即使您需要为您的类编写比较函数和哈希函数。

下面介绍的算法使用了两个unordered_set:

  • uniques最终会存储源容器中存在的所有元素,而不会重复。
  • repeated仅存储多次出现的元素的唯一实例。

仅当元素已存在于uniques中但未存在于repeated中时,才会将其复制到输出中。

#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <iterator>

struct point
{
    int x, y;

    bool operator== (point const& b) const
    {
        return x == b.x  &&  y == b.y;
    }
};

namespace std {

template<> struct hash<point>
{
    std::size_t operator() (point const& p) const
    {
        return (std::hash<int>{}(p.x) << 1) ^ std::hash<int>{}(p.y);
    }
};

}

std::ostream& operator<< (std::ostream& os, point const& pt)
{
    return os << '(' << pt.x << ", " << pt.y << ')';
}

template<class InputIt, class OutputIt>
OutputIt copy_repeated_values(InputIt first, InputIt last, OutputIt dest)
{
    using value_type = typename InputIt::value_type;

    std::unordered_set<value_type> uniques, repeated;

    return std::copy_if(
        first, last, dest, [&] (value_type const& value) {
            return
                not uniques.insert(value).second  &&
                repeated.insert(value).second;
        }
    );
}

int main()
{
    std::vector<point> points {
        {3,5}, {4,2}, {2,4}, {3,5}, {7,8}, {7,8}, {4,2}, {7,8}, {3,5}, {2,4}
    };

    copy_repeated_values(
        std::begin(points), std::end(points), 
        std::ostream_iterator<point>(std::cout, " ")
    );

    std::cout << '\n';
}

输出结果为:

(3, 5) (7, 8) (4, 2) (2, 4) 

答案 2 :(得分:-1)

这应该做你想要实现的目标,我在c ++中使用set和maps来处理唯一的条目。

地图会跟踪已访问过的点。

#include <iostream>
#include<stdlib.h>
#include <set>
#include <map>
using namespace std;

struct point
{
int x;
int y;
};

map<pair<int, int>, int> mapCountOfPoints;

set<pair<int, int> > disPoints;

void distinctPoints(point arr[], int size)
{
  for(int i=0; i<size; i++) {
    pair<int, int> temp = make_pair(arr[i].x, arr[i].y);
    if(mapCountOfPoints.find(temp) != mapCountOfPoints.end()) {
      disPoints.insert(temp);
    } else {
      mapCountOfPoints[temp] = 1;
    }
  }         

  // Now we will iterate over the set to get the distinct set of points
  for(set<pair<int, int>>::iterator it=disPoints.begin(); it!=disPoints.end(); it++) {
    cout<<it->first<<" "<<it->second<<endl;
  }

}
int main()
{   int size=10;
    point points[size]={{3,5},{4,2},{2,4},{3,5},{7,8},{7,8},{4,2},{7,8},{3,5},{2,4}};
    distinctPoints(points, size);
    return 0;
}

希望这有帮助!