c++ USES stl set_difference to determine the vehicle access area

  • 2020-05-12 03:00:07
  • OfStack

The core code


#include <iostream>  
#include <vector>  
#include <string>  
#include <algorithm>  
using namespace std;  
struct _AREA_VECTOR_STRUCT 
{ 
  int nAreaType;// Regional types  
  int nAreaID;// area ID 
}; 
 
void CtestDlg::OnBnClickedButton2() 
{ 
  vector<_AREA_VECTOR_STRUCT> structAreaHistory;// On the vehicle 1 Secondary region  
  vector<_AREA_VECTOR_STRUCT> structAreaNow;// The vehicle is in this area  
  vector<_AREA_VECTOR_STRUCT> OutStructAreaVector;// The output area  
 
  _AREA_VECTOR_STRUCT structVehicle; 
 
  // Simulation on 1 The area where the secondary vehicles are located  
  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); 
 
  // Simulate the region where the vehicle is located  
  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); 
 
  // Sort, first by region type, then by region ID The sorting  
  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; 
 
  // Take the difference, and the result is the set out of the region (out of the region)  
  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(" Out of the "); 
  } 
 
  OutStructAreaVector.clear();// Clears out  
 
  // Take the difference in reverse, and the result is the set of newly entered regions (newly entered regions).  
  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(" Back into the "); 
  } 
} 


Related articles: