剔除list中相同的结构体数据
剔除list中相同的结构体数据,有三个思路:
1、两层循环,逐个比较
2、使用set容器来剔除
3、使用unique方法去重
- // deduplication.cpp : 定义控制台应用程序的入口点。
- //
- #include <list>
- #include <set>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <tchar.h>
- using namespace std;
- struct tagRecentGameItem
- {
- std::wstring strGameCode;
- std::wstring strServerID;
- __int64 nTime;
- tagRecentGameItem()
- : strGameCode(L"")
- , strServerID(L"")
- , nTime(0)
- {
- }
- bool operator < (const tagRecentGameItem& right) const
- {
- if (this->nTime > right.nTime)
- {
- return true;
- }
- if (this->nTime == right.nTime)
- {
- //compare函数在>时返回1,<时返回-1,==时返回0
- return this->strGameCode.compare(right.strGameCode) < 0;
- }
- return false;
- }
- bool operator == (const tagRecentGameItem& right) const
- {
- bool bEqual = false;
- //_tcsicmp不区分大小写,wcscmp区分大小写
- bEqual = ( 0 == _tcsicmp(this->strGameCode.c_str(), right.strGameCode.c_str()) );
- bEqual &= ( 0 == _tcsicmp(this->strServerID.c_str(), right.strServerID.c_str()) );
- return bEqual;
- }
- };
- //
- typedef std::list<tagRecentGameItem> listRecentGame;
- typedef std::set<tagRecentGameItem> setRecentGame;
- //
- listRecentGame m_LocalList;
- listRecentGame m_RemoteList;
- setRecentGame m_setGames;
- //打印结果
- void print_result(listRecentGame& items)
- {
- listRecentGame::iterator iter = items.begin();
- for (iter; iter!=items.end(); ++iter)
- {
- printf("gameCode: %ls\n", iter->strGameCode.c_str());
- }
- printf("\n");
- }
- //逐个比较
- void deduplication1(listRecentGame& items)
- {
- printf("method :deduplication1\n");
- items.sort();//需要重载操作符<
- listRecentGame::iterator itrB = items.begin();
- listRecentGame::iterator itrE = items.end();
- listRecentGame::iterator itr;
- for (itrB; itrB != itrE; ++itrB)
- {
- itr = itrB;
- ++itr;
- for(itr; itr != itrE;)
- {
- if (*itr == *itrB)
- {
- items.erase(itr++);
- }
- else
- {
- ++itr;
- }
- }
- }
- //打印结果
- print_result(items);
- }
- //利用set容器特性去重
- void deduplication2(listRecentGame& items)
- {
- printf("method :deduplication2\n");
- listRecentGame::iterator iter1 = items.begin();
- listRecentGame::iterator iter2 = items.end();
- for (iter1; iter1 != iter2; ++iter1)
- {
- //需要重载操作符<
- m_setGames.insert(*iter1);
- }
- //再写回list
- items.clear();
- setRecentGame::iterator pos = m_setGames.begin();
- for (pos; pos!=m_setGames.end(); ++pos)
- {
- items.push_back(*pos);
- }
- //打印结果
- print_result(items);
- }
- //stl的unique方法去重
- void deduplication3(listRecentGame& items)
- {
- printf("method :deduplication3\n");
- //unique函数功能是去除相邻的重复元素,注意是相邻,所以必须先使用sort函数。
- items.sort();
- //unique必须重载==操作符
- listRecentGame::iterator new_end = unique(items.begin(), items.end());
- items.erase(new_end, items.end());
- //打印结果
- print_result(items);
- }
- //
- int _tmain(int argc, _TCHAR* argv[])
- {
- //装载本地记录
- tagRecentGameItem item;
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"abc";
- item.strServerID = L"s31";
- item.nTime = 20160501183417;
- m_LocalList.push_back(item);
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"bcd";
- item.strServerID = L"s32";
- item.nTime = 20160501183418;
- m_LocalList.push_back(item);
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"cde";
- item.strServerID = L"s33";
- item.nTime = 20160501183419;
- m_LocalList.push_back(item);
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"def";
- item.strServerID = L"s34";
- item.nTime = 20160501183420;
- m_RemoteList.push_back(item);
- //装载远程记录
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"abc";
- item.strServerID = L"s31";
- item.nTime = 20160501183417;
- m_RemoteList.push_back(item);
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"bcd";
- item.strServerID = L"s32";
- item.nTime = 20160501183418;
- m_RemoteList.push_back(item);
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"cde";
- item.strServerID = L"s33";
- item.nTime = 20160501183419;
- m_RemoteList.push_back(item);
- memset(&item, 0, sizeof(item));
- item.strGameCode = L"def0";
- item.strServerID = L"s34";
- item.nTime = 20160501183420;
- m_RemoteList.push_back(item);
- //合并到一个list
- m_LocalList.insert(m_LocalList.begin(), m_RemoteList.begin(), m_RemoteList.end());
- deduplication1(m_LocalList);
- deduplication2(m_LocalList);
- deduplication3(m_LocalList);
- system("pause");
- return 0;
- }
运行结果:
需要注意的地方:
STL中的排序都是默认使用小于号来排序。因此,在对结构体排序时,我们就需要重载小于号!比如:set容器在执行insert操作时,必须重载操作符<。另外,unique函数功能是去除相邻的重复元素,而在执行unique操作时必须重载操作符==。
https://blog.csdn.net/hellokandy/article/details/51333942
剔除list中相同的结构体数据的更多相关文章
- GPGPU OpenCL使用结构体数据
OpenCL编程中可以使用结构体,只需要在核函数kernel中提供同样的结构体申明就可以啦. 如果在主函数中定义了结构体: typedef struct studentNode{ int age; f ...
- 计算机二级-C语言-对结构体数据进行求平均值。对结构体数据进行比较处理。
//函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),作为函数返回,并将大于平均值的数放在形参y所指数组中,在主函数中输出. //重难点:对结构体数据进行求平均值. #in ...
- 如何系统学习C 语言(中)之 结构体篇
1,结构体 在前面我们知道变量和数组都可以用来存储数据,变量用来存储单个数据,数组可以用来存储一组同类型的数据,但你有没有发现--它们都只适合单一属性的数据.那现实生活中,很多对象都是具有多属性的.例 ...
- MFC中的NMHDR结构体和NMUPDOWN结构体
建立spin控件,创建UDN_DELTAPOS一个消息函数后: void CSpinDlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult) { NM ...
- C语言中 不定义结构体变量求成员大小
所谓的求成员大小, 是求成员在该结构体中 用 sizeof(结构体名.结构体成员名) 求来的. 很多时候我们需要知道一个结构体成员中的某个成员的大小, 但是我们又不需要定义该结构体类型的变量(定义的话 ...
- 问题解决——在结构体中使用set保存结构体数据
=====================声明========================== 本文原创,转载请明确的注明出处和作者,并保持文章的完整性(包括本声明部分). 本文链接:http:/ ...
- 【2016-08-18】转载:总结C++中几种结构体初始化的方法
作者:Ac_Von 博客地址:http://www.cnblogs.com/vongang/ 文章地址:http://www.cnblogs.com/vongang/archive/2011/07/3 ...
- Swift中元组(Tuples),结构体(Struct),枚举(Enums)之间的区别
Swift有许多种存储数据方式,你可以用枚举(enums),元组(tuples),结构体(structs),类(classes),在这篇文章中我们将比较枚举.元组.结构体之间区别,首先从最简单的开始- ...
- OC中常见的结构体,以及NSNumber、NSValue、NSDate的使用
常见的结构体 NSPoint和CGPoint NSSize和CGSize NSRect 和 CGRect NSPoint和CGPoint的使用 NSPoint和CGPoint是同义的 typedef ...
随机推荐
- 银行测试 http://blog.csdn.net/stillming/article/details/42275251
从一家工作了五年的软件公司的测试管理者跳槽到**银行做软件测试,短短两个月,对银行测试有了初步认识,总结和记录下来,加深个人的理解,同时也共享给各位. 银行作为大家的理财顾问,对金钱非常敏感,频繁甚至 ...
- MFC CListctr显示缩略图
我们知道通过CImageList可以让listctr显示出图片,但是添加的图片大小必须和要CImageList 创建的图片大小一致,才能显示出来.最近遇到一个需求,需要把很多大小不一的jpeg图片通过 ...
- 让我们彻底看清MVC、MVP
这里開始记录下来自己对MVC.MVP.MVVM这三种框架模式的理解,本文从以下几个方面来梳理. 架构的目的 框架模式.设计模式 MVC设计的介绍 MVC在Android中的应用 MVC该怎样设计 MV ...
- Android LruCache类分析
public class LurCache<K, V> { private final LinkedHashMap<K, V> map; private int size; / ...
- transform属性实现翻转效果
transform:perspective(800px) rotateY(180deg);//翻转180度,透视800px; transition-delay: 0.3s;//过程时间 opacity ...
- VC++ 6.0 BUG BUG BUG BUG BUG
http://blog.163.com/amao831@126/blog/#m=0 我经常在的VC++6.0中 定义某个类的对象时 再用.访问或者->访问时不自动弹出他的成员函数或者成员变量 最 ...
- ASP.NET MVC 入门4、Controller与Action
原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/04/1303672.html Controller是MVC中比較重要的一部分.差点儿全部的业 ...
- Socket 长连接与短连接,心跳
长连接与短连接 所谓长连接,指在一个TCP连接上可以连续发送多个数据包,在TCP连接保持期间,如果没有数据包发送,需要双方发检测包以维持此连接,一般需要自己做在线维持. 短连接是指通信双方有数据交互时 ...
- Android 监听软键盘点击回车及换行事件
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean ...
- Centos minimal 安装桌面
yum update yum groupinstall -y 'X Window System' yum groupinstall -y 'Desktop' #中文支持 yum groupinstal ...