STL容器使用中的拷贝成本

来源:互联网 发布:sql union limit 编辑:程序博客网 时间:2024/06/03 02:10

 

 

#include "stdafx.h"

#include <map>

#include <vector>

#include <list>

 

 

using namespace std;

 

//User Defined Object:

 

class CustomerObj

{

public:

 

CustomerObj():m_iObjValue(0)

{

printf("Object (0)->Default Construct!/n");

}

 

CustomerObj( int iValue ):m_iObjValue(iValue)

{

printf("Object (%d)->User define Construct!/n", m_iObjValue );

}

 

~CustomerObj()

{

printf( "Object (%d)->Destruct/n", m_iObjValue );

}

 

CustomerObj(const CustomerObj& obj )

{

m_iObjValue  = obj.m_iObjValue;

printf( "Obect (%d)->Copy Construct!/n", m_iObjValue );

}

 

CustomerObj& operator=(const CustomerObj& obj)

{

m_iObjValue = obj.m_iObjValue;

printf( "Object (%d)->Copy assignment/n", m_iObjValue );

 

return *this;

}

 

void SetObjValue( int iValue )

{

m_iObjValue = iValue;

}

 

int GetObjValue()

{

return m_iObjValue;

}

private:

int m_iObjValue;

};

 

 

//A function template to test the sequence container

 

 

template< class T >

void SEQ_Copy_Cost_Test( T TestContainer ) 

{

CustomerObj myObject;

 

printf( "push_back:(%d).>>>>>>>>/n", myObject.GetObjValue() );

TestContainer.push_back( myObject );

 

myObject.SetObjValue( 1 );

 

printf( "push_back:(%d).>>>>>>>>/n", myObject.GetObjValue() );

TestContainer.push_back( myObject );

myObject.SetObjValue( 2 );

printf( "push_back:(%d).>>>>>>>>/n", myObject.GetObjValue() );

TestContainer.push_back( myObject );

 

myObject.SetObjValue( 3 );

printf( "push_back:(%d).>>>>>>>>/n", myObject.GetObjValue() );

TestContainer.push_back( myObject );

myObject.SetObjValue( 4 );

printf( "push_back:(%d).>>>>>>>>/n", myObject.GetObjValue() );

TestContainer.push_back( myObject );

 

myObject.SetObjValue( 5 );

printf("insert:(%d) at begining.>>>>>>>>/n", myObject.GetObjValue() );

TestContainer.insert( TestContainer.begin(), myObject );

 

printf("pop_back.>>>>>>>>/n");

TestContainer.pop_back();

}

 

 

 

 

int _tmain(int argc, _TCHAR* argv[])

{

 

 

 

//Vector Container

printf("Vector Container:>>>>>>>/n");

vector< CustomerObj > VectorContainer;

SEQ_Copy_Cost_Test( VectorContainer );

 

//List Container

printf("List Container:>>>>>>>/n");

list< CustomerObj > ListContainer;

SEQ_Copy_Cost_Test( ListContainer );

 

 

 

}

 

 

原创粉丝点击