在我的C ++程序中,我有一个文本文件,我逐行读到一个名为vector
的{{1}},然后我在这个字符串向量中搜索一些字符串,但不幸的是,如果我不这样做找到任何匹配项我收到以下错误
航班矢量具有以下格式:
flights
OOP project.exe中0x769E4598处的未处理异常:Microsoft C ++异常:内存位置0x0052F0F0处的std :: out_of_range。
我想我发现错误必须在我EMA CDG BritishAirways 120 100
CDG VIE AirFrance 120 100
EMA VIE BritishAirways 150 300
EMA CDG AirFance 130 80
GRO FFF Rayanair 130 80
FFF HHH AirItalia 100 50
功能的某处:
connectedJourney
如果它有用,我还在这里上传了我的整个项目: https://drive.google.com/folderview?id=0B-VbnRtajCWIfmFxMk5UUncwSkNzNm8tT2xrU0hDM29kbzg4TUFKODJSUExMTV9oVDFncjA&usp=sharing
答案 0 :(得分:1)
试试这个:
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)) { //the call stack says the error is at this line
destMatches.push_back(flights[f2]);
}
}
这就是我要做的事情:
vector<string>::iterator iter = flights.begin();
for (; iter!=flights.end(); ++iter) {
istringstream is(*iter);
string tokens[5];
for (unsigned int i=0; i<5; ++i) {
is >> tokens[i];
}
if (tokens[0] == airpCode1) {
deptMatches.push_back(*iter);
}
if (tokens[1] == airpCode2) {
destMatches.push_back(*iter);
}
}
答案 1 :(得分:0)
你应该查看“传递闭包”和Warshall的算法。我认为下面的代码可以帮助您完成您的工作。
#include <string>
#include <iostream>
#include <set>
#include <vector>
#include <sstream>
#include <stdexcept>
// if you prefer to 'weight' your search on time
// uncomment time and comment price
#define PRICE
//#define TIME
using namespace std;
static const size_t NUMFLIGHTS=6;
static const char* data[] {
"EMA CDG BritishAirways 120 100",
"CDG VIE AirFrance 120 100",
"EMA VIE BritishAirways 150 300",
"EMA CDG AirFance 130 80",
"GRO FFF Rayanair 130 80",
"FFF HHH AirItalia 100 50"};
struct flight {
void set(const string& input) {
istringstream is(input);
is >> dept >> dest >> airways >> time >> price;
}
string dept;
string dest;
string airways;
unsigned int time;
unsigned int price;
};
struct travelData {
travelData() : weight(0), infinity(true) {}
vector<flight> flights;
unsigned int weight;
bool infinity;
void print() {
auto i=flights.begin();
for (;i!=flights.end(); ++i) {
cout << i->dept << " " << i->dest << endl;
}
cout << weight << endl;
}
};
struct database {
vector< vector<travelData> > allflights;
flight flights[NUMFLIGHTS];
vector<string> locations;
};
travelData FlightExists(const flight* flights, const string& dept, const string& dest) {
travelData ret;
for (size_t i=0; i<NUMFLIGHTS; ++i) {
if (flights[i].dest == dest && flights[i].dept == dept) {
ret.flights.push_back(flights[i]);
#ifdef PRICE
ret.weight = flights[i].price;
#elif LENGTH
ret.weight = flights[i].time;
#else
ret.weight = 1;
#endif
ret.infinity= false;
return ret;
}
}
return ret;
}
database setup()
{
database ret;
set<string> locationsSet;
for (size_t i=0; i<NUMFLIGHTS; ++i) {
ret.flights[i].set(data[i]);
locationsSet.insert(ret.flights[i].dept);
locationsSet.insert(ret.flights[i].dest);
}
copy(locationsSet.begin(), locationsSet.end(), back_inserter(ret.locations));
size_t len = ret.locations.size();
for (size_t i=0; i<len; ++i) {
string searchDept = ret.locations[i];
vector<travelData> row;
for (size_t j=0; j<len; ++j) {
string searchDest = ret.locations[j];
if (i == j) {
travelData blank;
blank.infinity = false;
row.push_back(blank);
} else {
row.push_back(FlightExists(ret.flights, searchDept, searchDest));
}
}
ret.allflights.push_back(row);
}
// Warshalls algorithn
for (size_t k=0; k<len; ++k) {
for (size_t i=0; i<len; ++i) {
for (size_t j=0; j<len; ++j) {
travelData& d = ret.allflights[i][j];
travelData& s1 = ret.allflights[i][k];
travelData& s2 = ret.allflights[k][j];
if (!s1.infinity && !s2.infinity) {
int sum = s1.weight + s2.weight;
if (d.infinity) {
d.infinity = false;
d.flights.insert(d.flights.begin(),
s1.flights.begin(),
s1.flights.end());
d.flights.insert(d.flights.begin(),
s2.flights.begin(),
s2.flights.end());
d.weight = sum;
} else if (d.weight > sum) {
d.flights.clear();
d.flights.insert(d.flights.begin(),
s1.flights.begin(),
s1.flights.end());
d.flights.insert(d.flights.begin(),
s2.flights.begin(),
s2.flights.end());
d.weight = sum;
}
}
}
}
}
return ret;
}
size_t GetIndex(const database& db, const string search) {
size_t len = db.locations.size();
for (size_t i=0; i<len; ++i) {
if (db.locations[i] == search) {
return i;
}
}
throw runtime_error("bogus search");
}
travelData GetResults(const database& db, const string& dept, const string& dest) {
travelData ret;
size_t i_index = GetIndex(db, dept);
size_t j_index = GetIndex(db, dest);
return db.allflights[i_index][j_index];
}
int main() {
database db = setup();
travelData result = GetResults(db, "GRO", "HHH");
result.print();
return 0;
}