Advertisement
Guest User

tree

a guest
Apr 26th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <string>
  2. #include "tree.hh"
  3.  
  4. #include <iostream>
  5. #include <array>
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.  
  10.  
  11.     //Init the database
  12.     std::string zones[10] = {"A",
  13.                              "A",
  14.                              "B",
  15.                              "C",
  16.                              "B",
  17.                              "D",
  18.                              "D",
  19.                              "E",
  20.                              "E",
  21.                              "E"};
  22.  
  23.     std::string subZones[10] = {"A.1",
  24.                                 "A.1",
  25.                                 "B.1",
  26.                                 "C.1",
  27.                                 "B.2",
  28.                                 "D.1",
  29.                                 "D.2",
  30.                                 "E.1",
  31.                                 "E.1",
  32.                                 "E.2"};
  33.  
  34.     //Prepare the strings for the categories
  35.     std::string tempZone = "";
  36.     std::string tempSubZone = "";
  37.  
  38.     //Prepare the tree
  39.     tree<std::string> bodyTree;
  40.     tree<std::string>::iterator zoneIt, subZoneIt, topIt;
  41.     topIt = bodyTree.begin();
  42.  
  43.     //Loop the entire database
  44.     for(int i=0; i<10; i++){
  45.  
  46.         //Grab the data
  47.         tempZone = zones[i];
  48.         tempSubZone = subZones[i];
  49.  
  50.         //Check if we have that zone already
  51.         zoneIt=find(bodyTree.begin(), bodyTree.end(), tempZone);
  52.  
  53.         //If we don't have the zone, add it to the tree
  54.         if(zoneIt==bodyTree.end()){
  55.  
  56.             bodyTree.insert(topIt, tempZone);
  57.  
  58.             std::cout << "Added new Zone: "<< tempZone << "\n";
  59.  
  60.         }
  61.         zoneIt=find(bodyTree.begin(), bodyTree.end(), tempZone);
  62.  
  63.         //Now we have the zone for sure, we do the same with the subZone
  64.  
  65.         //Check if we have that subzone already
  66.         tree<std::string>::iterator subZoneIt2 =find(bodyTree.begin(), bodyTree.end(), tempSubZone);
  67.         subZoneIt=find(bodyTree.begin(zoneIt), bodyTree.end(zoneIt), tempSubZone);
  68.        
  69.         //If the subZone doesn't exist, add it to the zone
  70.         if(subZoneIt==bodyTree.end(zoneIt)){
  71.  
  72.             bodyTree.append_child(zoneIt, tempSubZone);
  73.  
  74.             std::cout << "Added new subZone "<< tempSubZone << " --> to --> " << tempZone << "\n";
  75.  
  76.         }
  77.  
  78.     }
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement