//using the modual file
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
using namespace std;//using standard namespace
class CPerson
{
public:
CPerson();
~CPerson();
void Add( char * pszName, int nWage );
void Delete( char* pszName );
void Print();
protected:
struct PersonInfo
{
string strName;
int nWage;
};
private:
vector<PersonInfo*> m_vInfo;
};
CPerson::CPerson()
{
}
CPerson::~CPerson()
{
//delete all persons' information
for( size_t st = 0; st < m_vInfo.size(); st ++ )
{
Delete( (char*)m_vInfo[st] -> strName.data() );
}
m_vInfo.clear();
}
void CPerson::Add( char* pszName, int nWage )
{
if( pszName )
{
CPerson::PersonInfo* pPerson = new CPerson::PersonInfo;
pPerson -> strName = pszName;
pPerson -> nWage = nWage;
m_vInfo.push_back( pPerson );
}
else
{
cout<<"ERROR Name is input!!"<<endl;
}
}
void CPerson::Delete( char* pszName )
{
vector<PersonInfo*>::iterator it = m_vInfo.begin();
for( ; it != m_vInfo.end(); it++)
//for( size_t st = 0; st < m_vInfo.size(); st++ )
{
//if( m_vInfo[st] -> strName == pszName )
if( (*it) -> strName == pszName)
{
//delete m_vInfo[st];// this method doesnot delete the personinfo, just clear the name
delete (*it);
m_vInfo.erase( it );
break;
}
}
}
void CPerson::Print()
{
for( size_t st = 0; st < m_vInfo.size(); st++ )
{
cout << "Name = " << m_vInfo[st] -> strName
<< "/t"
<< "Wage = " << m_vInfo[st] -> nWage
<< endl;
}
}
int main(int argc, char* argv[])
{
CPerson p;
cout << "Add Member!" << endl;
//add member
p.Add( "Jackie", 2000 );
p.Add( "Paul", 3000 );
p.Add( "Hery", 3500 );
//print out
p.Print();
cout << "Delete Member!" << endl;
//delete member
p.Delete( "Paul" );
//print out after delete
p.Print();
getch();
return 0;
}
//delete m_vInfo[st]; 这样为什么没有删除呢