Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <vector>
- #include <random>
- // input the standard namespace
- using namespace std;
- // tester function declaration
- void tester()
- {
- // the hardcoded route addresses...
- // If you had a package for every address, this is the optimized order you'd want to go.
- vector<string> route_addresses;
- for (int i = 208; i < 300; ++i) // initialize the hardcoded relay addresss
- {
- string tmp_string_address = to_string(i) + " St. David drive";
- route_addresses.push_back(tmp_string_address);
- }
- // here get user input, a set of addresses of packages
- // assume that the user input's address exists in the route address.
- // input "stop" to stop getting user input.
- vector<int> indexes_into_route_addresses;
- cout << "Input your addresses, input \"stop\" to stop" << endl;
- bool do_again = true; // the following do-while loop continues until this is false
- do
- {
- string user_input;
- getline(cin, user_input);
- if (user_input == "stop") // if the user inputs "stop" then stop the input... and exit the loop
- do_again = false;
- else // otherwise, get the index of the current user_input address into the relay_addresses and push it into the vector.
- {
- // To find the index into the route_addresses vector, we loop through the entirety
- // of the input_addresses vector until the user_input matches an element
- // If we have a match, we have its index. This is the thing that'll need to be sorted later.
- for (int route_addr_idx = 0; route_addr_idx < route_addresses.size(); ++route_addr_idx)
- {
- if (route_addresses[route_addr_idx] == user_input)
- indexes_into_route_addresses.push_back(route_addr_idx);
- }
- }
- } while (do_again);
- // Once here, we can assume that the user has inputted all of the addresses
- // And that the indexes to relay addresses is filled too.
- // sort the indexes using the standard sorting function.
- // It simply sorts the integers. We could implement our own sort function if we wanted, this is just here for convenience (google mergesort).
- sort(indexes_into_route_addresses.begin(), indexes_into_route_addresses.end());
- // We have sorted the indexes. Now we can index into the relay_addresses to get the actual string back.
- cout << "The re-oredered set is: " << endl;
- for (int i = 0; i < indexes_into_route_addresses.size(); ++i)
- cout << "stop " + to_string(i) + ": " + route_addresses[indexes_into_route_addresses[i]] << endl;
- }
- // notice we don't call the main function anywhere, because it's called by the OS when we start the program via the GUI
- int main(const int argc, char **argv) {
- tester(); // call the tester function, no arguments
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement