mardi 24 mars 2015

C++ Using vectors within vectors

I have a function that needs to return two vectors so I have been using a vector within a vector to return them right now I have unsuccessfully tried doing:



vector<vector<string>> mainVector;
vector<string> vector1;
vector<string> vector2:

mainVector.reserve(2);
mainVector.push_back(vector1);
mainVector.push_back(vector2);

return mainVector;


so my question is how do you do I add a vector to another vector? this is my entire code:



vector < vector < string >> connectedJourney(string airpCode1, string airpCode2, vector < string > flights) {
vector < vector < string >> rawMatches;
vector < string > deptMatchesTemp;
vector < string > destMatchesTemp;
vector < string > deptMatches;
for (unsigned int f1 = 0; f1 < flights.size(); f1++) { //store all the fligths that match the departure airport into deptMatches

if (airpCode1 == flights[f1].substr(0, 3)) {

deptMatches.push_back(flights[f1]);
}
}

vector < string > destMatches;

for (unsigned int f2 = 0; f2 < flights.size(); f2++) { //store all the fligths that match the departure airport into deptMatches

string code = flights[f2];

if (code.length() > 7 && airpCode2 == flights[f2].substr(4, 3)) {

destMatches.push_back(flights[f2]);
}
}

if (deptMatches.size() == 0 || destMatches.size() == 0) { // check if there won't be any matches
cout << "no entries";
throw noEntryFound();

} else {
vector < string > cj_Matches; //connected journey matches
for (unsigned int g1 = 0; g1 < deptMatches.size() - 1; g1++) {
cout << deptMatches.at(0);
for (unsigned int g2 = 0; g2 < destMatches.size() - 1; g2++) {
cout << deptMatches.at(1);
if (deptMatches[g1].substr(4, 3) == destMatches[g2].substr(0, 3)) { //if the arrival place of the first flight matches the departure place of the first flight then the details of both flights are saved into a vector within another
deptMatchesTemp.push_back(deptMatches[g1]);
destMatchesTemp.push_back(deptMatches[g2]);
}
}
}
rawMatches.reserve(2);
rawMatches.push_back(deptMatchesTemp);
rawMatches.push_back(destMatchesTemp);
return rawMatches;
}

}

Aucun commentaire:

Enregistrer un commentaire