您好,登錄后才能下訂單哦!
核心代碼
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; struct _AREA_VECTOR_STRUCT { int nAreaType;//區域類型 int nAreaID;//區域ID }; void CtestDlg::OnBnClickedButton2() { vector<_AREA_VECTOR_STRUCT> structAreaHistory;//車輛上一次所在區域 vector<_AREA_VECTOR_STRUCT> structAreaNow;//車輛本次所在區域 vector<_AREA_VECTOR_STRUCT> OutStructAreaVector;//輸出區域 _AREA_VECTOR_STRUCT structVehicle; //模擬上一次車輛所在的區域集合 structVehicle.nAreaType = 2; structVehicle.nAreaID = 0x45; structAreaHistory.push_back(structVehicle); structVehicle.nAreaID = 0x7A; structAreaHistory.push_back(structVehicle); structVehicle.nAreaID = 0x88; structAreaHistory.push_back(structVehicle); structVehicle.nAreaType = 3; structVehicle.nAreaID = 0x55; structAreaHistory.push_back(structVehicle); structVehicle.nAreaID = 0x88; structAreaHistory.push_back(structVehicle); //模擬本次車輛所在的區域集合 structVehicle.nAreaType = 2; structVehicle.nAreaID = 0x88; structAreaNow.push_back(structVehicle); structVehicle.nAreaID = 0x45; structAreaNow.push_back(structVehicle); structVehicle.nAreaID = 0x11; structAreaNow.push_back(structVehicle); structVehicle.nAreaType = 3; structVehicle.nAreaID = 0x55; structAreaNow.push_back(structVehicle); //排序,先按區域類型排序,再按區域ID排序 sort(structAreaHistory.begin(), structAreaHistory.end(),[] (_AREA_VECTOR_STRUCT structArea1, _AREA_VECTOR_STRUCT structArea2)->bool { if(structArea1.nAreaType != structArea2.nAreaType) return structArea1.nAreaType < structArea2.nAreaType; else return structArea1.nAreaID < structArea2.nAreaID; }); sort(structAreaNow.begin(), structAreaNow.end(),[] (_AREA_VECTOR_STRUCT structArea1, _AREA_VECTOR_STRUCT structArea2)->bool { if(structArea1.nAreaType != structArea2.nAreaType) return structArea1.nAreaType < structArea2.nAreaType; else return structArea1.nAreaID < structArea2.nAreaID; }); int a = 0; //求差,結果為離開區域的集合(出區域) set_difference(structAreaHistory.begin(), structAreaHistory.end(), structAreaNow.begin(), structAreaNow.end(), back_inserter(OutStructAreaVector), [](_AREA_VECTOR_STRUCT structArea1, _AREA_VECTOR_STRUCT structArea2)->bool { if(structArea1.nAreaType != structArea2.nAreaType) return structArea1.nAreaType < structArea2.nAreaType; else return structArea1.nAreaID < structArea2.nAreaID; }); if(OutStructAreaVector.size() != 0) { TRACE("出"); } OutStructAreaVector.clear();//先清空 //反過來求差,結果為新進入的區域集合(新進區域) set_difference(structAreaNow.begin(), structAreaNow.end(), structAreaHistory.begin(), structAreaHistory.end(), back_inserter(OutStructAreaVector), [](_AREA_VECTOR_STRUCT structArea1, _AREA_VECTOR_STRUCT structArea2)->bool { if(structArea1.nAreaType != structArea2.nAreaType) return structArea1.nAreaType < structArea2.nAreaType; else return structArea1.nAreaID < structArea2.nAreaID; }); if(OutStructAreaVector.size() != 0) { TRACE("重新進"); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。