是否有引用计数std :: vector的实现? [英] Is there a reference counted implementation of std::vector?

查看:115
本文介绍了是否有引用计数std :: vector的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,std :: vector不使用引用计数来避免复制和初始化的开销。我在哪里可以获得参考

计算std :: vector的实现?谢谢。

To my understanding, std::vector does not use reference counting to avoid
the overhead of copying and initialisation. Where can I get a reference
counted implementation of std::vector? Thanks.

推荐答案

Jason Heyes写道:
Jason Heyes wrote:
据我所知,std :: vector不使用引用计数来避免
复制和初始化的开销。我在哪里可以获得参考
计算std :: vector的实现?谢谢。
To my understanding, std::vector does not use reference counting to avoid
the overhead of copying and initialisation. Where can I get a reference
counted implementation of std::vector? Thanks.




您可能想查看boost :: shared_ptr。


或者,你可以做这样的事情:


#include< vector>


模板< typename T>

class vector_reference_counted

:public std :: vector< T>

{


int m_reference_count;


public:


inline vector_reference_counted()

:m_reference_count(1)

{

}


内联vector_reference_counted(const vector_reference_counted&

i_rhs)

:m_reference_count(1),

std :: vector< T>(i_rhs)

{

}


inline vector_reference_counted& operator =(const

vector_reference_counted& i_rhs)

{

//引用计数在分配时不会改变

* static_cast<的std ::矢量< T> *>(this)= i_rhs;


返回* this;

}


inline vector_reference_counted& operator =(const std :: vector< T>

& i_rhs)

{

//引用计数在分配时不会改变


* static_cast<的std ::矢量< T> *>(this)= i_rhs;


返回* this;

}


int AddRef()

{

int l_count = ++ m_reference_count;

返回l_count;

}


int发布()

{

int l_count = - m_reference_count;


if(l_count = = 0)

{

删除此内容;

}


返回l_count;

}


};



You might want to check out boost::shared_ptr.

Or, you could do somthing like this :

#include <vector>

template <typename T>
class vector_reference_counted
: public std::vector<T>
{

int m_reference_count;

public:

inline vector_reference_counted()
: m_reference_count( 1 )
{
}

inline vector_reference_counted( const vector_reference_counted &
i_rhs )
: m_reference_count( 1 ),
std::vector<T>( i_rhs )
{
}

inline vector_reference_counted & operator = ( const
vector_reference_counted & i_rhs )
{
// reference count does not change upon assignment

* static_cast< std::vector<T> * >( this ) = i_rhs;

return * this;
}

inline vector_reference_counted & operator = ( const std::vector<T>
& i_rhs )
{
// reference count does not change upon assignment

* static_cast< std::vector<T> * >( this ) = i_rhs;

return * this;
}

int AddRef()
{
int l_count = ++ m_reference_count;
return l_count;
}

int Release()
{
int l_count = -- m_reference_count;

if ( l_count == 0 )
{
delete this;
}

return l_count;
}

};


" Gianni Mariani" < GI ******* @ mariani.ws>在消息中写道

news:br ******** @ dispatch.concentric.net ...
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:br********@dispatch.concentric.net...
Jason Heyes写道:
Jason Heyes wrote:
据我了解,std :: vector不使用引用计数到
避免复制和初始化的开销。我在哪里可以获得参考
计算std :: vector的实现?谢谢。
你可能想看一下boost :: shared_ptr。
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference
counted implementation of std::vector? Thanks.
You might want to check out boost::shared_ptr.




为什么?我知道shared_ptr。它是如何帮助的?

或者,你可以做这样的事情:

#include< vector>

模板< typename T>
类vector_reference_counted
:public std :: vector< T>

int m_reference_count;

公开:

内联vector_reference_counted()
:m_reference_count(1)


内联vector_reference_counted(const vector_reference_counted&
i_rhs)
:m_reference_count(1),
std :: vector< T>(i_rhs)
{
}
内联vector_reference_counted& operator =(const
vector_reference_counted& i_rhs)
{
//引用计数在分配时不会改变

* static_cast<的std ::矢量< T> *>(this)= i_rhs;

返回* this;
}

内联vector_reference_counted& operator =(const std :: vector< T>
& i_rhs)
{
//引用计数在分配时不会改变

* static_cast<的std ::矢量< T> *>(this)= i_rhs;

返回* this;
}
int AddRef()
{
int l_count = ++ m_reference_count;

返回l_count;
}
int版本()
{
int l_count = - m_reference_count;

if(l_count == 0)
{
删除此内容;
}
返回l_count;
}

};



Why? I know about the shared_ptr. How does it help?

Or, you could do somthing like this :

#include <vector>

template <typename T>
class vector_reference_counted
: public std::vector<T>
{

int m_reference_count;

public:

inline vector_reference_counted()
: m_reference_count( 1 )
{
}

inline vector_reference_counted( const vector_reference_counted &
i_rhs )
: m_reference_count( 1 ),
std::vector<T>( i_rhs )
{
}

inline vector_reference_counted & operator = ( const
vector_reference_counted & i_rhs )
{
// reference count does not change upon assignment

* static_cast< std::vector<T> * >( this ) = i_rhs;

return * this;
}

inline vector_reference_counted & operator = ( const std::vector<T>
& i_rhs )
{
// reference count does not change upon assignment

* static_cast< std::vector<T> * >( this ) = i_rhs;

return * this;
}

int AddRef()
{
int l_count = ++ m_reference_count;
return l_count;
}

int Release()
{
int l_count = -- m_reference_count;

if ( l_count == 0 )
{
delete this;
}

return l_count;
}

};




我也不知道这有什么帮助。请解释。



I don''t see how that helps either. Please explain.


Jason Heyes写道:
Jason Heyes wrote:
" Gianni Mariani" < GI ******* @ mariani.ws>在消息中写道
新闻:br ******** @ dispatch.concentric.net ...
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:br********@dispatch.concentric.net...
Jason Heyes写道:
Jason Heyes wrote:
根据我的理解,std :: vector不使用引用计数
To my understanding, std::vector does not use reference counting to



我也看不出它有何帮助。请解释。


I don''t see how that helps either. Please explain.




也许你应该解释一下你想要做什么。



Maybe you should explain what you''re trying to do.


这篇关于是否有引用计数std :: vector的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆