CPosition

来源:互联网 发布:如何投诉淘宝店铺 编辑:程序博客网 时间:2024/03/28 19:08

 

//CPosition.cpp

//

#include <iostream>

#include <string>

#include <vector>

#include <conio.h>

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<PositionInfo*> src);

void AddPosition( string province, string city, string county );

void DelPosition( string province, string city, string county );

vector<PositionInfo*>::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<PositionInfo*> GetVector() const;

PositionInfo* GetPosition( vector<PositionInfo*> ::iterator iter ) const ;

 

 

private:

vector<PositionInfo*> 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<PositionInfo*> 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!!"<<endl;

    }

}

 

void CPosition::DelPosition( string province, string city ,string county )

{

    bool bflag = false;

    vector<PositionInfo*>::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."<<endl;

    }

}

 

 vector<PositionInfo*>::iterator  CPosition::FindPosition( const string province, const string city, const string county ) 

{

    bool bflag = false;

    vector<PositionInfo*>::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."<<endl;

            bflag = true;

            break;

        }

    }

    if( false == bflag )

    {

        cout<<"This Position can't be found!!"<<endl;

    }

    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 = " <<m_vInfo[st] -> county

             << endl;

    }

}

void CPosition::ModifyPosition( string srcprovince, string srccity, string srccounty, string province, string city, string county)

{

    vector<PositionInfo*>::iterator it = m_vInfo.begin();

    for( ; it != m_vInfo.end(); it++)

    {

        if( (*it) -> province == srcprovince && (*it) -> city == srccity && (*it) -> county == srccounty )

        {

            (*it) -> province = province;

            (*it) -> city = city;

            (*it) -> county = county; 

            break;

        }

    }

}

 

PositionInfo* CPosition::GetPosition( vector<PositionInfo*> ::iterator iter) const

{

    return (*iter);

}

 

vector<PositionInfo*> 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<PositionInfo*> ::iterator iter;

    iter = p.FindPosition( "shanghai", "jingan", "sss" );

    PositionInfo* pP = p.GetPosition( iter );

    cout  << (pP) -> province << (pP) -> county <<endl;

    cout  << (*iter) -> province << (*iter) -> county <<endl;

    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;

}

原创粉丝点击