剔除list中相同的结构体数据,有三个思路:
1、两层循环,逐个比较

2、使用set容器来剔除

3、使用unique方法去重

  1. // deduplication.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <tchar.h>
  9. using namespace std;
  10. struct tagRecentGameItem
  11. {
  12. std::wstring strGameCode;
  13. std::wstring strServerID;
  14. __int64 nTime;
  15. tagRecentGameItem()
  16. : strGameCode(L"")
  17. , strServerID(L"")
  18. , nTime(0)
  19. {
  20. }
  21. bool operator < (const tagRecentGameItem& right) const
  22. {
  23. if (this->nTime > right.nTime)
  24. {
  25. return true;
  26. }
  27. if (this->nTime == right.nTime)
  28. {
  29. //compare函数在>时返回1,<时返回-1,==时返回0
  30. return this->strGameCode.compare(right.strGameCode) < 0;
  31. }
  32. return false;
  33. }
  34. bool operator == (const tagRecentGameItem& right) const
  35. {
  36. bool bEqual = false;
  37. //_tcsicmp不区分大小写,wcscmp区分大小写
  38. bEqual = ( 0 == _tcsicmp(this->strGameCode.c_str(), right.strGameCode.c_str()) );
  39. bEqual &= ( 0 == _tcsicmp(this->strServerID.c_str(), right.strServerID.c_str()) );
  40. return bEqual;
  41. }
  42. };
  43. //
  44. typedef std::list<tagRecentGameItem> listRecentGame;
  45. typedef std::set<tagRecentGameItem> setRecentGame;
  46. //
  47. listRecentGame m_LocalList;
  48. listRecentGame m_RemoteList;
  49. setRecentGame m_setGames;
  50. //打印结果
  51. void print_result(listRecentGame& items)
  52. {
  53. listRecentGame::iterator iter = items.begin();
  54. for (iter; iter!=items.end(); ++iter)
  55. {
  56. printf("gameCode: %ls\n", iter->strGameCode.c_str());
  57. }
  58. printf("\n");
  59. }
  60. //逐个比较
  61. void deduplication1(listRecentGame& items)
  62. {
  63. printf("method :deduplication1\n");
  64. items.sort();//需要重载操作符<
  65. listRecentGame::iterator itrB = items.begin();
  66. listRecentGame::iterator itrE = items.end();
  67. listRecentGame::iterator itr;
  68. for (itrB; itrB != itrE; ++itrB)
  69. {
  70. itr = itrB;
  71. ++itr;
  72. for(itr; itr != itrE;)
  73. {
  74. if (*itr == *itrB)
  75. {
  76. items.erase(itr++);
  77. }
  78. else
  79. {
  80. ++itr;
  81. }
  82. }
  83. }
  84. //打印结果
  85. print_result(items);
  86. }
  87. //利用set容器特性去重
  88. void deduplication2(listRecentGame& items)
  89. {
  90. printf("method :deduplication2\n");
  91. listRecentGame::iterator iter1 = items.begin();
  92. listRecentGame::iterator iter2 = items.end();
  93. for (iter1; iter1 != iter2; ++iter1)
  94. {
  95. //需要重载操作符<
  96. m_setGames.insert(*iter1);
  97. }
  98. //再写回list
  99. items.clear();
  100. setRecentGame::iterator pos = m_setGames.begin();
  101. for (pos; pos!=m_setGames.end(); ++pos)
  102. {
  103. items.push_back(*pos);
  104. }
  105. //打印结果
  106. print_result(items);
  107. }
  108. //stl的unique方法去重
  109. void deduplication3(listRecentGame& items)
  110. {
  111. printf("method :deduplication3\n");
  112. //unique函数功能是去除相邻的重复元素,注意是相邻,所以必须先使用sort函数。
  113. items.sort();
  114. //unique必须重载==操作符
  115. listRecentGame::iterator new_end = unique(items.begin(), items.end());
  116. items.erase(new_end, items.end());
  117. //打印结果
  118. print_result(items);
  119. }
  120. //
  121. int _tmain(int argc, _TCHAR* argv[])
  122. {
  123. //装载本地记录
  124. tagRecentGameItem item;
  125. memset(&item, 0, sizeof(item));
  126. item.strGameCode = L"abc";
  127. item.strServerID = L"s31";
  128. item.nTime = 20160501183417;
  129. m_LocalList.push_back(item);
  130. memset(&item, 0, sizeof(item));
  131. item.strGameCode = L"bcd";
  132. item.strServerID = L"s32";
  133. item.nTime = 20160501183418;
  134. m_LocalList.push_back(item);
  135. memset(&item, 0, sizeof(item));
  136. item.strGameCode = L"cde";
  137. item.strServerID = L"s33";
  138. item.nTime = 20160501183419;
  139. m_LocalList.push_back(item);
  140. memset(&item, 0, sizeof(item));
  141. item.strGameCode = L"def";
  142. item.strServerID = L"s34";
  143. item.nTime = 20160501183420;
  144. m_RemoteList.push_back(item);
  145. //装载远程记录
  146. memset(&item, 0, sizeof(item));
  147. item.strGameCode = L"abc";
  148. item.strServerID = L"s31";
  149. item.nTime = 20160501183417;
  150. m_RemoteList.push_back(item);
  151. memset(&item, 0, sizeof(item));
  152. item.strGameCode = L"bcd";
  153. item.strServerID = L"s32";
  154. item.nTime = 20160501183418;
  155. m_RemoteList.push_back(item);
  156. memset(&item, 0, sizeof(item));
  157. item.strGameCode = L"cde";
  158. item.strServerID = L"s33";
  159. item.nTime = 20160501183419;
  160. m_RemoteList.push_back(item);
  161. memset(&item, 0, sizeof(item));
  162. item.strGameCode = L"def0";
  163. item.strServerID = L"s34";
  164. item.nTime = 20160501183420;
  165. m_RemoteList.push_back(item);
  166. //合并到一个list
  167. m_LocalList.insert(m_LocalList.begin(), m_RemoteList.begin(), m_RemoteList.end());
  168. deduplication1(m_LocalList);
  169. deduplication2(m_LocalList);
  170. deduplication3(m_LocalList);
  171. system("pause");
  172. return 0;
  173. }

运行结果:

需要注意的地方:

STL中的排序都是默认使用小于号来排序。因此,在对结构体排序时,我们就需要重载小于号!比如:set容器在执行insert操作时,必须重载操作符<。另外,unique函数功能是去除相邻的重复元素,而在执行unique操作时必须重载操作符==。

https://blog.csdn.net/hellokandy/article/details/51333942

剔除list中相同的结构体数据的更多相关文章

  1. GPGPU OpenCL使用结构体数据

    OpenCL编程中可以使用结构体,只需要在核函数kernel中提供同样的结构体申明就可以啦. 如果在主函数中定义了结构体: typedef struct studentNode{ int age; f ...

  2. 计算机二级-C语言-对结构体数据进行求平均值。对结构体数据进行比较处理。

    //函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),作为函数返回,并将大于平均值的数放在形参y所指数组中,在主函数中输出. //重难点:对结构体数据进行求平均值. #in ...

  3. 如何系统学习C 语言(中)之 结构体篇

    1,结构体 在前面我们知道变量和数组都可以用来存储数据,变量用来存储单个数据,数组可以用来存储一组同类型的数据,但你有没有发现--它们都只适合单一属性的数据.那现实生活中,很多对象都是具有多属性的.例 ...

  4. MFC中的NMHDR结构体和NMUPDOWN结构体

    建立spin控件,创建UDN_DELTAPOS一个消息函数后: void CSpinDlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult) { NM ...

  5. C语言中 不定义结构体变量求成员大小

    所谓的求成员大小, 是求成员在该结构体中 用 sizeof(结构体名.结构体成员名) 求来的. 很多时候我们需要知道一个结构体成员中的某个成员的大小, 但是我们又不需要定义该结构体类型的变量(定义的话 ...

  6. 问题解决——在结构体中使用set保存结构体数据

    =====================声明========================== 本文原创,转载请明确的注明出处和作者,并保持文章的完整性(包括本声明部分). 本文链接:http:/ ...

  7. 【2016-08-18】转载:总结C++中几种结构体初始化的方法

    作者:Ac_Von 博客地址:http://www.cnblogs.com/vongang/ 文章地址:http://www.cnblogs.com/vongang/archive/2011/07/3 ...

  8. Swift中元组(Tuples),结构体(Struct),枚举(Enums)之间的区别

    Swift有许多种存储数据方式,你可以用枚举(enums),元组(tuples),结构体(structs),类(classes),在这篇文章中我们将比较枚举.元组.结构体之间区别,首先从最简单的开始- ...

  9. OC中常见的结构体,以及NSNumber、NSValue、NSDate的使用

    常见的结构体 NSPoint和CGPoint NSSize和CGSize NSRect 和 CGRect NSPoint和CGPoint的使用 NSPoint和CGPoint是同义的 typedef ...

随机推荐

  1. Spider_scrapy

    多线程爬虫 进程线程回顾 进程 系统中正在运行的一个应用程序 1个CPU核心1次只能执行1个进程,其他进程处于非运行状态 N个CPU核心可同时执行N个任务 线程 进程中包含的执行单元,1个进程可包含多 ...

  2. 配置spotlight连接linux服务器

    本文转自(https://blog.csdn.net/qq_31391261/article/details/79429098) 一.配置spotlight连接linux服务器 1.以管理员身份运行软 ...

  3. SSO单点登录学习总结(1)——单点登录(SSO)原理解析

    SSO的概念: 单点登录SSO(Single Sign-On)是身份管理中的一部分.SSO的一种较为通俗的定义是:SSO是指访问同一服务器不同应用中的受保护资源的同一用户,只需要登录一次,即通过一个应 ...

  4. 三菱FX系列PLC学习

    1.PLC工作原理 PLC将程序存储在用户存储器当中, 驱动其运行, 相对比微型计算机软件, PLC程序则不同的是, 微型计算机整个流程则是从规定的开始 至结束完整工作流程.相对与PLC运行,则是从位 ...

  5. Python 极简教程(八)字符串 str

    由于字符串过于重要,请认真看完并保证所有代码都至少敲过一遍. 对于字符串,前面在数据类型中已经提到过.但是由于字符串类型太过于常用,Python 中提供了非常多的关于字符串的操作.而我们在实际编码过程 ...

  6. C# List 复制克隆副本

    [C#技术] C# List 复制克隆副本数字人 发表于:2012-8-28 18:02:49废话不多说,看代码: 方法一: List<string> t = new List<st ...

  7. EventWaitHandle

    在查资料的过程中,我突然想到一个类:EventWaitHandle,也就是本文的主角. 这个类通过在线程之间设置信号量,可以非常方便的控制线程运行的顺序.具体代码如下: 首先全局申明: EventWa ...

  8. 【例题 7-6 UVA - 140】Bandwidth

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力做就好. O(8!*26^2) [代码] /* 1.Shoud it use long long ? 2.Have you ev ...

  9. 关于C++中用两个迭代器方式初始化string的知识

    string(iter1, iter2); 第一点:两个迭代器必须指向同一个容器. 第二点:iter2必须>=iter1. 第三点:假设iter1等于iter2,那么结果为空[] 另外一个比較特 ...

  10. Android 软键盘弹出,界面整体上移的问题

    AndroidManifest.xml文件中界面对应的<activity>里加入android:windowSoftInputMode="adjustPan" 键盘就会 ...