首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

list容器重复删除及计数关联有关问题

2012-04-04 
list容器重复删除及计数关联问题首先有一个结构体struct node{double lengthdouble widthdouble height

list容器重复删除及计数关联问题
首先有一个结构体  
  struct node  
 {  
  double length;
  double width; 
  double height;
 }  

然后将node类型的数据全部都放进了LIST容器里面 大概有10多万条数据 其中有很多重复数据

list<node> nodelist;

我现在想把 每个节点里面 length,width,height 三条数据一样的节点合并为一个 并且都有计数

就好比像是

  length width height sum(数量)
  11.1 2.12 23.1 6500
  22.3 21.0 45.2 10000
  ..............................

请高手帮忙! (小弟新手能否用代码演示)

[解决办法]
提供一个笨方法,将list数据放到1个vector里

#include <stdlib.h>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

struct node
{
double length;
double width;
double height;
}; 

struct nodeEx
{
double length;
double width;
double height;
int count;
};

bool nodeEqual(node &lvalue, node &rvalue)
{
if (lvalue.length ==rvalue.length && lvalue.width == rvalue.width && lvalue.height == rvalue.height)
{
return true;

else
{
return false;
}
}

bool nodeExEqual(nodeEx &lvalue, nodeEx &rvalue)
{
if (lvalue.length ==rvalue.length && lvalue.width == rvalue.width && lvalue.height == rvalue.height && lvalue.count == rvalue.count)
{
return true;

else
{
return false;
}
}

list<node> nodelist;
vector<nodeEx> nodeExVec;

void ReadData(int n)
{
node object;
object.length = n;
object.width = n;
object.height = n;
nodelist.push_back(object);
}

void UnionData()
{
for (list<node>::iterator it = nodelist.begin();it !=nodelist.end(); it++)
{
int count = 1;
list<node>::iterator it1 = it;
it1++;
for (; it1 != nodelist.end(); it1++)
{
if (nodeEqual(*it, *it1))
{
count++;
}
}
nodeEx object;
object.length = it->length;
object.width = it->width;
object.height = it->height;
object.count = count;
vector<nodeEx>::iterator vecit = nodeExVec.begin();
for (; vecit != nodeExVec.end(); vecit++)
{
if (nodeExEqual(object, *vecit))
{
break;
}
}
if (vecit == nodeExVec.end())
{
nodeExVec.push_back(object);
}
}
}

int main()
{
//读取10万个数据,可以从文件读取
for (int i=0; i< 100000; i++)
{
ReadData(rand()%1000);
}
//合并数据,将list的数据存到vector里
UnionData();
//打印数据
for (int i = 0; i<nodeExVec.size(); i++)
{
cout << nodeExVec[i].length<<" "<<nodeExVec[i].width<<" "<<nodeExVec[i].height<<" "<<nodeExVec[i].count<<endl;
}
return 1;
}

热点排行