﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-陈陈-随笔分类-STL</title><link>http://www.cppblog.com/chenchen/category/684.html</link><description>C++</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 08:56:31 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 08:56:31 GMT</pubDate><ttl>60</ttl><item><title>伪随机数产生器</title><link>http://www.cppblog.com/chenchen/archive/2005/12/28/2210.html</link><dc:creator>chenchen</dc:creator><author>chenchen</author><pubDate>Wed, 28 Dec 2005 05:35:00 GMT</pubDate><guid>http://www.cppblog.com/chenchen/archive/2005/12/28/2210.html</guid><wfw:comment>http://www.cppblog.com/chenchen/comments/2210.html</wfw:comment><comments>http://www.cppblog.com/chenchen/archive/2005/12/28/2210.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/chenchen/comments/commentRss/2210.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenchen/services/trackbacks/2210.html</trackback:ping><description><![CDATA[class subtractive_rng : public unary_function&lt;unsigned int, unsigned int&gt; {<BR>private:<BR>&nbsp; unsigned int _M_table[55];<BR>&nbsp; size_t _M_index1;<BR>&nbsp; size_t _M_index2;<BR>public:<BR>&nbsp; unsigned int operator()(unsigned int __limit) {<BR>&nbsp;&nbsp;&nbsp; _M_index1 = (_M_index1 + 1) % 55;<BR>&nbsp;&nbsp;&nbsp; _M_index2 = (_M_index2 + 1) % 55;<BR>&nbsp;&nbsp;&nbsp; _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];<BR>&nbsp;&nbsp;&nbsp; return _M_table[_M_index1] % __limit;<BR>&nbsp; }<BR><BR>&nbsp; void _M_initialize(unsigned int __seed)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; unsigned int __k = 1;<BR>&nbsp;&nbsp;&nbsp; _M_table[54] = __seed;<BR>&nbsp;&nbsp;&nbsp; size_t __i;<BR>&nbsp;&nbsp;&nbsp; for (__i = 0; __i &lt; 54; __i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size_t __ii = (21 * (__i + 1) % 55) - 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _M_table[__ii] = __k;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __k = __seed - __k;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __seed = _M_table[__ii];<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; for (int __loop = 0; __loop &lt; 4; __loop++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (__i = 0; __i &lt; 55; __i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; _M_index1 = 0;<BR>&nbsp;&nbsp;&nbsp; _M_index2 = 31;<BR>&nbsp; }<BR><BR>&nbsp; subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }<BR>&nbsp; subtractive_rng() { _M_initialize(161803398u); }<BR>};<img src ="http://www.cppblog.com/chenchen/aggbug/2210.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenchen/" target="_blank">chenchen</a> 2005-12-28 13:35 <a href="http://www.cppblog.com/chenchen/archive/2005/12/28/2210.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>stl 函数对象</title><link>http://www.cppblog.com/chenchen/archive/2005/12/27/2190.html</link><dc:creator>chenchen</dc:creator><author>chenchen</author><pubDate>Tue, 27 Dec 2005 13:14:00 GMT</pubDate><guid>http://www.cppblog.com/chenchen/archive/2005/12/27/2190.html</guid><wfw:comment>http://www.cppblog.com/chenchen/comments/2190.html</wfw:comment><comments>http://www.cppblog.com/chenchen/archive/2005/12/27/2190.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/chenchen/comments/commentRss/2190.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/chenchen/services/trackbacks/2190.html</trackback:ping><description><![CDATA[<P><BR>template &lt;class _Tp&gt;<BR>struct _Identity : public unary_function&lt;_Tp,_Tp&gt; {<BR>&nbsp; const _Tp&amp; operator()(const _Tp&amp; __x) const { return __x; }<BR>};</P>
<P>template &lt;class _Tp&gt; struct identity : public _Identity&lt;_Tp&gt; {};<BR></P>
<P>//验证模板参数是不是一个函数对象类型<BR><BR>template &lt;class _Pair&gt;<BR>struct _Select1st : public unary_function&lt;_Pair, typename _Pair::first_type&gt; {<BR>&nbsp; const typename _Pair::first_type&amp; operator()(const _Pair&amp; __x) const {<BR>&nbsp;&nbsp;&nbsp; return __x.first;<BR>&nbsp; }<BR>};</P>
<P>template &lt;class _Pair&gt;<BR>struct _Select2nd : public unary_function&lt;_Pair, typename _Pair::second_type&gt;<BR>{<BR>&nbsp; const typename _Pair::second_type&amp; operator()(const _Pair&amp; __x) const {<BR>&nbsp;&nbsp;&nbsp; return __x.second;<BR>&nbsp; }<BR>};</P>
<P>template &lt;class _Pair&gt; struct select1st : public _Select1st&lt;_Pair&gt; {};<BR>template &lt;class _Pair&gt; struct select2nd : public _Select2nd&lt;_Pair&gt; {};<BR></P>
<P>//忽略模板参数为pair类型所包含的相应参数(第一个参数和第二个参数)<BR><BR>template &lt;class _Arg1, class _Arg2&gt;<BR>struct _Project1st : public binary_function&lt;_Arg1, _Arg2, _Arg1&gt; {<BR>&nbsp; _Arg1 operator()(const _Arg1&amp; __x, const _Arg2&amp;) const { return __x; }<BR>};</P>
<P>template &lt;class _Arg1, class _Arg2&gt;<BR>struct _Project2nd : public binary_function&lt;_Arg1, _Arg2, _Arg2&gt; {<BR>&nbsp; _Arg2 operator()(const _Arg1&amp;, const _Arg2&amp; __y) const { return __y; }<BR>};</P>
<P>template &lt;class _Arg1, class _Arg2&gt; <BR>struct project1st : public _Project1st&lt;_Arg1, _Arg2&gt; {};</P>
<P>template &lt;class _Arg1, class _Arg2&gt;<BR>struct project2nd : public _Project2nd&lt;_Arg1, _Arg2&gt; {};<BR></P>
<P>//忽略调用运算符时相应的参数</P>
<P>template &lt;class _Result&gt;<BR>struct _Constant_void_fun {<BR>&nbsp; typedef _Result result_type;<BR>&nbsp; result_type _M_val;</P>
<P>&nbsp; _Constant_void_fun(const result_type&amp; __v) : _M_val(__v) {}<BR>&nbsp; const result_type&amp; operator() const { return _M_val; }<BR>};&nbsp; </P>
<P>template &lt;class _Result, class _Argument&gt;<BR>struct _Constant_unary_fun {<BR>&nbsp; typedef _Argument argument_type;<BR>&nbsp; typedef&nbsp; _Result&nbsp; result_type;<BR>&nbsp; result_type _M_val;</P>
<P>&nbsp; _Constant_unary_fun(const result_type&amp; __v) : _M_val(__v) {}<BR>&nbsp; const result_type&amp; operator()(const _Argument&amp;) const { return _M_val; }<BR>};</P>
<P>template &lt;class _Result, class _Arg1, class _Arg2&gt;<BR>struct _Constant_binary_fun {<BR>&nbsp; typedef&nbsp; _Arg1&nbsp;&nbsp; first_argument_type;<BR>&nbsp; typedef&nbsp; _Arg2&nbsp;&nbsp; second_argument_type;<BR>&nbsp; typedef&nbsp; _Result result_type;<BR>&nbsp; _Result _M_val;</P>
<P>&nbsp; _Constant_binary_fun(const _Result&amp; __v) : _M_val(__v) {}<BR>&nbsp; const result_type&amp; operator()(const _Arg1&amp;, const _Arg2&amp;) const {<BR>&nbsp;&nbsp;&nbsp; return _M_val;<BR>&nbsp; }<BR>};<BR></P>
<P>//返回一个普通变量的const副本,无论参数为何,只返回同一个值的函数对象<BR><BR>template &lt;class _Result&gt;<BR>struct constant_void_fun : public _Constant_void_fun&lt;_Result&gt; {<BR>&nbsp; constant_void_fun(const _Result&amp; __v) : _Constant_void_fun&lt;_Result&gt;(__v) {}<BR>};&nbsp; </P>
<P>template &lt;class _Result,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class _Argument __STL_DEPENDENT_DEFAULT_TMPL(_Result)&gt;<BR>struct constant_unary_fun : public _Constant_unary_fun&lt;_Result, _Argument&gt;<BR>{<BR>&nbsp; constant_unary_fun(const _Result&amp; __v)<BR>&nbsp;&nbsp;&nbsp; : _Constant_unary_fun&lt;_Result, _Argument&gt;(__v) {}<BR>};</P>
<P><BR>template &lt;class _Result,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class _Arg1 __STL_DEPENDENT_DEFAULT_TMPL(_Result),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class _Arg2 __STL_DEPENDENT_DEFAULT_TMPL(_Arg1)&gt;<BR>struct constant_binary_fun<BR>&nbsp; : public _Constant_binary_fun&lt;_Result, _Arg1, _Arg2&gt;<BR>{<BR>&nbsp; constant_binary_fun(const _Result&amp; __v)<BR>&nbsp;&nbsp;&nbsp; : _Constant_binary_fun&lt;_Result, _Arg1, _Arg2&gt;(__v) {}<BR>};<BR></P>
<P>//__STL_DEPENDENT_DEFAULT_TMPL 是一个宏，意思是如果编译器不支持默认模板参数，则使用上一级模板参数<BR><BR>template &lt;class _Result&gt;<BR>inline constant_void_fun&lt;_Result&gt; constant0(const _Result&amp; __val)<BR>{<BR>&nbsp; return constant_void_fun&lt;_Result&gt;(__val);<BR>}</P>
<P>template &lt;class _Result&gt;<BR>inline constant_unary_fun&lt;_Result,_Result&gt; constant1(const _Result&amp; __val)<BR>{<BR>&nbsp; return constant_unary_fun&lt;_Result,_Result&gt;(__val);<BR>}</P>
<P>template &lt;class _Result&gt;<BR>inline constant_binary_fun&lt;_Result,_Result,_Result&gt; <BR>constant2(const _Result&amp; __val)<BR>{<BR>&nbsp; return constant_binary_fun&lt;_Result,_Result,_Result&gt;(__val);<BR>}<BR><BR>//函数返回一个只返回常量和输入参数无关的函数对象</P><img src ="http://www.cppblog.com/chenchen/aggbug/2190.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/chenchen/" target="_blank">chenchen</a> 2005-12-27 21:14 <a href="http://www.cppblog.com/chenchen/archive/2005/12/27/2190.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>