剔除list中相同的結構體數據
阿新 • • 發佈:2018-04-07
我們 net bar local mod algorithm compare remote AR
剔除list中相同的結構體數據,有三個思路:
1、兩層循環,逐個比較
2、使用set容器來剔除
3、使用unique方法去重
[cpp] view plain copy
- // 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中相同的結構體數據