typedef向量发送到函数以从文件读入向量

时间:2015-11-17 04:37:55

标签: function c++11 vector typedef

该程序与数组运行完美,但我必须将每个数组转换为未分级的向量,一旦我在typedef中为一个向量更改了iarray,程序就会编译,但它会超时说出它运行的时间和返回的值

//Author: Miguel Acosta
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>

//universal variables
const int size=99;
const int col=3;
int studentsize=0;
//user friendly setup
using namespace std;
using std::setw; 
//typedef for arrays
typedef vector<int unsigned> iarray; //ID
typedef short table[size][col]; //quiz scores
typedef float farray[col]; // average
typedef short sarray[col]; //lowest 
typedef float starray[size]; //student average

//functions declaration
void printall(iarray, table, farray, starray, sarray, sarray, int);
void findqavg(table, farray);
void findlow (table, sarray);
void findhigh(table, sarray);
void findstudavg(table, starray );
void getdata(iarray studentid, table score, ifstream &infile);
int main()
{  
    ifstream infile("pr2data.txt");
     
     //variable declaration and initialization
     iarray studentid;
     table score;
     farray average={0};
     starray studavg={0};
     sarray lowest={100};
     sarray highest={0};
     
     //error if file did not open
     if(!infile.is_open()){
     	
		cout << "inInfo.txt could not be accessed!" << endl;

    }
	
     else if(infile.is_open()){
	  
	  		     

        getdata(studentid, score, infile);
     infile.close();
     
    }
     
    //invoking functions
    findqavg(score, average);
    findlow(score, lowest);
    findhigh(score, highest);
    findstudavg(score, studavg);
    
  	printall(studentid, score, average, 
			 studavg, highest, lowest, studentsize);
   

cin.get();
cin.ignore();

return 0;
}//end of main


//read data from file--------------------
void getdata(iarray studentid, table score, ifstream &infile)
{
    
    for(int i = 0; i < size; i++)
    {
        
        infile>>studentid[i];
        
        if(studentid[i]<1)
        {i=100;
		}
        for (int k=0; k<col; k++)
        {
            infile>>score[i][k];
        }
		studentsize++;  
    }
    
    studentsize -=1;
}//end

//find average per quiz
void findqavg(table score, farray avg)
{
	short total=0;
	
	for (int k=0; k<3; k++)
	{
			for(int i=0;i<studentsize; i++)
			{		
			total += score[i][k];

			}
			
		avg[k] = (float)total/(float)studentsize;
		total=0;
		
	}
}//end

//find average per student
void findstudavg(table score, starray studavg)
{
	short total=0;
	
	for (int k=0; k<studentsize; k++)
	{
			for(int i=0;i<col; i++)
			{		
			total += score[k][i];
		
			}
			
		studavg[k] = (float)total/(float)col;
		total=0;
		
	}
}//end

//find lowest grades for each quiz
void findlow(table score, sarray lowest)
{
	
	for (int k=0; k<3; k++)
	{
		for(int i=0;i<studentsize; i++)
		{	
		
			if (score[i][k]<lowest[k])
			{
				lowest[k]=score[i][k];
			}
		
		}	
	}	
}//end

//find highest grades for each quiz
void findhigh(table score, sarray highest)
{
	
	for (int k=0; k<3; k++)
	{
		for(int i=0;i<studentsize; i++)
		{	
		
			if (score[i][k]>highest[k])
			{
				highest[k]=score[i][k];
			}
		
		}	
	}	
}//end

//print out the data in a user friendly manner
void printall(iarray studentid, table score, farray average, 
			  starray studavg, sarray highest, sarray lowest, 
			  int studentsize)
{
	
	cout<<"Student       Quiz 1       Quiz 2       Quiz 3       Average\n\n";

		
		cout << setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
		for (int k=0; k<studentsize; k++)
		{
	cout<<studentid[k]<<setw(13)<<score[k][0]<<setw(13)<<score[k][1]<<
		  setw(13)<<score[k][2]<<setw(15)<<studavg[k]<<endl;	
		}

		
	cout<<"\n\n\nHigh score "<<setw(7)<<highest[0]<<setw(13)<<highest[1]<<
					   setw(13)<<highest[2]<<endl;

	cout<<"\nLow score "<<setw(8)<<lowest[0]<<setw(13)<<lowest[1]<<
					     setw(13)<<lowest[2]<<endl;
						 
						 
	cout<<"\nQuiz average "<<setw(5)<<average[0]<<setw(13)<<average[1]<<
						  setw(13)<<average[2];
	
 }//end 

1 个答案:

答案 0 :(得分:0)

我怀疑问题是当你声明这样的函数时:

void getdata(iarray studentid, ...)
{
    // operations on studentid...
}

当您调用此函数时,实际上将<{em>} studentid变量复制到本地函数范围中,而不是更改原始变量。如果您希望getdata功能修改studentid,则需要通过引用传递它(请注意&amp;)。

 void getdata(iarray &studentid, ...)
 {
     // ...
 }

这意味着您的getdata函数将在引用上运行到studentid,而不是创建studentid的副本,该函数在函数结束时被删除。

数组/指针没有出现这种情况的原因是你有效地将内存地址传递给getdata函数(因为这就是数组/指针)。它仍然复制了变量,但由于它只是指内存地址,它仍然在同一个地方更新数据。由于您现在使用的是vector课程,因此如果您没有指定参考资料,则会复制数据。

您可能遇到的另一个问题是您将数据插入到向量中的方式:您使用的是infile >> studentid[i];,但实际上您并未在任何地方分配它的大小,所以这将是可能会破坏你的程序。相反,我建议使用studentid.resize()或使用push_back明确分配矢量的大小。