Stupid question: The below is O(n^3). I'm sure there is a way to improve this, so any help is appreciated.
//vector<string> flight_path; given as parameter
//vector<int> flight_number; need to fill
//vector<segment> flight_segments; known, contains segments (dep, arr, flightnum)
//Purpose of the function is to take in a flight_path and give back a list of
// flight numbers for each segment
//
// Input: New York, London, Kolkata
// Output: 101 201 301, 102 939
vector<string> get_flight_numbers (vector<string> flight_path) {
vector<int> flight_number;
for (int i = 0; i < flight_path.size() - 1; i++) {
string dep = flight_path.at(i);
string arr = flight_path.at(i+1);
for (int j = 0; j < flight_segments.size(); j ++) {
if (flight_segments.at(j).departure == dep && flight_segments.at(j).arrival == arr) {
for (int k = 0; k < flight_segments.at(j).segment_numbers.size(); k++) {
flight_numbers.push_back(atoi(flight_segments.at(j).segment_numbers.at(k).c_str()));
}
}
}
}
}
if()tests if the flight covers the current path segment. – David Harkness Feb 17 '11 at 3:20