C++ 在向量中使用向量

C++ Using vectors within vectors

我有一个函数需要 return 两个向量,所以我一直在一个向量中使用一个向量来 return 它们,现在我试过没有成功:

我得到的错误是

Unhandled exception at at 0x769E4598 in OOP project.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00CAF490.

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

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

return mainVector;

所以我的问题是如何将一个向量添加到另一个向量? 这是我的全部代码:

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;
  }

}

//I try to view the contents like this and I get an out or range error
vector < vector < string >> connectedMatches = connectedJourney(airpCode1, airpCode2, rawFlights);

cout << connectedMatches[1].at(0);

另一种解决方案是使用 vector<string> 引用作为参数:

void connectedJourney(string airpCode1, string airpCode2, vector < string > flights, vector < string >& outputDeparture, vector < string >& outputDestination) {
  // Do your stuff using reference parameter instead of temp vector
  //...
  //...

}

在这种情况下,你不需要 return 任何东西,你只需要在函数调用之前创建你的 2 vector<string> 并将它们传递给函数(我的例子中的最后两个参数).

vector<string> myDeparture;
vector<string> myDestination;
connectedJourney(airpCode1, airpCode2, rawFlights, myDeparture, myDestination);

if (!myDeparture.empty()) cout << myDeparture.at(0);
else cout<<"Your departure vector is empty"

if (!myDestination.empty()) cout << myDestination.at(0);
else cout<<"Your destination vector is empty"

你运行变成了"out or range error"(实际上,这是一个异常,是一种特殊的错误),因为向量可能是空的:

cout << connectedMatches[1].at(0);
                         ^  ^
                         |  |
                         |  Access to first element, if available, 
                         |  otherwise exception will be thrown.
                         |
                         |
                         Unsafe access to the second element (only
                         recommended if access must be fast and you're
                         certain that you're allowed to access)

有两种方法可以直接访问向量的数据:下标运算符operator[]at方法。如果提供的索引有效,结果将相同。然而,如果索引超出范围,行为会有所不同:operator[] 将不保证会出错(但它可能会导致崩溃或看似工作正常,非常危险!)。 at 保证出现异常,告诉您索引无效。

所以您遇到了异常情况。有两种解决方法:

  1. 不要访问!

避免访问简单,测试vector是否为空

if ( connectedMatches[1].empty() )
{
    std::cerr << "No element to display!\n";
}
else
{
    std::cout << connectedMatches[1].at(0);
}
  1. 捕获异常:

这首先尝试执行代码,并且只有在发生错误时才会处理错误。

try
{
    std::cout << connectedMatches[1].at(0);
}
catch ( const std::exception& e )
{
    std::cerr << e.what() << '\n';
}