溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c++利用stl set_difference對(duì)車(chē)輛進(jìn)出區(qū)域進(jìn)行判定

發(fā)布時(shí)間:2020-09-29 02:57:53 來(lái)源:腳本之家 閱讀:204 作者:Leckun 欄目:編程語(yǔ)言

核心代碼

#include <iostream>  
#include <vector>  
#include <string>  
#include <algorithm>  
using namespace std;  
struct _AREA_VECTOR_STRUCT 
{ 
  int nAreaType;//區(qū)域類(lèi)型 
  int nAreaID;//區(qū)域ID 
}; 
 
void CtestDlg::OnBnClickedButton2() 
{ 
  vector<_AREA_VECTOR_STRUCT> structAreaHistory;//車(chē)輛上一次所在區(qū)域 
  vector<_AREA_VECTOR_STRUCT> structAreaNow;//車(chē)輛本次所在區(qū)域 
  vector<_AREA_VECTOR_STRUCT> OutStructAreaVector;//輸出區(qū)域 
 
  _AREA_VECTOR_STRUCT structVehicle; 
 
  //模擬上一次車(chē)輛所在的區(qū)域集合 
  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); 
 
  //模擬本次車(chē)輛所在的區(qū)域集合 
  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); 
 
  //排序,先按區(qū)域類(lèi)型排序,再按區(qū)域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; 
 
  //求差,結(jié)果為離開(kāi)區(qū)域的集合(出區(qū)域) 
  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();//先清空 
 
  //反過(guò)來(lái)求差,結(jié)果為新進(jìn)入的區(qū)域集合(新進(jìn)區(qū)域) 
  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("重新進(jìn)"); 
  } 
} 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI