剔除list中相同的结构体数据

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

运行结果:

需要注意的地方:

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

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

原文地址:https://www.cnblogs.com/findumars/p/8732309.html