删除2d数组中的重复值 [英] Removing duplicate values in 2d array
本文介绍了删除2d数组中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有5700行和3列2d数组。
必须从该数组中消除重复的行,并且只打印多个重复行中的一行。
count = 5700。
例如,
i have 5700 rows and 3 columns of 2d array.
have to eliminate the repeated row from that array and print only one row out of multiple repeated rows.
count=5700.
for example,
24.742235 0.005334 0.052465
27.345229 -0.222687 0.266042
27.345229 -0.222687 0.266042
25.528389 -0.063540 0.117000
24.742235 0.005334 0.052465
25.528389 -0.063540 0.117000
24.742235 0.005334 0.052465
25.528389 -0.063540 0.117000
24.742235 0.005334 0.052465
25.528389 -0.063540 0.117000
27.345229 -0.222687 0.266042
24.742235 0.005334 0.052465
27.345229 -0.222687 0.266042
25.528389 -0.063540 0.117000
25.528389 -0.063540 0.117000
27.345229 -0.222687 0.266042
27.345229 -0.222687 0.266042
24.742235 0.005334 0.052465
24.742235 0.005334 0.052465
27.345229 -0.222687 0.266042
我尝试过:
What I have tried:
for(p=0;p<count;p++)
{
for(ppp=p+1;ppp<count;ppp++)
{
if(plane[ppp][1]!=plane[p][1])
{
printf("%lf\t%lf\t%lf\n",plane[p][0],plane[p][1],plane[p][2]);
printf("%lf\t%lf\t%lf\n",plane[ppp][0],plane[ppp][1],plane[ppp][2]);
}
}
}
推荐答案
如果你想删除所有重复项,那么你就是只留下不同的行,然后是最简单的方法,如果要对输入进行排序,那么重复是彼此相邻的。
这样,删除重复的行是微不足道的。
C和C ++都包含用于对数组进行排序的库例程。
If you want to remove all duplicates, so you are left with the distinct rows only, then the simplest way if to sort the input, so that duplicates are next to each other.
that way, it's trivial to remove duplicated rows.
C and C++ both include library routines to sort an array.
不改变顺序的解决方案(未经测试):
A solution that does not change the order (untested):
/* Print first item */
printf("%lf\t%lf\t%lf\n",plane[0][0],plane[0][1],plane[0][2]);
for(p=1;p<count;p++)
{
/* Check if a duplicate (already printed) */
for(ppp=0;ppp<p;ppp++)
{
if(plane[p][1]==plane[ppp][1])
break;
}
/* Not a duplicate if above loop not terminated */
if (p >= ppp)
printf("%lf\t%lf\t%lf\n",plane[p][0],plane[p][1],plane[p][2]);
}
利用C ++
库,您可以使用std :: set
class。例如:
Taking advantage of theC++
library, you may use thestd::set
class. For instance:
#include <iostream>
#include <set>
using namespace std;
struct DataInfo
{
double d[3];
bool operator()( const DataInfo & dia, const DataInfo & dib) const
{
for (size_t n=0; n<3; ++n)
{
if ( dia.d[n] < dib.d[n] ) return true;
if ( dia.d[n] > dib.d[n] ) return false;
}
return false;
}
};
int main()
{
set <DataInfo, DataInfo> sdi;
for (;;)
{
DataInfo di;
cin >> di.d[0] >> di.d[1] >> di.d[2];
if ( cin.eof()) break;
sdi.insert(di);
}
for (const auto & di : sdi)
cout << di.d[0] << " " << di.d[1] << di.d[2] << endl;
}
这篇关于删除2d数组中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文