CPosition



//CPosition.cpp
//
#include
#include
#include
#include
using namespace std;
struct PositionInfo
{
string province;
string city;
string county;
}*pPosition;

using namespace std; //using standard namespace

class CPosition
{
protected:
public:

public:
CPosition();
~CPosition();
PositionInfo GetStruct();
void CpyPosition( CPosition& dest, const CPosition& src );
void InitPosition(vector src);
void AddPosition( string province, string city, string county );
void DelPosition( string province, string city, string county );
vector::iterator FindPosition( const string province, const string city, const string county ) ;
void ModifyPosition( const string srcprovince, const string srccity, const string srccounty, string province, string city, string county);
void Print() const;
vector GetVector() const;
PositionInfo* GetPosition( vector ::iterator iter ) const ;


private:
vector m_vInfo;
};

CPosition::CPosition()
{

}

CPosition::~CPosition()
{
//delete all information of position
for( size_t st = 0; st < m_vInfo.size(); st ++ )
{
DelPosition( m_vInfo[st] -> province, m_vInfo[st] -> city, m_vInfo[st] -> county );
}
m_vInfo.clear();
}

void CPosition::InitPosition(vector src)
{
m_vInfo = src;
}

void CPosition::CpyPosition( CPosition& dest, const CPosition& src )
{
dest = src;
}

void CPosition::AddPosition( string province, string city, string county )
{
if( !province.empty() )
{
PositionInfo* pPosition = new PositionInfo;
pPosition -> province = province;
pPosition -> city = city;
pPosition -> county = county;
m_vInfo.push_back( pPosition );
}
else
{
cout<<"ERROR Position info is input!!"< }
}

void CPosition::DelPosition( string province, string city ,string county )
{
bool bflag = false;
vector::iterator it = m_vInfo.begin();
for( ; it != m_vInfo.end(); it++)

{

if( (*it) -> province == province && (*it) -> city == city && (*it) -> county == county )
{
delete (*it);
m_vInfo.erase( it );
bflag = true;
break;
}
}
if( false == bflag )
{
cout<<"This Position is not exist!!You can't delete it."< }
}

vector::iteratorCPosition::FindPosition( const string province, const string city, const string county )
{
bool bflag = false;
vector::iterator it = m_vInfo.begin();
for( ; it != m_vInfo.end(); it++)
{
if( (*it) -> province == province && (*it) -> city == city && (*it) -> county == county )
{
cout<<"This Position is found."< bflag = true;
break;
}
}
if( false == bflag )
{
cout<<"This Position can't be found!!"< }
return it;
}

void CPosition::Print() const
{
for( size_t st = 0; st < m_vInfo.size(); st++ )
{
cout << "province = " << m_vInfo[st] -> province
<< "/t"
<< "city = " << m_vInfo[st] -> city
<< "/t"
<< "county = " < county
<< endl;
}
}
void CPosition::ModifyPosition( string srcprovince, string srccity, string srccounty, string province, string city, string county)
{
vector::iterator it = m_vInfo.begin();
for( ; it != m_vInfo.end(); it++)
{
if( (*it) -> province == srcprovince && (*it) -> city == srccity && (*it) -> county == srccounty )
{
(*it) -> province = province;
【CPosition】(*it) -> city = city;
(*it) -> county = county;
break;
}
}
}

PositionInfo* CPosition::GetPosition( vector ::iterator iter) const
{
return (*iter);
}

vector CPosition::GetVector() const
{
return this -> m_vInfo;
}
int main(int argc, char* argv[])
{
CPosition p;
cout << "Add Member!" << endl;
//add member
p.AddPosition( "shanghai", "jingan", "sss" );
p.AddPosition( "guangdong", "shenzhen", "nanshan" );
p.AddPosition( "sichuan", "chengdu", "chenghua" );
p.AddPosition( "shanghai", "jingan", "sss" );
p.AddPosition( "shanghai", "jingan", "sss" );

//print out
p.Print();

vector ::iterator iter;
iter = p.FindPosition( "shanghai", "jingan", "sss" );
PositionInfo* pP = p.GetPosition( iter );
cout<< (pP) -> province << (pP) -> county < cout<< (*iter) -> province << (*iter) -> county < p.ModifyPosition( "shanghai", "jingan", "sss", "beijing","chaoyang","xian");
iter = p.FindPosition( "beijing","chaoyang","xian" );
cout << "Delete Member!" << endl;
//delete member
p.DelPosition( "beijing","chaoyang","xian" );
//print out after delete
p.Print();
iter = p.FindPosition( "beijing","chaoyang","xian" );
CPosition q;
q.InitPosition(p.GetVector());
q.Print();
getch();

return 0;
}

    推荐阅读