STL扩展 [英] STL extension

查看:78
本文介绍了STL扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我尝试扩展STL,例如,添加成员函数如trim_left到

basic_string< charT,traits,Alloc>。然而,我一旦绝望,我就会看到

STL来源。我正在使用STLport 5.0-0409和vc 6.0。


如何轻松扩展STL?任何帮助将不胜感激。谢谢。

Hello.

I try to extend STL, for example, add member function like trim_left to
basic_string<charT, traits, Alloc>. However I am in the despair as soon as I
see
STL source. I am using STLport 5.0-0409 with vc 6.0.

How can I extend STL easily? Any help will be appreciated. Thanks.

推荐答案

" Park Ho-Kyung" < IL **** @ eoneo.co.kr>在消息中写道

news:cf ********** @ news1.kornet.net ...
"Park Ho-Kyung" <il****@eoneo.co.kr> wrote in message
news:cf**********@news1.kornet.net...
你好。
我尝试扩展STL,例如,添加成员函数如trim_left到
basic_string< charT,traits,Alloc>。然而,我在绝望中只要
我看到了STL来源。我正在使用STLport 5.0-0409和vc 6.0。

如何轻松扩展STL?任何帮助将不胜感激。谢谢。
Hello.

I try to extend STL, for example, add member function like trim_left to
basic_string<charT, traits, Alloc>. However I am in the despair as soon as I see
STL source. I am using STLport 5.0-0409 with vc 6.0.

How can I extend STL easily? Any help will be appreciated. Thanks.




如果你想要一个在std :: string上运行的trim_left函数,那么你需要将它作为一个非b / b来实现会员功能。标准容器

类不是为继承而设计的。没有理由扩展

容器类来添加成员函数,因为与非成员函数方法相比,它没有提供重要的好处。假设你希望从字符串中删除一些字符,你可以这样实现:


//返回结果

std :: string trim_left(const std :: string& s);


//或


//修改参数

void trim_left(std :: string& s);


如果你不希望它出现在

全局命名空间。


-

David Hilsee



If you want a trim_left function that operates on a std::string, then you
should implement it as a non-member function. The standard container
classes were not designed for inheritance. There is no reason to extend a
container class to add member functions because that provides no significant
benefits over the non-member function approach. Assuming that you wish to
remove some characters from the string, you could implement it like so:

// returns the result
std::string trim_left( const std::string& s );

// or

// modifies the argument
void trim_left( std::string& s );

You could place this function in a namespace if you don''t want it to be in
the global namespace.

--
David Hilsee


Park Ho-Kyung写道:
Park Ho-Kyung wrote:
你好。

我尝试扩展STL,例如,添加成员函数如trim_left到
basic_string< charT,traits,Alloc> ;。但是,一旦我看到STL来源,我就绝望了。我正在使用STLport 5.0-0409和vc 6.0。

如何轻松扩展STL?任何帮助将不胜感激。谢谢。
Hello.

I try to extend STL, for example, add member function like trim_left to
basic_string<charT, traits, Alloc>. However I am in the despair as soon as I
see
STL source. I am using STLport 5.0-0409 with vc 6.0.

How can I extend STL easily? Any help will be appreciated. Thanks.



从STL中获取自己的类并添加新的

功能。不要搞乱那里的东西,那不是C ++方式!


Ian


Derive your own classes form those in the STL and add your new
functionality. Don''t mess with what''s there, that''s not the C++ way!

Ian


> ;标准容器类不是为继承而设计的。


怎么没有?
> The standard container classes were not designed for inheritance.

How not?
没有理由扩展容器类来添加成员函数
因为与非成员函数方法没有明显的好处。
There is no reason to extend a container class to add member functions
because that provides no significant benefits over the non-member
function approach.




除了语义之外。 IMO,编写

str.trim_left()而不是trim_left(str)更合乎逻辑。


-

Kevin W :-)

Opera / CSS / webdev blog: http ://www.exclipy.com/

使用Opera: http://www.opera.com/m2/


这篇关于STL扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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