??xml version="1.0" encoding="utf-8" standalone="yes"?>久久亚洲AV成人无码,热99RE久久精品这里都是精品免费,久久综合亚洲色HEZYO国产http://m.shnenglu.com/Rich-cw/探烦未知zh-cnSat, 28 Jun 2025 11:25:45 GMTSat, 28 Jun 2025 11:25:45 GMT60to be continuehttp://m.shnenglu.com/Rich-cw/archive/2010/01/05/104864.html崇文崇文Tue, 05 Jan 2010 13:51:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2010/01/05/104864.htmlhttp://m.shnenglu.com/Rich-cw/comments/104864.htmlhttp://m.shnenglu.com/Rich-cw/archive/2010/01/05/104864.html#Feedback0http://m.shnenglu.com/Rich-cw/comments/commentRss/104864.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/104864.html     q切地想要写点什么,却不知从何开头。这些天看的东西很杂Q看《c++模板元编E》看得头大,看《深入浅出ATL》苦于无法实践,做题也没心思,又纠l于快要l课的考试。Ş了,静心潜修才是正道。但d提炼一下这混ؕ的思想Q不?#8220;脑黄?#8221;p随着“豆腐?#8221;被时间的潮水冲走了。先拟个题目Q待考试一完就来补上?/p>

     杂项Qboost mpl 中的q代器、算法与其它?/strong>

     题目有点大,只能力研究源码Q尽量多写。好Q目标在此,留待再议?/p>

崇文 2010-01-05 21:51 发表评论
]]>
一些图http://m.shnenglu.com/Rich-cw/archive/2009/12/30/104489.html崇文崇文Wed, 30 Dec 2009 13:19:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2009/12/30/104489.htmlhttp://m.shnenglu.com/Rich-cw/comments/104489.htmlhttp://m.shnenglu.com/Rich-cw/archive/2009/12/30/104489.html#Feedback1http://m.shnenglu.com/Rich-cw/comments/commentRss/104489.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/104489.html










崇文 2009-12-30 21:19 发表评论
]]>
TopCoder l习题之排列QPermutationQ?/title><link>http://m.shnenglu.com/Rich-cw/archive/2009/12/19/103543.html</link><dc:creator>崇文</dc:creator><author>崇文</author><pubDate>Sat, 19 Dec 2009 12:53:00 GMT</pubDate><guid>http://m.shnenglu.com/Rich-cw/archive/2009/12/19/103543.html</guid><wfw:comment>http://m.shnenglu.com/Rich-cw/comments/103543.html</wfw:comment><comments>http://m.shnenglu.com/Rich-cw/archive/2009/12/19/103543.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Rich-cw/comments/commentRss/103543.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Rich-cw/services/trackbacks/103543.html</trackback:ping><description><![CDATA[<p>     题目及解题程序给在末,先来看看排列一个数l的Ҏ?/p> <p>     l定一个数l?array[] = {3, 1, 2, 4, 0}; q个l定的数l有目的性,卛_W合 n * m 的规则,q里?5 * 5Q?个元素,5个连l且不同的|。按我想到的一般的ҎQ就是用@环来求出各种排列的可能,但这U方法不能确保每个元素只出现一ơ,且随着元素个数的增长,循环深度变得很深。l想下去Q这U方法将会变得很复杂Q这p求我L另外一U方法。注意到每个元素q不相同Q那么要使各个元素在每个位置上只出现一ơ,很明昄一U方法就是“彩机ȝ法”。比如数据读入口在第一个元素的位置Q那么依ơ@环这个数l,每次使后面的元素向前Ud一位,各个数字不就都读C吗,q就像在打印Z滚动的纸。具体步骤如下:</p> <p>31240<br>12403 <—rotate <p>     W一位如此,那么后面的每一位也如此Q也是递归地处理后面的数字Q每Ud一位就以下一位ؓL做相同的处理Q直到所有数字@环了一遍,那排列的工作也就完成了。一个具体的实现如下Q?pre class="code"><span style="color: green">/* * @param r: 需要求其排列的向量 * @param iPos: 当前所q行到的位置 * E序体中的注释表C处于那个位|的向量都是一个新的且唯一的排? */</span></pre><pre class="code"><span style="color: green"></span><span style="color: blue">void </span>rotate(vector<<span style="color: blue">int</span>>& r, <span style="color: blue">int </span>iPos) { <span style="color: blue">if</span>(iPos == r.size() - 1)<span style="color: green">//是否循环完毕Q调用函数时 iPos |? </span><span style="color: blue">return</span>; <span style="color: blue">int </span>iNextPos = iPos + 1; <span style="color: blue">for</span>(size_t i = iPos; i < r.size(); ++i) { <span style="color: blue">if</span>(i == 0) { <span style="color: green">//a different permutation, do something here </span>} <span style="color: blue">int </span>t = r[iPos]; <span style="color: blue">for</span>(size_t j = iPos; j < r.size() - 1; ++j)<span style="color: green">//循环前移 </span>r[j] = r[j + 1]; r[r.size() - 1] = t; <span style="color: blue">if</span>(i != r.size() - 1) { <span style="color: green">//a different permutation, do something here </span>} rotate(r, iNextPos);<span style="color: green">//从下一位数字开始新的位U? </span>} }</pre><a ></a><pre class="code"> q种Ҏ不要求数字式q箋的,也不用事先规定好向量的长度。只是当向量长度C一定的时候,q算旉会很长!其它Ҏ未知…?/pre><pre class="code"> topcoder 上的l习题如下:</pre> <p>Problem Statement <p>A permutation A[0], A[1], ..., A[N-1] is a sequence containing each integer between 0 and N-1, inclusive, exactly once. Each permutation A of length N has a corresponding child array B of the same length, where B is defined as follows:<br>B[0] = 0<br>B[i] = A[B[i-1]], for every i between 1 and N-1, inclusive.<br>A permutation is considered perfect if its child array is also a permutation.  Below are given all permutations for N=3 with their child arrays. Note that for two of these permutations ({1, 2, 0} and {2, 0, 1}) the child array is also a permutation, so these two permutations are perfect.<br>Permutation        Child array<br>{0, 1, 2}        {0, 0, 0}<br>{0, 2, 1}        {0, 0, 0}<br>{1, 0, 2}        {0, 1, 0}<br>{1, 2, 0}        {0, 1, 2}<br>{2, 0, 1}        {0, 2, 1}<br>{2, 1, 0}        {0, 2, 0}<br>You are given a vector <int> P containing a permutation of length N. Find a perfect permutation Q of the same length such that the difference between P and Q is as small as possible, and return this difference. The difference between P and Q is the number of indices i for which P[i] and Q[i] are different.<br>Definition <p>Class:<br>PerfectPermutation<br>Method:<br>reorder<br>Parameters:<br>vector <int><br>Returns:<br>int<br>Method signature:<br>int reorder(vector <int> P)<br>(be sure your method is public) <p>Constraints<br>-<br>P will contain between 1 and 50 elements, inclusive.<br>-<br>P will contain each integer between 0 and N-1, inclusive, exactly once, where N is the number of elements in P.<br>Examples <p>0)<br>{2, 0, 1}<br>Returns: 0<br>P is a perfect permutation, so we can use the same permutation for Q. The difference is then 0 because P and Q are the same. <p>1)<br>{2, 0, 1, 4, 3}<br>Returns: 2<br>Q might be {2, 0, 3, 4, 1}. <p>2)<br>{2, 3, 0, 1}<br>Returns: 2<br>Q might be {1, 3, 0, 2}. <p>3)<br>{0, 5, 3, 2, 1, 4}<br>Returns: 3 <p>4)<br>{4, 2, 6, 0, 3, 5, 9, 7, 8, 1}<br>Returns: 5 <p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.</p>    我的解答如下Q?pre class="code"><span style="color: blue">#include </span><span style="color: #a31515"><iostream> </span><span style="color: blue">#include </span><span style="color: #a31515"><vector> </span><span style="color: blue">#include </span><span style="color: #a31515"><cstddef> </span><span style="color: blue">#include </span><span style="color: #a31515"><limits> </span><span style="color: blue">#include </span><span style="color: #a31515"><cassert> </span><span style="color: blue">#include </span><span style="color: #a31515"><boost\assign.hpp> </span><span style="color: green">// for vector += </span><span style="color: blue">using namespace </span>std; <span style="color: blue">class </span>PerfectPermutation { <span style="color: blue">public</span>: <span style="color: blue">int </span>reorder(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& P, vector<<span style="color: blue">int</span>>& result); <span style="color: blue">bool </span>isPerfect(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& P); <span style="color: blue">private</span>: <span style="color: blue">int </span>difference(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& P, <span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& Q); <span style="color: blue">void </span>rotate(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& src, vector<<span style="color: blue">int</span>>& r, <span style="color: blue">int </span>level, <span style="color: blue">int</span>& nMin, vector<<span style="color: blue">int</span>>& out); }; <span style="color: blue">int </span>PerfectPermutation::difference(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& P, <span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& Q) { size_t cDiff = P.size(); assert(cDiff == Q.size()); <span style="color: blue">for</span>(size_t i = 0; i < P.size(); ++i) { <span style="color: blue">if</span>(P[i] == Q[i]) cDiff--; } <span style="color: blue">return </span>cDiff; } <span style="color: blue">bool </span>PerfectPermutation::isPerfect(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& A) { <span style="color: blue">int </span>Bi = 0, Bi_1 = 0; vector<<span style="color: blue">bool</span>> vb(A.size()); vb[0] = <span style="color: blue">true</span>; <span style="color: blue">for</span>(size_t i = 1; i < A.size(); ++i) { <span style="color: blue">if</span>(vb[Bi = A[Bi_1]]) <span style="color: blue">return false</span>; <span style="color: blue">else </span>vb[Bi] = <span style="color: blue">true</span>; Bi_1 = Bi; } <span style="color: blue">return true</span>; } <span style="color: blue">void </span>PerfectPermutation::rotate(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& src, vector<<span style="color: blue">int</span>>& r, <span style="color: blue">int </span>level, <span style="color: blue">int</span>& nMin, vector<<span style="color: blue">int</span>>& out) { <span style="color: blue">if</span>(level == r.size() - 1) <span style="color: blue">return</span>; <span style="color: blue">int </span>in = level + 1; <span style="color: blue">for</span>(size_t i = level; i < r.size(); ++i) { <span style="color: blue">if</span>(i == 0 && isPerfect(r)) { nMin = min(difference(src, r), nMin); out = r; } <span style="color: blue">int </span>t = r[level]; <span style="color: blue">for</span>(size_t j = level; j < r.size() - 1; ++j) r[j] = r[j + 1]; r[r.size() - 1] = t; <span style="color: blue">if</span>((i != r.size() - 1) && isPerfect(r)) { nMin = min(difference(src, r), nMin); out = r; } rotate(src, r, in, nMin, out); } } <span style="color: blue">int </span>PerfectPermutation::reorder(<span style="color: blue">const </span>vector<<span style="color: blue">int</span>>& P, vector<<span style="color: blue">int</span>>& result) { <span style="color: blue">if</span>(P.size() == 1 || isPerfect(P)) <span style="color: blue">return </span>0; <span style="color: blue">int </span>nMin = numeric_limits<<span style="color: blue">int</span>>::max(); vector<<span style="color: blue">int</span>> Q(P); rotate(P, Q, 0, nMin, result); <span style="color: blue">return </span>nMin == numeric_limits<<span style="color: blue">int</span>>::max() ? -1 : nMin; } <span style="color: blue">int </span>main() { <span style="color: blue">using namespace </span>boost::assign; PerfectPermutation pp; vector<<span style="color: blue">int</span>> P; P += 2, 0, 1, 4, 3; vector<<span style="color: blue">int</span>> result(P.size()); cout << <span style="color: #a31515">"Is a perfect Permutation : " </span><< (pp.isPerfect(P) ? <span style="color: #a31515">"Yes" </span>: <span style="color: #a31515">"No"</span>) << endl; cout << <span style="color: #a31515">"Difference between before reorder and after : " </span><< pp.reorder(P, result) << endl; assert(pp.isPerfect(result)); cout << <span style="color: #a31515">"One answer might be : "</span>; <span style="color: blue">for</span>(size_t i = 0; i < result.size(); ++i) cout << result[i] << <span style="color: #a31515">" "</span>; cout << endl; <span style="color: blue">return </span>0; }</pre><a ></a><img src ="http://m.shnenglu.com/Rich-cw/aggbug/103543.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Rich-cw/" target="_blank">崇文</a> 2009-12-19 20:53 <a href="http://m.shnenglu.com/Rich-cw/archive/2009/12/19/103543.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>整理几个offsethttp://m.shnenglu.com/Rich-cw/archive/2009/12/12/103059.html崇文崇文Sat, 12 Dec 2009 07:16:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2009/12/12/103059.htmlhttp://m.shnenglu.com/Rich-cw/comments/103059.htmlhttp://m.shnenglu.com/Rich-cw/archive/2009/12/12/103059.html#Feedback0http://m.shnenglu.com/Rich-cw/comments/commentRss/103059.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/103059.html      q几?offset 是:

      c 中的 offsetof 宏;

      《COM本质论? "inttable.h" 中的 BASE_OFFSET Q?/p>

      ?This() (它用C offsetofQ权且当做是一?packaged offset ??/p>

      先定义一些辅助类Q?/p>

struct IX {
    virtual void fx() = 0;
};
struct IY {
    virtual void fy() = 0;
};
class CA : public IX,
           public IY {
public:
    virtual void fx() { cout << "CA::fx()" << endl; };
    virtual void fy() { cout << "CA::fy()" << endl; };

    struct Inner {
        CA* This() { return (CA*)((char*)this - offsetof(CA, m_inner)); }
    };
    Inner m_inner;
};

      先看 offsetof Q?/p>

#define offsetof(s,m)   (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
    工作原理大体同下图所C,其值就?a ?0 = a Q即 m 被映后的地址。所以一??gt; 操作可以搞定?/pre>
offset 
    再看 BASE_OFFSET Q?/pre>
#define BASE_OFFSET(ClassName, BaseName) \
    (DWORD(static_cast<BaseName*>(reinterpret_cast<ClassName*>(0x10000000))) - 0x10000000)

    对于先前的定义,有:

DWORD disX = BASE_OFFSET(CA, IX);
DWORD disY = BASE_OFFSET(CA, IY);
cout << disX << endl    // 0
      
<< disY << endl;   // 4

    q个宏主要利用了zcL针到基类指针的{换,q一切由语言内置的{换符完成。在 ATL 中相应的定义是:

#define offsetofclass(base, derived) ((DWORD_PTR)(static_cast<base*>((derived*)_ATL_PACKING))-_ATL_PACKING)

    其中 _ATL_PACKING 的gؓ 8 。ؓ什么用这两个值呢Q我不知道。网上有说是Z避免 NULL 指针和负|我暂时不能够理解?/p>

    最后来?This() Q?/p>

CA* This() { return (CA*)((BYTE*)this - offsetof(CA, Inner)); }
    This() q回的即是指?CA 的指针,如: ca.m_inner.This()->fx(); 有了上面两个 offset Q这个就不难理解了?/pre>
    整理完毕?/pre>

崇文 2009-12-12 15:16 发表评论
]]>typedef 关键字二三事http://m.shnenglu.com/Rich-cw/archive/2009/12/09/102864.html崇文崇文Wed, 09 Dec 2009 13:20:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2009/12/09/102864.htmlhttp://m.shnenglu.com/Rich-cw/comments/102864.htmlhttp://m.shnenglu.com/Rich-cw/archive/2009/12/09/102864.html#Feedback0http://m.shnenglu.com/Rich-cw/comments/commentRss/102864.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/102864.html      记得在看《windows 核心~程》第二十五章?Spreadsheet CZE序Ӟ我对其中 typedef 的用法很是困惑。当时翻看了《Big c++》(我的W一?c++ 书籍Q,很可惜,上面讲得很简单,不以解{我的疑惑。不知ؓ什么当时没有坚持去解决那个问题Q一直到现在才想h?typedef 做个全面的回。STL ?BOOST 为我们呈C众多_ֽ的应用,typedef 功不可没。先看看 《c++ primer?怎么说的Q?/p>

      Introduces a synonym for some other type. Form:

      为某U类型引入同义词。格式:

      typedef type synonym;
     

      defines synonym as another name for the type named type.

      定义 synonym 为名?type 的类型的另一名字?/p>

      在看《核》一书的时候,我的了解p么一炏Vؓ什么我看不懂呢Q看看《核》中的用法:

const int g_nNumRows = 256;
const int g_nNumCols = 1024;

typedef struct {
    DWORD dwValue;
    BYTE  bDummy[1020];
} CELL, *PCELL;

typedef CELL SPREADSHEET[g_nNumRows][g_nNumCols];
typedef SPREADSHEET *PSPREADSHEET;
    按照当时的想法,句子 SPREADSHEET[g_nNumRows][g_nNumCols] sh 的意思应该是 CELL sh Q不p是这L吗(因ؓ它只是个别名/马甲Q?但在我那个想法里 sh 可不是数l,所以我当时无法理解 sh 后来的行为。Ş式主义,对,形式M影响了我Q当时我׃d的Ş式主义者。我q想再一遍地重复q个词,不过那也太给它面子了。算了, Not worth to fight!(哈,最q看的一部精彩的家庭喜剧中的台词。这部剧名叫《the middle?。《核》一书中接下来还有一个精彩的用法Qؓ了简化代码,我把它写成这个样子:
class Base {
public:
    virtual ~Base();
    void fn();
    ...
private:
    int n;
};

class Derive : public Base {
    ...
    //no data member allow, just function here.
};
static Derive g_ssObject;
SPREADSHEET& g_ss = *(PSPREADSHEET)(PCELL)g_ssObject;
    如此一来,g_ss 有?Base ?n 成员?256 * 1024 ?CELL l构。当?Derive 对象使用 [] Ӟ它是 SPREADSHEET& 型)Q它的行为就跟数l一致了Qn 成员不会索引刎ͼq也是不允许zcL数据成员的原因)。关于它的内存ƈ没有在它声明的时候开辟,以及后来怎么开辟,那就是另一个冒险故事了。这U手法让我再一ơؓ《核》这本书所折服?/pre>
    好了Q回?typedef ?/span>我见得最多的用法不外?typedef int value_type Q“取l号”是他的职责所在,我不能只把它理解为:当你看到 BQ?value_type Q?Ӟ它只不过?A Q?int Q,q要把它理解为像上面提到的一PSPREADSHEET q不只是一?CELL Q而是多个 CELL l成?Array 的首个元素的位置指针Q不然后面的两个跟屁虫双胞胎 “[][]?作何解释。我的这U想法让我想起了 ATL 中的 OLECHAR Q在那里我也曾是个Ş式主义者(又重复了一遍!Q。OLECHAR 在特定的q_上有特定的意义,比如?windows q_q定义了 UNICODE 的情况下Q它?wchar_t 。如果保持这U理解,无法把它当做一U特别的cdQ从而在语言层面上有了障,也就q背?OLECHAR 生来是ؓ COM lg做“特D”服务的本意。我们要把它当做基本cd看待Q至于怎么看待以及详细的方法,另一个冒险故事…?所以,“取l号”专家的内涵q不单薄Q我不能一厢情愿的认ؓ他只是个爱捉弄h的讨厌鬼Q它正经h可也是一表h才滴。下面是一?typedef 的用法(来自《c++模板元编E》中某习题的解答Q:
class foo {...};

typedef int (foo::*pmd);    // pmd 解释为:指向 foo 数据成员的指针,q回gؓ int
typedef int (foo::*pmf)();  // pmf 解释为:指向 foo 函数成员的指针,q回gؓ int
typedef int (*pfunc)();     // pfunc 解释为:指向函数的指针,q回gؓ int 且无参数Q有参数的也׃难想象了Q?
typedef const int& refc;    // 不说q个?/span>
    上面的展C非怸全面Q网上ȝ的东西一大堆Q我不能h引用Q去看才实在。不q有了这些知识,理解其它东西应该_了?

崇文 2009-12-09 21:20 发表评论
]]>boost 中的 vector 及其 operator+=http://m.shnenglu.com/Rich-cw/archive/2009/12/09/102837.html崇文崇文Wed, 09 Dec 2009 03:01:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2009/12/09/102837.htmlhttp://m.shnenglu.com/Rich-cw/comments/102837.htmlhttp://m.shnenglu.com/Rich-cw/archive/2009/12/09/102837.html#Feedback0http://m.shnenglu.com/Rich-cw/comments/commentRss/102837.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/102837.html     在看 《c++ 模板元编E?的过E中Q我时时d都会有新发现。对于这之前从未接触q?Boost 库的我来_惊奇的感觉尤甚。初看此书的习题4-5Q哈Q不是实现一?param_type 嘛。的是Q不q?answer 上的测试代码,才是让我惊奇的东西:

using namespace boost::assign;
std::vector<int> v1;
v1 += 1, 2, 30, 40, 500;
   话不多讲Q按囄骥的来分析。既然是 vector 的操作,那就扑և对应的头文g来看看。第一句用了 boost::assign 命名I间Q?STL 库中当然不会提供q样?operator+= Q果然在 boost\assign\std\vector.hpp 中有声明Q?/pre>
template< class V, class A, class V2 >
inline list_inserter< assign_detail::call_push_back< std::vector<V,A> >, V > 
operator+=( std::vector<V,A>& c, V2 v ) {
    return push_back( c )( v );
}
    For the god’s sake Q它一口气抛给我这么多待查的东西,q不是打L的求知欲么!Q歇息片删Z…)l箋。先不管q个重蝲操作W的q回|待会在函数的q回语句中自然会了解到。看?push_back( c )( v ) q个句子Q当然是一?operator()Ql搜索。这一ơ,VS 代码查看工具直接把我带到了目的地—?boost\assign\list_inserter.hpp q块新大陆。接下来的一切都在q里发生。首先我C “push_back 站”:
template< class C >
inline list_inserter< assign_detail::call_push_back<C>, 
                      BOOST_DEDUCED_TYPENAME C::value_type >
push_back( C& c ) {
    static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;//static typename std::vector<int>::value_type* p = 0;
    return make_list_inserter( assign_detail::call_push_back<C>( c ), p );
}
    Z方便查看Q把 make_list_inserter ?call_push_back 也列出来Q?/pre>
template< class Function, class Argument >
inline list_inserter<Function,Argument>
make_list_inserter( Function fun, Argument* ) {
    return list_inserter<Function, Argument>( fun );
}
template< class C >
class call_push_back {
    C& c_;  //q里Qc_ ?vector<int>& v1
public:
    call_push_back( C& c ) : c_( c ) { }
    
    template< class T >
    void operator()( T r ) {
        c_.push_back( r );
    }
};
    q了q条水U,现在 operator+= 中的 push_back( c ) p加工成了Q?/pre>
    list_inserter<call_push_back, p>( call_push_back<vector<int> > ) Q其U有成员 insert_ ?call_push_back<vector<int> > 。前面提到的 operator() pd了,先看 list_inserter 的部分代码:
template< class Function, class Argument = assign_detail::forward_n_arguments > 
class list_inserter {
    ...  
public:
    list_inserter( Function fun ) : insert_( fun ) {}    
    ...
    list_inserter& operator()() {    //q就是那?operator() ! l于扑ֈ你了
        insert_( Argument() );
        return *this;
    }
    ...    
    template< class T >
    list_inserter& operator,( const T& r ) { //它化腐朽为神奇,逗号(,)从以前的“\标”升U成了“交警?/span>
        insert_( r  );
        return *this;
    }
    ... ...
private:   
    list_inserter& operator=( const list_inserter& );
    Function insert_;  // call_push_back
};
    到这里,W一?v1 += 1 是下面q个样子Q?/pre>
    list_inserter<call_push_back, p>( call_push_back<vector<int> > )( 1 )
    相当?call_push_back(1)Qcall_push_back?c_ 成员是 v1 的引用,q当于 v1.push_back(1) 。第一个元素进入了容器Q鼓掌?/pre>
    看过代码后,逗号Q?, Q就不为奇了, v1 += 1, 2, 30, 40, 500; 句子中的 ?”, 功能是依ơ插入逗号后的元素?/pre>
    l箋探烦新大陆…?/pre>

崇文 2009-12-09 11:01 发表评论
]]>boost 中的 and_ 如何实现短\Qshort-circuit behaviorQ?/title><link>http://m.shnenglu.com/Rich-cw/archive/2009/12/08/102787.html</link><dc:creator>崇文</dc:creator><author>崇文</author><pubDate>Tue, 08 Dec 2009 03:36:00 GMT</pubDate><guid>http://m.shnenglu.com/Rich-cw/archive/2009/12/08/102787.html</guid><wfw:comment>http://m.shnenglu.com/Rich-cw/comments/102787.html</wfw:comment><comments>http://m.shnenglu.com/Rich-cw/archive/2009/12/08/102787.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Rich-cw/comments/commentRss/102787.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Rich-cw/services/trackbacks/102787.html</trackback:ping><description><![CDATA[<p>      它是用递归实现的。首先看下面的句子:</p><pre class="code">mpl::and_<exp1, exp2> </pre><pre class="code"> 它用的是头文g中的模板: </pre><pre class="code">mpl::and_<exp1, exp2, true_, true_, true_> 1_ 2_ 3_ 4_ 5_ </pre><pre class="code"> 然后q个模板l承自: </pre><pre class="code">aux::and_impl< BOOST_MPL_AUX_NESTED_TYPE_WKND(exp1)::value, exp2, 3_, 4_, 5_ > </pre><pre class="code"> ?1_ ?value ?<span style="color: blue">false </span>Q它ql于下面的主模板Q? <span style="color: blue"></span></pre><pre class="code"><span style="color: blue">template</span>< <span style="color: blue">bool </span>C_, <span style="color: blue">typename </span>T1, <span style="color: blue">typename </span>T2, <span style="color: blue">typename </span>T3, <span style="color: blue">typename </span>T4 > <span style="color: blue">struct </span>and_impl : false_ {}; </pre><pre class="code"> 否则Ql前q:</pre><pre class="code">aux::and_impl< BOOST_MPL_AUX_NESTED_TYPE_WKND(exp2)::value, 3_, 4_, 5_, true_ > </pre><pre class="code"> q又回到了上一步的分叉路口。多个参数的情况与此cM。当全部参数都成 ture/true_ Ӟ有了这个特化版本:</pre><pre class="code"><span style="color: blue">template</span><> <span style="color: blue">struct </span>and_impl< <span style="color: blue">true </span>, true_, true_, true_, true_ > : true_ {};</pre><pre class="code"> q样一来,当前一个参Cؓ false Ӟ后面的参数就不用触及 ::value Q也׃会被实例化,q就?short-circuit behaviorQ类g ||, && {操作符?</pre><pre class="code"> 同理可推?or_ 的行为?/pre><img src ="http://m.shnenglu.com/Rich-cw/aggbug/102787.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Rich-cw/" target="_blank">崇文</a> 2009-12-08 11:36 <a href="http://m.shnenglu.com/Rich-cw/archive/2009/12/08/102787.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Little thing about boost::mpl::if_http://m.shnenglu.com/Rich-cw/archive/2009/12/07/102747.html崇文崇文Mon, 07 Dec 2009 12:47:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2009/12/07/102747.htmlhttp://m.shnenglu.com/Rich-cw/comments/102747.htmlhttp://m.shnenglu.com/Rich-cw/archive/2009/12/07/102747.html#Feedback0http://m.shnenglu.com/Rich-cw/comments/commentRss/102747.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/102747.html      q本来没什么好说的Q不q我在强啃了一天《c++ 模板元编E》这本书后,头脑昏沉Q反应迟钝。也借整?boost::mpl::if_ q个让我查阅半天源码的小东西的机会,让自己放松一下?/p>

      if_ 是?if_c 定义了自?::type 的一个元函数。之所以我会对 if_ qLQ是因ؓ我想当然的认?if_ 模板的第一个参数类型ؓ bool 倹{这是受?if 关键字的影响Q也有错误理?if_c 的原因。且?if_c 的声明:

template<
      bool C          // yeah, the type here is bool, but...
    , typename T1
    , typename T2
    >
struct if_c {
    typedef T1 type;
};

template<            // 特化版本Q类D样定义元函数的例子很?
      typename T1
    , typename T2
    >
struct if_c<false,T1,T2> {
    typedef T2 type;
};
  既生 if_c Q何?if_ Q对比二者的代码Qif_ ?if_c 多了如下句子QBOOST_MPL_AUX_LAMBDA_SUPPORT(3,if_,(T1,T2,T3))。而在 MSVCQVS2008Q?~译器上q个句子没v作用。那它就是ؓ了兼容编译器而生的,名字来看和 lambda 表达式的支持性有关系。关于其他编译器的事情,我不知道Q先搁置一辏V再?if_ 的声明:
template<
      typename BOOST_MPL_AUX_NA_PARAM(T1)
    , typename BOOST_MPL_AUX_NA_PARAM(T2)
    , typename BOOST_MPL_AUX_NA_PARAM(T3)
    >
struct if_ {
 private:
    // agurt, 02/jan/03: two-step 'type' definition for the sake of aCC 
    typedef if_c<
#if defined(BOOST_MPL_CFG_BCC_INTEGRAL_CONSTANTS)
          BOOST_MPL_AUX_VALUE_WKND(T1)::value
#else
          BOOST_MPL_AUX_STATIC_CAST(bool, BOOST_MPL_AUX_VALUE_WKND(T1)::value)
#endif
        , T2
        , T3
        > almost_type_;
 
 public:
    typedef typename almost_type_::type type;
    BOOST_MPL_AUX_LAMBDA_SUPPORT(3,if_,(T1,T2,T3))
};
    q就很明了了Q先?if_c 定应该使用 T2 q是 T3Q再来个q回的定义,成?::type 。所以就声明来看Qif_ 的第一个模板参数确实是要求cd。初看时Q我郁闷了,如果我喜Ƣ用 value Q有些习惯连自己也感到奇怪!Q,那我Z么不?if_c 呢?哈,当然可以Q如书上的例子:
template <typename T>
struct param_type 
    : mpl::if_<
               typename boost::is_scalar<T>::type,
               T,
               typename boost::add_reference<T const>::type
    > {};
typedef param_type<float>::type float_;
    完全可以改ؓQ?/pre>
template <typename T>
struct param_type 
    : mpl::if_c<
                boost::is_scalar<T>::value,
                T,
                typename boost::add_reference<T const>::type
    > {};

     对于书上面的后几个简化,也照样适用。我更郁闷了,我前几十分钟q嘛MQ死抓着 if_ 不放Q不考虑试试其它可能。我惌v了昨天对书上某个例子的扩充,实受用Q这当然不是说作者坏话,他是Z使书写得清晰明了才不使用在我看来是优化的东西的。呃Q有炚w…)Q我才再一ơ责怪v自己的读书习惯。古ZQ尽信书不如无书。这句话用到q里是Q别死跟着书走Q哪里跟哪里嘛,风马牛不相及Q哈哈!Q?/p>

     关于 boost Q我不了解的太多Q需要一步一步来Q就从简单的开始吧。现在它可是q远出了我的理解范_希望能赶上一Ҏ一炏V?/p>

崇文 2009-12-07 20:47 发表评论
]]>《Inside Com》小l(一Q?/title><link>http://m.shnenglu.com/Rich-cw/archive/2009/12/05/102618.html</link><dc:creator>崇文</dc:creator><author>崇文</author><pubDate>Sat, 05 Dec 2009 12:16:00 GMT</pubDate><guid>http://m.shnenglu.com/Rich-cw/archive/2009/12/05/102618.html</guid><wfw:comment>http://m.shnenglu.com/Rich-cw/comments/102618.html</wfw:comment><comments>http://m.shnenglu.com/Rich-cw/archive/2009/12/05/102618.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://m.shnenglu.com/Rich-cw/comments/commentRss/102618.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Rich-cw/services/trackbacks/102618.html</trackback:ping><description><![CDATA[<p>      前不久看完了《Inside Com》。书写得非常,有了q个基础Q打l研I《Com  本质论》。下面是我看完此书记录下来的一些东西:</p> <p>      1. CoCreateInstance 的调用过E?q里的组件位?DLL ?Q?/p> <p><a href="http://m.shnenglu.com/images/cppblog_com/Rich-cw/WindowsLiveWriter/InsideCom_12C2C/CoCreateInstance_4.jpg"><img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=CoCreateInstance border=0 alt=CoCreateInstance src="http://m.shnenglu.com/images/cppblog_com/Rich-cw/WindowsLiveWriter/InsideCom_12C2C/CoCreateInstance_thumb_1.jpg" width=504 height=337></a> </p> <p>      囄说明了一切。对比一?CoCreateInstance 的声明:</p> <pre class=code>WINOLEAPI CoCreateInstance(__in REFCLSID rclsid, __in_opt LPUNKNOWN pUnkOuter, __in DWORD dwClsContext, __in REFIID riid, __deref_out LPVOID FAR* ppv);</pre> <pre class=code> 值得一提的?CoGetClassObject 调用?Dll 的导出函?DllGetClassObject QRegsvr32 /s /u 当然是用了对应?DllRegisterServer / DllUnregisterServer 导出函数。如果?ATLQ这一切繁杂的工作都省却了Q但反注册还需手动操作……</pre> <pre class=code> 2. 获取聚合lg接口的过E:</pre> <pre class=code> 理解lg聚合技术是一ơ痛苦的l历。前后三天,苦苦思烦Q我l于在那个阴L下午获得?#8220;安东D?#8221;的垂青。如果他早一点给我一瓶智力药_何至如此呢!看图说话Q?/pre> <pre class=code><a href="http://m.shnenglu.com/images/cppblog_com/Rich-cw/WindowsLiveWriter/InsideCom_12C2C/aggrgation_2.jpg"><img style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; BORDER-TOP: 0px; BORDER-RIGHT: 0px" title=aggrgation border=0 alt=aggrgation src="http://m.shnenglu.com/images/cppblog_com/Rich-cw/WindowsLiveWriter/InsideCom_12C2C/aggrgation_thumb.jpg" width=754 height=259></a> </pre> <a ></a> <p>      其中Q两个InUnk都是非代理IUnknown?/p> <p>      q样一来,对于外部lg来说Q?/p> <table border=0 cellSpacing=0 cellPadding=2 width=248> <tbody> <tr> <td vAlign=top width=34> </td> <td vAlign=top width=25>IX</td> <td vAlign=top width=187>  Outer->QueryInterface</td> </tr> <tr> <td vAlign=top width=34>获取</td> <td vAlign=top width=25> </td> <td vAlign=top width=187> </td> </tr> <tr> <td vAlign=top width=34> </td> <td vAlign=top width=25>IY</td> <td vAlign=top width=187>  InUnkO->QueryInterface</td> </tr> </tbody> </table> <p>      对于内部lg来说:</p> <table border=0 cellSpacing=0 cellPadding=2 width=435> <tbody> <tr> <td vAlign=top width=34> </td> <td vAlign=top width=25>IX</td> <td vAlign=top width=374>  OutUnk->QueryInterface->InUnkO->QueryInterface</td> </tr> <tr> <td vAlign=top width=34>获取</td> <td vAlign=top width=25> </td> <td vAlign=top width=374> </td> </tr> <tr> <td vAlign=top width=34> </td> <td vAlign=top width=25>IY</td> <td vAlign=top width=374>  OutUnk->QueryInterface</td> </tr> </tbody> </table> <p> </p> <p>      所有的 QueryInterface 动作都直接或间接的由外部lg执行?/p> <p>      q让我想起了《变形金?》。假如我一q电影院q着了,{到天火和擎天柱l合之后我才醒来。那么对于我来说Q擎天柱是外部lgQIX。天火ؓ内部lgQIY为飞。此时我不知道有天火的存在。当我问擎天׃会飞吗的时候,他知道他不会飞(没有IY接口Q,于是他问天火你会不会飞,天火p回IYQ飞Q接口,于是擎天柱告诉我他会飞。当他飞着的时候(我想通过IY来查询IXQ,我问他会跑么Q这句话只有天火听得见(因ؓ我用的是IY接口Q,天火不会跑,他就问擎天柱Q擎天柱知道自己会跑Q有IX接口Q,于是q我一个IX接口。整个过E中Q我只知道擎天柱Qƈ不知道天火的存在Q于是擎天柱聚合了天火?/p> <p>      但我怎么可能q去q着了呢Q?/p> <p>      内部lg的代理接口只是简单的调用外部的QueryInterfaceQ是个{发站Q一切工作交由外部组件统{V关于具体实玎ͼ《COM本质论》中l出?"impunk.h" 头文件中有很好的实现。我非常喜欢其中关于接口查找表的实现Q那个offset获取偏移地址的方法让我这个C语言功力薄弱的h感到十分惭愧?/p> <p>      q两天再把套间线E和自由U程整理出来?/p> <img src ="http://m.shnenglu.com/Rich-cw/aggbug/102618.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Rich-cw/" target="_blank">崇文</a> 2009-12-05 20:16 <a href="http://m.shnenglu.com/Rich-cw/archive/2009/12/05/102618.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Boost 中的 BOOST_STATIC_ASSERThttp://m.shnenglu.com/Rich-cw/archive/2009/12/04/102530.html崇文崇文Fri, 04 Dec 2009 05:05:00 GMThttp://m.shnenglu.com/Rich-cw/archive/2009/12/04/102530.htmlhttp://m.shnenglu.com/Rich-cw/comments/102530.htmlhttp://m.shnenglu.com/Rich-cw/archive/2009/12/04/102530.html#Feedback1http://m.shnenglu.com/Rich-cw/comments/commentRss/102530.htmlhttp://m.shnenglu.com/Rich-cw/services/trackbacks/102530.html先看定义在头文g中的语句Q?/font>
template <bool x> struct STATIC_ASSERTION_FAILURE;

template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };

template<int x> struct static_assert_test{};

?/span>
使用的是昄转换
// Note that the argument to the assert is explicitly cast to bool using old-
// style casts: too many compilers currently have problems with static_cast
// when used inside integral constant expressions.
?/span>
#elif defined(BOOST_MSVC)
#define BOOST_STATIC_ASSERT( B ) \
   typedef ::boost::static_assert_test<\
      sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\ // ***********
         BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
关键的地方在?sizeof() 中的句子。当 (bool)( B ) ?false Ӟ因ؓ没有q个特化版本Q编译器报错,?/font>
?b>Beyond the C++ Standard Library: An Introduction to Boost》中所qͼ
Error: use of undefined type
'boost::STATIC_ASSERTION_FAILURE<false>'
BOOST_JOIN(X, Y) 最l解释ؓ X##Y。__COUNTER__ ?MSDN 中的描述Q?/pre>
Expands to an integer starting with 0 and incrementing by 1 every time it is used in a compiland.
每用一ơ它增1?/pre>

崇文 2009-12-04 13:05 发表评论
]]> þþ97ɫ| պƷþþվ| ҹþþƷ| ɫۺϾþĻ| AVһȾþ| þþƷ99͵| þþƷŷպ| þþþAVרɫ| ޾Ʒþþwww| AVվþþƷ| ݺɫۺϾþ| ľƷ99þù | þҹ³˿Ƭ| ҹþӰԺ| ľƷþþþþò| 99þѹƷ| &#228;v뾫Ʒþþ| ھƷžžþþƷ| ھƷþ| þֻƷ99| þƵ1| þ99ƷþþþþҰ | þZYZԴվĶ| ƷȾþþø| 91ƷɫۺϾþ| þùAV䡪ٶ| պݺݾþ͵͵ɫۺ| Ʒþþþþþ| ˾þô߽ۺ5g| 㽶97þ| ھƷþþþav| þþþ޹| Ʒþһ| ĻþþƷAPP| ŷպƷþþþ| ĻþþƷ| þþþһƷ | һɫۺϾþ| ƷþþþþҰ | ͵ٸþþþþþþ| ƷѾþþþþþþ|