我遇到了一个奇怪的问题。我试图用一个函数填充一个向量,然后用另一个函数输出它。即使在调用该函数后,该向量仍显示为空,并且我相信它正在正确填充它。到目前为止,这是我的代码。结果是
"输入输入文件名 in.txt 在抛出 'std::out_of_range' what() 实例后调用向量中的搜索终止:vector::_M_range_check Aborted (core dumped)"
#include <iostream>
#include <cassert>
#include<vector>
#include "ArrayUtilities.h"
using namespace std;
void fillArray(istream& io, std::vector<int> a, int& actualSize)
{
int temp,i=0;
io>>temp;
while (temp!=-999)
{
a.push_back(temp);
io>>temp;
i++;
}
actualSize=i;
}
void outputArray(ostream& os,
std::vector<int> a,
const int actualSize,
const string& info )
{
os<<info<<"\n";
for (int w=0;w<actualSize;++w)
{
os<< a.at(w)<<" ";
}
os<<"\n";
}
int main() {
int actualSize;
string fileName;
string info;
std::vector<int> a;
cout << "Enter the input file name ";
cin >> fileName;
ifstream inData;
ofstream outData;
inData.open(fileName.c_str());
outData.open("out.txt");
int searchItem;
inData>>info;
inData>>searchItem;
fillArray (inData, a, actualSize);
cout <<"Search in vector\n";
outData<<"Search in vector\n";
outputArray(outData, a, actualSize, info);
outputArray(cout, a, actualSize, info);
//sequential search
int position;
position=seqSearch(a,actualSize,searchItem);
if (position > 0)
{
cout << "Sequential Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< "\n";
outData << "Sequential Search: SearchItem "
<< searchItem
<< " is on position "
<< position
<< "\n";
}
else
{
cout << "Sequential Search: SearchItem "
<< searchItem
<< " is not in the list\n";
outData << "Sequential Search: SearchItem "
<< searchItem
<< " is not in the list\n";
}
感谢您的帮助