1. 程式人生 > >剔除list中相同的結構體數據

剔除list中相同的結構體數據

我們 net bar local mod algorithm compare remote AR

剔除list中相同的結構體數據,有三個思路:
1、兩層循環,逐個比較

2、使用set容器來剔除

3、使用unique方法去重

[cpp] view plain copy
  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中相同的結構體數據