Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string>
  2. #include <iostream>
  3. #include <vector>
  4. #include <random>
  5.  
  6. // input the standard namespace
  7. using namespace std;
  8.  
  9. // tester function declaration
  10. void tester()
  11. {
  12.     // the hardcoded route addresses...
  13.     // If you had a package for every address, this is the optimized order you'd want to go.
  14.     vector<string> route_addresses;
  15.     for (int i = 208; i < 300; ++i)  // initialize the hardcoded relay addresss
  16.     {
  17.         string tmp_string_address = to_string(i) + " St. David drive";
  18.         route_addresses.push_back(tmp_string_address);
  19.     }
  20.  
  21.     // here get user input, a set of addresses of packages
  22.     // assume that the user input's address exists in the route address.
  23.     // input "stop" to stop getting user input.
  24.     vector<int> indexes_into_route_addresses;
  25.     cout << "Input your addresses, input \"stop\" to stop" << endl;
  26.     bool do_again = true;  // the following do-while loop continues until this is false
  27.     do
  28.     {
  29.         string user_input;
  30.         getline(cin, user_input);
  31.         if (user_input == "stop") // if the user inputs "stop" then stop the input... and exit the loop
  32.             do_again = false;
  33.         else  // otherwise, get the index of the current user_input address into the relay_addresses and push it into the vector.
  34.         {
  35.             // To find the index into the route_addresses vector, we loop through the entirety
  36.             // of the input_addresses vector until the user_input matches an element
  37.             // If we have a match, we have its index. This is the thing that'll need to be sorted later.
  38.             for (int route_addr_idx = 0; route_addr_idx < route_addresses.size(); ++route_addr_idx)
  39.             {
  40.                 if (route_addresses[route_addr_idx] == user_input)
  41.                     indexes_into_route_addresses.push_back(route_addr_idx);
  42.             }
  43.         }
  44.     } while (do_again);
  45.  
  46.     // Once here, we can assume that the user has inputted all of the addresses
  47.     // And that the indexes to relay addresses is filled too.
  48.    
  49.     // sort the indexes using the standard sorting function.
  50.     // It simply sorts the integers. We could implement our own sort function if we wanted, this is just here for convenience (google mergesort).
  51.     sort(indexes_into_route_addresses.begin(), indexes_into_route_addresses.end());
  52.  
  53.     // We have sorted the indexes. Now we can index into the relay_addresses to get the actual string back.
  54.     cout << "The re-oredered set is: " << endl;
  55.     for (int i = 0; i < indexes_into_route_addresses.size(); ++i)
  56.         cout << "stop " + to_string(i) + ": " + route_addresses[indexes_into_route_addresses[i]] << endl;
  57.  
  58.  
  59. }
  60.  
  61. // notice we don't call the main function anywhere, because it's called by the OS when we start the program via the GUI
  62. int main(const int argc, char **argv) {
  63.     tester();  // call the tester function, no arguments
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement