概述
#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 );
}
最后
以上就是和谐香水为你收集整理的STL容器使用中的拷贝成本的全部内容,希望文章能够帮你解决STL容器使用中的拷贝成本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复