【问题标题】:typedef Vector sent into function to read from file into a vectortypedef 向量发送到函数以从文件读取到向量
【发布时间】:2015-11-17 04:37:54
【问题描述】:

该程序可以完美地与数组一起运行,但是我必须将每个数组转换为未调整大小的向量,一旦我更改了 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 

【问题讨论】:

    标签: function c++11 vector typedef


    【解决方案1】:

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

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

    当您调用此函数时,这实际上将studentid 变量复制到本地函数范围内,而不是更改原始变量。如果您希望 getdata 函数修改 studentid,则需要通过引用传递它(注意 &)。

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

    这意味着您的 getdata 函数将对studentid的reference进行操作,而不是创建studentid的副本,该副本会在函数结束时被删除。

    数组/指针没有发生这种情况的原因是您有效地将内存地址传递给getdata 函数(因为这就是数组/指针)。它仍然复制了变量,但由于它只是引用内存地址,它仍然在同一个地方更新数据。由于您现在使用的是vector 类,如果您不指定引用,它将复制数据。

    您可能会遇到的另一个问题是将数据插入向量的方式:您使用的是infile &gt;&gt; studentid[i];,但实际上并未在任何地方分配它的大小,因此这可能会使您的程序崩溃。相反,我建议使用studentid.resize() 或使用push_back 显式分配向量的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多