?? stuinfovec.cpp
字號(hào):
//StuInfoVec.cpp
#include"StuInfoVec.h"
StuInfoVec::~StuInfoVec()
{
recVec.clear();
}
/***********************************
功能:添加新的記錄
*參數(shù):rec,指向新的指針
**************************************/
void StuInfoVec::addRecord(Record* rec)
{
if(rec != NULL)
{
recVec.push_back(rec);
}
}
/**************************
*功能:從數(shù)組中刪除做了“待刪除”標(biāo)記的記錄
*返回:實(shí)際上刪除了的記錄數(shù)
***********************************/
int StuInfoVec::removePerform()
{
int deletedNum=0;
//為了獲得更高的效率,從數(shù)組的末尾開(kāi)始查找做了標(biāo)記的記錄并將其刪除
Iterator it,it2,it_end;
Iterator it_beforeBegin=recVec.begin()-1;
int inx; //被刪除記錄的名次
for (it=recVec.end()-1; it != it_beforeBegin; it--)
{
if((*it)->toBeDeleted)
{
inx=(*it)->index;
it=recVec.erase(it);
deletedNum++;
// 更新剩下的所有記錄的名次
//將名次排在被刪除記錄后面的記錄的名次減1
it_end=recVec.end();
for(it2=recVec.begin(); it2!=it_end; it2++)
{
if((*it2)->index > inx)
(*it2)->index--;
}
}
}
return deletedNum;
}
/***************************************************************
*功能:從記錄信息中尋找一個(gè)記錄
*參數(shù):pattern,欲尋找的記錄的指定域與pattern相等。
* type,為0表示欲尋找記錄的學(xué)號(hào)(number)與pattern相等;
* 為1表示欲尋找記錄的姓名(name)與pattern相等;
* 為2表示欲尋找記錄的名詞(index)與pattern相等,
* form,從porm開(kāi)始尋找匹配的記錄。
*返回:若找到了則返回指向第一個(gè)匹配記錄的迭代器,
* 若找不到則返回的迭代器等于pastEnd()的返回值。
*注意:from應(yīng)是可提領(lǐng)的迭代器。
* 第一次調(diào)用可以用first(),之后使用上一次findRecord()的返回值增1,
* 直到返回pastEnd(),便可以獲得所有匹配的記錄
*****************************************************************/
StuInfoVec::Iterator StuInfoVec::findRecord(
const string& pattern,int type,StuInfoVec::Iterator from)
{
Iterator it;
Iterator it_end = recVec.end();
for (it=from; it != it_end; it++)
{
if((type==0 && (*it)->number==pattern) ||
(type==1 && (*it)->name==pattern)||
(type==2 && (*it)->index==atoi(pattern.c_str())))//將字符型轉(zhuǎn)換為整數(shù)型
//string.c_str()返回的是一個(gè)
//char*
break;
}
return it;
}
/***********************************
*功能:將信息記錄數(shù)組保存到輸出流中
*參數(shù):os,指定的輸出流
***********************************/
void StuInfoVec::saveRecords(ostream &os)
{
os << recVec;
}
/**************************************
*功能:從輸入流中讀入數(shù)據(jù)并追加到當(dāng)前記錄的末尾
*參數(shù):is,指定的輸入流
*返回:讀入的記錄數(shù)
*******************************************/
int StuInfoVec::loadRecords(istream &is)
{
int ori = size();
is >> recVec;
int addedCount = size()-ori;
//如果是在原有記錄的基礎(chǔ)上追加新的記錄,則更新所有記錄的名詞項(xiàng)
if (ori !=0)
{
int i,j;
int num =size();
int ncount; // 記錄總分大于當(dāng)前記錄的人數(shù)
float sumi;
for (i=0; i<num; i++)
{
ncount = 0;
sumi = recVec[i]->sum;
for (j=0; j<num; j++)
{
if (recVec[j]->sum > sumi )
ncount++;
}
recVec[i]->index = ncount+1;
}
}
return addedCount;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -