C ++在向量中使用向量

时间:2015-03-24 20:06:27

标签: c++ c++11 visual-c++ vector stdvector

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

我得到的错误是

  

OOP project.exe中0x769E4598处的未处理异常:Microsoft C ++异常:内存位置0x00CAF490处的std :: out_of_range。

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

2 个答案:

答案 0 :(得分:3)

你遇到了“out或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. 不要访问!
  2. 避免访问很简单,测试向量是否为空。

    if ( connectedMatches[1].empty() )
    {
        std::cerr << "No element to display!\n";
    }
    else
    {
        std::cout << connectedMatches[1].at(0);
    }
    
    1. 抓住例外:
    2. 这首先尝试执行代码,只有在发生错误时才会处理错误。

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

答案 1 :(得分: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
  //...
  //...

}

在这种情况下,您不需要返回任何内容,只需在函数调用之前创建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"