﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>麦田的小黑屋</title>
	<atom:link href="http://blog.mtian.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mtian.net</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 10:09:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>avpbase.dll的一些学习笔记</title>
		<link>http://blog.mtian.net/2011/12/avpbase-dll/</link>
		<comments>http://blog.mtian.net/2011/12/avpbase-dll/#comments</comments>
		<pubDate>Sun, 11 Dec 2011 10:08:14 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[学习笔记]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=452</guid>
		<description><![CDATA[avpbase.dll的一点学习笔记 http://www.krnl.info/thread-4832-1-1.html avpbase.dll的学习笔记（2）CBase::PrepareFileRecords http://www.krnl.info/thread-4835-1-1.html avpbase.dll的一点学习笔记（3）CLink http://www.krnl.info/thread-4839-1-1.html avpbase.dll的一点学习笔记（4）CBase::DoRecords http://www.krnl.info/thread-4840-1-1.html 简而言之，卡巴的病毒库中有6种记录，其中BT_RECORD和BT_LINK32较为重要，BT_RECORD下又分为8个子类型，其中RT_FILE最为重要，另外还有RT_UNPACK和RT_EXTRACT类型。卡巴对于RT_FILE类型的记录有特别的优化，一条RT_FILE为一个R_File结构，卡巴加载库后会把一个R_File 分割为R_File_Short、R_File_Cutted和R_File_CureData三个小结构放到数组里，呃，RT_FILE也有8个子类型的。BT_LINK32为卡巴的NB所在，CLink充当了连接器的作用，把COFF格式的OBJ代码加载到内存中，并进行重定位等操作，使之可以像加载DLL一样从病毒库中加载可执行代码，而卡巴的一些核心代码比如_GetFirstEntry和_GetNextEntry就在库文件中，另外卡巴的那些脱壳和解密代码也在库文件中，因为病毒库的代码并没有泄漏，所以也无从得知_GetFirstEntry这个后面会提到的重要函数的代码内容。卡巴在对文件查毒的时候，会把文件看作一个个Entry，把每个Entry读入到缓存中，而后对缓存进行查毒。查毒的过程可见我在上面提到的帖子，主要根据病毒库指定偏移取一定长度的数据作HASH后比较HASH是否一样，有指定Link则将代码执行起来。 可能有不对的地方，还请指正，呵呵。]]></description>
			<content:encoded><![CDATA[<p>avpbase.dll的一点学习笔记 <a href="http://www.krnl.info/thread-4832-1-1.html">http://www.krnl.info/thread-4832-1-1.html</a><br />
avpbase.dll的学习笔记（2）CBase::PrepareFileRecords <a href="http://www.krnl.info/thread-4832-1-1.html">http://www.krnl.info/thread-4835-1-1.html</a><br />
avpbase.dll的一点学习笔记（3）CLink <a href="http://www.krnl.info/thread-4839-1-1.html">http://www.krnl.info/thread-4839-1-1.html</a><br />
avpbase.dll的一点学习笔记（4）CBase::DoRecords  <a href="http://www.krnl.info/thread-4840-1-1.html">http://www.krnl.info/thread-4840-1-1.html</a></p>
<p>简而言之，卡巴的病毒库中有6种记录，其中BT_RECORD和BT_LINK32较为重要，BT_RECORD下又分为8个子类型，其中RT_FILE最为重要，另外还有RT_UNPACK和RT_EXTRACT类型。卡巴对于RT_FILE类型的记录有特别的优化，一条RT_FILE为一个R_File结构，卡巴加载库后会把一个R_File 分割为R_File_Short、R_File_Cutted和R_File_CureData三个小结构放到数组里，呃，RT_FILE也有8个子类型的。BT_LINK32为卡巴的NB所在，CLink充当了连接器的作用，把COFF格式的OBJ代码加载到内存中，并进行重定位等操作，使之可以像加载DLL一样从病毒库中加载可执行代码，而卡巴的一些核心代码比如_GetFirstEntry和_GetNextEntry就在库文件中，另外卡巴的那些脱壳和解密代码也在库文件中，因为病毒库的代码并没有泄漏，所以也无从得知_GetFirstEntry这个后面会提到的重要函数的代码内容。卡巴在对文件查毒的时候，会把文件看作一个个Entry，把每个Entry读入到缓存中，而后对缓存进行查毒。查毒的过程可见我在上面提到的帖子，主要根据病毒库指定偏移取一定长度的数据作HASH后比较HASH是否一样，有指定Link则将代码执行起来。</p>
<p>可能有不对的地方，还请指正，呵呵。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/12/avpbase-dll/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>迅雷客户端漏洞三个</title>
		<link>http://blog.mtian.net/2011/11/thunderwul/</link>
		<comments>http://blog.mtian.net/2011/11/thunderwul/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 16:26:25 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[胡思乱想]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=446</guid>
		<description><![CDATA[无意间发现的迅雷客户端几个漏洞，都是dll hijacking类型的。 迅雷客户端有几个版本在启动的时候有几个DLL路径没有写好，所以会导致加载失败。 因为迅雷是能打开种子的，那么当迅雷启动时，会把种子的目录作为程序的当前路径，如果种子目录下刚好有那几个名字DLL的话，DLL就会被LoadLibrary，DllMain中的代码就会被执行。所以，你如果把种子和含有恶意代码的DLL一起打包发给别人的话，打中的几率还是很高的。不过迅雷现在似乎会推动最新版本的升级补丁。 我测试到的迅雷版本和DLL名。 迅雷版本号：7.2.1.3136 进程名：ThunderPlatform.exe 1.1.2.76 加载失败的DLL名： .dll (没错，就是.dll) 迅雷版本号：7.2.1.3136 进程名：XBrowser.exe 1.0.0.1 加载失败的DLL名：呃，我记得似乎是ieframe.dll，怎么今天没试出来，算了，不确认了，反正已经被补掉了。 迅雷版本号:7.2.3.3244 进程名：ThunderPlatform.exe 加载失败的DLL名：xlnet_manager.dll 3136版本的那两个漏洞，通过一个在迅雷工作的朋友报了上去，后来朋友跟我说修复了，我又下载了3244版本的瞧了几眼，发现。。。。，又出来一个新的，于是报乌云上去了：http://wooyun.org/bugs/wooyun-2011-02980。今天乌云上过了30天自动公开了，我也懒得去确认迅雷最新版本到底修改没。有兴趣的人可以再瞧下，呵呵。]]></description>
			<content:encoded><![CDATA[<p>无意间发现的迅雷客户端几个漏洞，都是dll hijacking类型的。<br />
迅雷客户端有几个版本在启动的时候有几个DLL路径没有写好，所以会导致加载失败。<br />
因为迅雷是能打开种子的，那么当迅雷启动时，会把种子的目录作为程序的当前路径，如果种子目录下刚好有那几个名字DLL的话，DLL就会被LoadLibrary，DllMain中的代码就会被执行。所以，你如果把种子和含有恶意代码的DLL一起打包发给别人的话，打中的几率还是很高的。不过迅雷现在似乎会推动最新版本的升级补丁。</p>
<p><a href="http://blog.mtian.net/wp-content/uploads/2011/11/120134169e0a6db8c908d4d49086ac0603be56c2.jpg"><img src="http://blog.mtian.net/wp-content/uploads/2011/11/120134169e0a6db8c908d4d49086ac0603be56c2.jpg" alt="迅雷漏洞" title="120134169e0a6db8c908d4d49086ac0603be56c2" width="670" height="454" class="aligncenter size-full wp-image-449" /></a></p>
<p>我测试到的迅雷版本和DLL名。<br />
迅雷版本号：7.2.1.3136<br />
进程名：ThunderPlatform.exe  1.1.2.76<br />
加载失败的DLL名：  .dll  (没错，就是.dll)</p>
<p>迅雷版本号：7.2.1.3136<br />
进程名：XBrowser.exe  1.0.0.1<br />
加载失败的DLL名：呃，我记得似乎是ieframe.dll，怎么今天没试出来，算了，不确认了，反正已经被补掉了。</p>
<p>迅雷版本号:7.2.3.3244<br />
进程名：ThunderPlatform.exe<br />
加载失败的DLL名：xlnet_manager.dll</p>
<p>3136版本的那两个漏洞，通过一个在迅雷工作的朋友报了上去，后来朋友跟我说修复了，我又下载了3244版本的瞧了几眼，发现。。。。，又出来一个新的，于是报乌云上去了：http://wooyun.org/bugs/wooyun-2011-02980。今天乌云上过了30天自动公开了，我也懒得去确认迅雷最新版本到底修改没。有兴趣的人可以再瞧下，呵呵。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/11/thunderwul/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>《STL源码剖析》学习笔记(4)</title>
		<link>http://blog.mtian.net/2011/10/stlxuexibiji4/</link>
		<comments>http://blog.mtian.net/2011/10/stlxuexibiji4/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 17:14:14 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[源码]]></category>
		<category><![CDATA[笔记]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=442</guid>
		<description><![CDATA[20，STL仿函数需要定义相应的型别，以便让配接器取出，以此而拥有配接能力，相应型别只是一些typedef，所有操作在编译期全部完成，对程序的执行效率没有影响。仿函数的相应型别主要用来表现函数参数型别和传回值，为了方便，STL已经定义了两个class，代码如下： template &#60;class Arg, class Result&#62; struct unary_function &#123; typedef Arg argument_type; typedef Result result_type; &#125;; &#160; template &#60;class Arg1, class Arg2, class Result&#62; struct binary_function &#123; typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; typedef Result result_type; &#125;; 21，仿函数只是行为类似函数的对象，自定义了operator()操作符。以下为几个仿函数的代码： template &#60;class T&#62; struct logical_not : public unary_function&#60;T, bool&#62; &#123; bool operator&#40;&#41;&#40;const T&#38; x&#41; const &#123; return [...]]]></description>
			<content:encoded><![CDATA[<p>20，STL仿函数需要定义相应的型别，以便让配接器取出，以此而拥有配接能力，相应型别只是一些typedef，所有操作在编译期全部完成，对程序的执行效率没有影响。仿函数的相应型别主要用来表现函数参数型别和传回值，为了方便，STL已经定义了两个class，代码如下：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Arg, <span style="color: #0000ff;">class</span> Result<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> unary_function <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">typedef</span> Arg argument_type<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">typedef</span> Result result_type<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Arg1, <span style="color: #0000ff;">class</span> Arg2, <span style="color: #0000ff;">class</span> Result<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> binary_function <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">typedef</span> Arg1 first_argument_type<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">typedef</span> Arg2 second_argument_type<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">typedef</span> Result result_type<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><span id="more-442"></span><br />
21，仿函数只是行为类似函数的对象，自定义了operator()操作符。以下为几个仿函数的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> logical_not <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> unary_function<span style="color: #000080;">&lt;</span>T, <span style="color: #0000ff;">bool</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">bool</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #000040;">!</span>x<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> plus <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> binary_function<span style="color: #000080;">&lt;</span>T, T, T<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
    T operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> y<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> x <span style="color: #000040;">+</span> y<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>22，每一个fuction adapters内也有一个member object，其型别等同于它要配接的对象，以下为几个函数配接器的源码：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Operation<span style="color: #000080;">&gt;</span> 
<span style="color: #0000ff;">class</span> binder1st
  <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> unary_function<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">second_argument_type</span>,
                          <span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">result_type</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
  Operation op<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">first_argument_type</span> value<span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  binder1st<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Operation<span style="color: #000040;">&amp;</span> x,
            <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">first_argument_type</span><span style="color: #000040;">&amp;</span> y<span style="color: #008000;">&#41;</span>
      <span style="color: #008080;">:</span> op<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span>, value<span style="color: #008000;">&#40;</span>y<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">result_type</span>
  operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">second_argument_type</span><span style="color: #000040;">&amp;</span> x<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> op<span style="color: #008000;">&#40;</span>value, x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Operation, <span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> binder1st<span style="color: #000080;">&lt;</span>Operation<span style="color: #000080;">&gt;</span> bind1st<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Operation<span style="color: #000040;">&amp;</span> op, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Operation<span style="color: #008080;">::</span><span style="color: #007788;">first_argument_type</span> arg1_type<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> binder1st<span style="color: #000080;">&lt;</span>Operation<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>op, arg1_type<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">//--------------------------------------</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Arg, <span style="color: #0000ff;">class</span> Result<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> pointer_to_unary_function <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> unary_function<span style="color: #000080;">&lt;</span>Arg, Result<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
  Result <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
  pointer_to_unary_function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">explicit</span> pointer_to_unary_function<span style="color: #008000;">&#40;</span>Result <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> ptr<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
  Result operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg x<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> ptr<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Arg, <span style="color: #0000ff;">class</span> Result<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> pointer_to_unary_function<span style="color: #000080;">&lt;</span>Arg, Result<span style="color: #000080;">&gt;</span> ptr_fun<span style="color: #008000;">&#40;</span>Result <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> pointer_to_unary_function<span style="color: #000080;">&lt;</span>Arg, Result<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Arg1, <span style="color: #0000ff;">class</span> Arg2, <span style="color: #0000ff;">class</span> Result<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> pointer_to_binary_function <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> binary_function<span style="color: #000080;">&lt;</span>Arg1, Arg2, Result<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
    Result <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>ptr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg1, Arg2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    pointer_to_binary_function<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">explicit</span> pointer_to_binary_function<span style="color: #008000;">&#40;</span>Result <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg1, Arg2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> ptr<span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span>
    Result operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg1 x, Arg2 y<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> ptr<span style="color: #008000;">&#40;</span>x, y<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Arg1, <span style="color: #0000ff;">class</span> Arg2, <span style="color: #0000ff;">class</span> Result<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> pointer_to_binary_function<span style="color: #000080;">&lt;</span>Arg1, Arg2, Result<span style="color: #000080;">&gt;</span> 
ptr_fun<span style="color: #008000;">&#40;</span>Result <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>Arg1, Arg2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> pointer_to_binary_function<span style="color: #000080;">&lt;</span>Arg1, Arg2, Result<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>23，一个使用mem_fun的示例代码：</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Shape <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> draw<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Circle <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> draw<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Circle::Draw()&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Square <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Shape <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> draw<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Square::Draw()&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	vector<span style="color: #000080;">&lt;</span>Shape<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> vs<span style="color: #008080;">;</span>
	vs.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> Circle<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	vs.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> Square<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	for_each<span style="color: #008000;">&#40;</span>vs.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, vs.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, mem_fun<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>Shape<span style="color: #008080;">::</span><span style="color: #007788;">draw</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/10/stlxuexibiji4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《STL源码剖析》学习笔记(3)</title>
		<link>http://blog.mtian.net/2011/10/stlxuexibiji3/</link>
		<comments>http://blog.mtian.net/2011/10/stlxuexibiji3/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 16:18:03 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[源码]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=438</guid>
		<description><![CDATA[18，copy算法利用函数重载，对char* 和wchar_t* 的操作直接memmove，利用模版特化对于有trivial operator=的操作memmove，对于RandomAccessIterator通过头尾间隔来确定循环次数，对于InputIterator通过不断累加是否到达last来确定循环次数。 template &#60;class InputIterator, class OutputIterator&#62; inline OutputIterator __copy&#40;InputIterator first, InputIterator last, OutputIterator result, input_iterator_tag&#41; &#123; for &#40; ; first != last; ++result, ++first&#41; *result = *first; return result; &#125; template &#60;class RandomAccessIterator, class OutputIterator, class Distance&#62; inline OutputIterator __copy_d&#40;RandomAccessIterator first, RandomAccessIterator last, OutputIterator result, Distance*&#41; &#123; //通过last - first来确定循环次数，比上面的那个快一些 for &#40;Distance [...]]]></description>
			<content:encoded><![CDATA[<p>18，copy算法利用函数重载，对char* 和wchar_t* 的操作直接memmove，利用模版特化对于有trivial operator=的操作memmove，对于RandomAccessIterator通过头尾间隔来确定循环次数，对于InputIterator通过不断累加是否到达last来确定循环次数。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> InputIterator, <span style="color: #0000ff;">class</span> OutputIterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> OutputIterator __copy<span style="color: #008000;">&#40;</span>InputIterator first, InputIterator last,
	OutputIterator result, input_iterator_tag<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #008080;">;</span> first <span style="color: #000040;">!</span><span style="color: #000080;">=</span> last<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>result, <span style="color: #000040;">++</span>first<span style="color: #008000;">&#41;</span>
		<span style="color: #000040;">*</span>result <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>first<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><span id="more-438"></span></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator, <span style="color: #0000ff;">class</span> OutputIterator, <span style="color: #0000ff;">class</span> Distance<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> OutputIterator
	__copy_d<span style="color: #008000;">&#40;</span>RandomAccessIterator first, RandomAccessIterator last,
	OutputIterator result, Distance<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">//通过last - first来确定循环次数，比上面的那个快一些</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>Distance n <span style="color: #000080;">=</span> last <span style="color: #000040;">-</span> first<span style="color: #008080;">;</span> n <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>n, <span style="color: #000040;">++</span>result, <span style="color: #000040;">++</span>first<span style="color: #008000;">&#41;</span> 
		<span style="color: #000040;">*</span>result <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>first<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator, <span style="color: #0000ff;">class</span> OutputIterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> OutputIterator 
	__copy<span style="color: #008000;">&#40;</span>RandomAccessIterator first, RandomAccessIterator last,
	OutputIterator result, random_access_iterator_tag<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> __copy_d<span style="color: #008000;">&#40;</span>first, last, result, distance_type<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> InputIterator, <span style="color: #0000ff;">class</span> OutputIterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> __copy_dispatch
<span style="color: #008000;">&#123;</span>
	OutputIterator operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>InputIterator first, InputIterator last,
		OutputIterator result<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0000ff;">return</span> __copy<span style="color: #008000;">&#40;</span>first, last, result, iterator_category<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION </span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> T<span style="color: #000040;">*</span> __copy_t<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> first, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> last, T<span style="color: #000040;">*</span> result, __true_type<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">memmove</span><span style="color: #008000;">&#40;</span>result, first, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> result <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> T<span style="color: #000040;">*</span> __copy_t<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> first, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> last, T<span style="color: #000040;">*</span> result, __false_type<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> __copy_d<span style="color: #008000;">&#40;</span>first, last, result, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">ptrdiff_t</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> __copy_dispatch<span style="color: #000080;">&lt;</span>T<span style="color: #000040;">*</span>, T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span>
<span style="color: #008000;">&#123;</span>
	T<span style="color: #000040;">*</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>T<span style="color: #000040;">*</span> first, T<span style="color: #000040;">*</span> last, T<span style="color: #000040;">*</span> result<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> __type_traits<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">has_trivial_assignment_operator</span> t<span style="color: #008080;">;</span> 
		<span style="color: #0000ff;">return</span> __copy_t<span style="color: #008000;">&#40;</span>first, last, result, t<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> __copy_dispatch<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span>, T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span>
<span style="color: #008000;">&#123;</span>
	T<span style="color: #000040;">*</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> first, <span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span> last, T<span style="color: #000040;">*</span> result<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> __type_traits<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">has_trivial_assignment_operator</span> t<span style="color: #008080;">;</span> 
		<span style="color: #0000ff;">return</span> __copy_t<span style="color: #008000;">&#40;</span>first, last, result, t<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> InputIterator, <span style="color: #0000ff;">class</span> OutputIterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> OutputIterator copy<span style="color: #008000;">&#40;</span>InputIterator first, InputIterator last,
	OutputIterator result<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">return</span> __copy_dispatch<span style="color: #000080;">&lt;</span>InputIterator,OutputIterator<span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>first, last, result<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> copy<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> first, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> last, <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> result<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000dd;">memmove</span><span style="color: #008000;">&#40;</span>result, first, last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> result <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">wchar_t</span><span style="color: #000040;">*</span> copy<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">wchar_t</span><span style="color: #000040;">*</span> first, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">wchar_t</span><span style="color: #000040;">*</span> last,
	<span style="color: #0000ff;">wchar_t</span><span style="color: #000040;">*</span> result<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">memmove</span><span style="color: #008000;">&#40;</span>result, first, <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">wchar_t</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000ff;">return</span> result <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>19，sort算法，SGI中采用了IntroSort，其行为在大部分情况下几乎与median-of-3 Quick Sort完全相同，但是当分割行为有恶化为二次行为的倾向时，能够自我侦测，转而改用Heap Sort，又比一开始就使用Heap Sort来的好。SGI的sort采用__lg(Size n)来确定递归调用次数，一旦调用次数大于这个的时候就判定为恶化，然后调用Heap Sort。当元素个数少于16个的时候就会跳出Quick Sort，等待最后的插入排序。插入排序中还有一些小技巧，具体见源码。</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator, <span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> __linear_insert<span style="color: #008000;">&#40;</span>RandomAccessIterator first, 
                            RandomAccessIterator last, T<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  T value <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>last<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&lt;</span> <span style="color: #000040;">*</span>first<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>  <span style="color: #666666;">//尾大于头，直接移动</span>
    copy_backward<span style="color: #008000;">&#40;</span>first, last, last <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #000040;">*</span>first <span style="color: #000080;">=</span> value<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">else</span>
    __unguarded_linear_insert<span style="color: #008000;">&#40;</span>last, value<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator, <span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span> __unguarded_linear_insert<span style="color: #008000;">&#40;</span>RandomAccessIterator last, T value<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  RandomAccessIterator next <span style="color: #000080;">=</span> last<span style="color: #008080;">;</span>
  <span style="color: #000040;">--</span>next<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>value <span style="color: #000080;">&lt;</span> <span style="color: #000040;">*</span>next<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #000040;">*</span>last <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>next<span style="color: #008080;">;</span>
    last <span style="color: #000080;">=</span> next<span style="color: #008080;">;</span>
    <span style="color: #000040;">--</span>next<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #000040;">*</span>last <span style="color: #000080;">=</span> value<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span> __final_insertion_sort<span style="color: #008000;">&#40;</span>RandomAccessIterator first, 
                            RandomAccessIterator last<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first <span style="color: #000080;">&gt;</span> __stl_threshold<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    __insertion_sort<span style="color: #008000;">&#40;</span>first, first <span style="color: #000040;">+</span> __stl_threshold<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    __unguarded_insertion_sort<span style="color: #008000;">&#40;</span>first <span style="color: #000040;">+</span> __stl_threshold, last<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">else</span>
    __insertion_sort<span style="color: #008000;">&#40;</span>first, last<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Size<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> Size __lg<span style="color: #008000;">&#40;</span>Size n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  Size k<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>k <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> n <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> n <span style="color: #000080;">&gt;&gt;=</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">++</span>k<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">return</span> k<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator, <span style="color: #0000ff;">class</span> T, <span style="color: #0000ff;">class</span> Size<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span> __introsort_loop<span style="color: #008000;">&#40;</span>RandomAccessIterator first,
                      RandomAccessIterator last, T<span style="color: #000040;">*</span>,
                      Size depth_limit<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first <span style="color: #000080;">&gt;</span> __stl_threshold<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">//元素个数少于16个话就跳出introsort</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>depth_limit <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>  <span style="color: #666666;">//调用次数太多，判定为恶化，调用Heap Sort</span>
      partial_sort<span style="color: #008000;">&#40;</span>first, last, last<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #000040;">--</span>depth_limit<span style="color: #008080;">;</span>
    RandomAccessIterator cut <span style="color: #000080;">=</span> __unguarded_partition
      <span style="color: #008000;">&#40;</span>first, last, T<span style="color: #008000;">&#40;</span>__median<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>first, <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>first <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>,
                               <span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    __introsort_loop<span style="color: #008000;">&#40;</span>cut, last, value_type<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span>, depth_limit<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    last <span style="color: #000080;">=</span> cut<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> sort<span style="color: #008000;">&#40;</span>RandomAccessIterator first, RandomAccessIterator last<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>first <span style="color: #000040;">!</span><span style="color: #000080;">=</span> last<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">//先调用introsort，排序排个差不多，然后调用insertion_sort</span>
    __introsort_loop<span style="color: #008000;">&#40;</span>first, last, value_type<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span>, __lg<span style="color: #008000;">&#40;</span>last <span style="color: #000040;">-</span> first<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    __final_insertion_sort<span style="color: #008000;">&#40;</span>first, last<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/10/stlxuexibiji3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《STL源码剖析》学习笔记(2)</title>
		<link>http://blog.mtian.net/2011/10/stlxuexibiji2/</link>
		<comments>http://blog.mtian.net/2011/10/stlxuexibiji2/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 16:02:25 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[源码]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=430</guid>
		<description><![CDATA[12，set、map、multiset、multimap底层均以红黑树实现。红黑树是一种平衡二叉搜索树，平衡二叉搜索树比失去平衡的二叉搜索树来说插入和删除的时间长，但是查找速度快，红黑树相对普通平衡二叉搜索树来说据说统计性能比较好。红色树的迭代器内含一个节点的指针，按照一定的规则来移动，是双向迭代器。跟list一样，每个都是个节点，所以插入不会导致其他迭代器失效，内存也是随用随申请和释放。 13，二叉搜索树具有对数平均时间的表现，但是这样的表现构造在输入数据具有足够随机性的假设上。hashtable的数据结构在插入、删除、搜索等操作上也具有常数平均时间的表现，而且这种表现是以统计为基础，不需要依赖输入数据的随机性。 14，SGI STL的hashtable采用开链法来实现hashtable,用一个vector来存放bucket，bucket内含一个链表来存放元素。与红黑树实现的容器不同的是，hashtable内的元素并不是有序的。hashtable的迭代器内含容器的指针，和当前元素的指针，依照一定的规则向前移动，注意，这是一个前向迭代器，不能向前移动。还是节点型的老规矩，插入不会导致迭代器失效，节点的内存随用随申请和释放，但是存放bucket的vector的内存不会变小和释放。 15，hashtable存放无法处理基本类型外的数据，用户需要自己定义hash fuction。 16，STL只规范复杂度与接口，并不规范实现方法。 17，hash_set、hash_map、hash_multiset、hash_multimap均以hashtable来实现。]]></description>
			<content:encoded><![CDATA[<p>12，set、map、multiset、multimap底层均以红黑树实现。红黑树是一种平衡二叉搜索树，平衡二叉搜索树比失去平衡的二叉搜索树来说插入和删除的时间长，但是查找速度快，红黑树相对普通平衡二叉搜索树来说据说统计性能比较好。红色树的迭代器内含一个节点的指针，按照一定的规则来移动，是双向迭代器。跟list一样，每个都是个节点，所以插入不会导致其他迭代器失效，内存也是随用随申请和释放。</p>
<p>13，二叉搜索树具有对数平均时间的表现，但是这样的表现构造在输入数据具有足够随机性的假设上。hashtable的数据结构在插入、删除、搜索等操作上也具有常数平均时间的表现，而且这种表现是以统计为基础，不需要依赖输入数据的随机性。<br />
<span id="more-430"></span><br />
14，SGI STL的hashtable采用开链法来实现hashtable,用一个vector来存放bucket，bucket内含一个链表来存放元素。与红黑树实现的容器不同的是，hashtable内的元素并不是有序的。hashtable的迭代器内含容器的指针，和当前元素的指针，依照一定的规则向前移动，注意，这是一个前向迭代器，不能向前移动。还是节点型的老规矩，插入不会导致迭代器失效，节点的内存随用随申请和释放，但是存放bucket的vector的内存不会变小和释放。</p>
<p>15，hashtable存放无法处理基本类型外的数据，用户需要自己定义hash fuction。</p>
<p>16，STL只规范复杂度与接口，并不规范实现方法。</p>
<p>17，hash_set、hash_map、hash_multiset、hash_multimap均以hashtable来实现。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/10/stlxuexibiji2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>《STL源码剖析》学习笔记(1)</title>
		<link>http://blog.mtian.net/2011/10/stlxuexibiji/</link>
		<comments>http://blog.mtian.net/2011/10/stlxuexibiji/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 17:06:38 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[学习笔记]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[源码剖析]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=422</guid>
		<description><![CDATA[1，STL的价值：①，一套可复用的封装了常用算法与数据结构的代码，②，高层次的，以泛型思维为基础的、系统化的、条理分明的“软件组件分类学”。 2，STL的六大组件：容器、算法、迭代器、仿函数、适配器和空间配置器 3，STL allocator将内存申请/释放与对象的构造和析构这两个阶段操作区分开来，内存配置操作由alloc::allocate()负责，内存释放由alloc::deallocate()负责；对象构造由::construct()负责，对象析构由::destory()负责。 4，SGI STL有两级的配置器。第一级直接调用malloc(),free(),realloc()来进行内存配置。第二级配置器在大于128字节的操作时直接调用第一级配置器，小于128字节的为了减少内存碎片和额外负担直接的则从内存池中分配。（需要注意的是内存池中的内存不足时会从系统申请，但是STL返回给内存池的内存并不会从内存池返还给系统，没有看到明确的函数来显式调用使内存池返还内存。VC的STL没有使用内存池。） 5,Traits编程技法 struct input_iterator_tag &#123;&#125;; struct output_iterator_tag &#123;&#125;; struct forward_iterator_tag : public input_iterator_tag &#123;&#125;; struct bidirectional_iterator_tag : public forward_iterator_tag &#123;&#125;; struct random_access_iterator_tag : public bidirectional_iterator_tag &#123;&#125;; &#160; &#160; template &#60;class Iterator&#62; struct iterator_traits &#123; typedef typename Iterator::iterator_category iterator_category; typedef typename Iterator::value_type value_type; typedef typename Iterator::difference_type difference_type; typedef typename Iterator::pointer pointer; [...]]]></description>
			<content:encoded><![CDATA[<p>1，STL的价值：①，一套可复用的封装了常用算法与数据结构的代码，②，高层次的，以泛型思维为基础的、系统化的、条理分明的“软件组件分类学”。</p>
<p>2，STL的六大组件：容器、算法、迭代器、仿函数、适配器和空间配置器</p>
<p>3，STL allocator将内存申请/释放与对象的构造和析构这两个阶段操作区分开来，内存配置操作由alloc::allocate()负责，内存释放由alloc::deallocate()负责；对象构造由::construct()负责，对象析构由::destory()负责。</p>
<p>4，SGI STL有两级的配置器。第一级直接调用malloc(),free(),realloc()来进行内存配置。第二级配置器在大于128字节的操作时直接调用第一级配置器，小于128字节的为了减少内存碎片和额外负担直接的则从内存池中分配。（需要注意的是内存池中的内存不足时会从系统申请，但是STL返回给内存池的内存并不会从内存池返还给系统，没有看到明确的函数来显式调用使内存池返还内存。VC的STL没有使用内存池。）</p>
<p>5,Traits编程技法 <span id="more-422"></span></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> input_iterator_tag <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">struct</span> output_iterator_tag <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">struct</span> forward_iterator_tag <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> input_iterator_tag <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">struct</span> bidirectional_iterator_tag <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> forward_iterator_tag <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">struct</span> random_access_iterator_tag <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> bidirectional_iterator_tag <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Iterator<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> iterator_traits <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Iterator<span style="color: #008080;">::</span><span style="color: #007788;">iterator_category</span> iterator_category<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Iterator<span style="color: #008080;">::</span><span style="color: #007788;">value_type</span>        value_type<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Iterator<span style="color: #008080;">::</span><span style="color: #007788;">difference_type</span>   difference_type<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Iterator<span style="color: #008080;">::</span><span style="color: #007788;">pointer</span>           pointer<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> Iterator<span style="color: #008080;">::</span><span style="color: #007788;">reference</span>         reference<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> iterator_traits<span style="color: #000080;">&lt;</span>T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">typedef</span> random_access_iterator_tag iterator_category<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> T                          value_type<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">ptrdiff_t</span>                  difference_type<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> T<span style="color: #000040;">*</span>                         pointer<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> T<span style="color: #000040;">&amp;</span>                         reference<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> iterator_traits<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">typedef</span> random_access_iterator_tag iterator_category<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> T                          value_type<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">ptrdiff_t</span>                  difference_type<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">const</span> T<span style="color: #000040;">*</span>                   pointer<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span>                   reference<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>一些算法会根据迭代器的类型进行优化，比如</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> InputIterator, <span style="color: #0000ff;">class</span> Distance<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> __distance<span style="color: #008000;">&#40;</span>InputIterator first, InputIterator last, Distance<span style="color: #000040;">&amp;</span> n, 
	input_iterator_tag<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span>first <span style="color: #000040;">!</span><span style="color: #000080;">=</span> last<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #000040;">++</span>first<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>n<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> RandomAccessIterator, <span style="color: #0000ff;">class</span> Distance<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> __distance<span style="color: #008000;">&#40;</span>RandomAccessIterator first, RandomAccessIterator last, 
	Distance<span style="color: #000040;">&amp;</span> n, random_access_iterator_tag<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		n <span style="color: #000040;">+</span><span style="color: #000080;">=</span> last <span style="color: #000040;">-</span> first<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> InputIterator, <span style="color: #0000ff;">class</span> Distance<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> distance<span style="color: #008000;">&#40;</span>InputIterator first, InputIterator last, Distance<span style="color: #000040;">&amp;</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	__distance<span style="color: #008000;">&#40;</span>first, last, n, iterator_category<span style="color: #008000;">&#40;</span>first<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>6，SGI中还有__type_traits负责萃取型别的特性</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> type<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> __type_traits <span style="color: #008000;">&#123;</span> 
   <span style="color: #0000ff;">typedef</span> __true_type     this_dummy_member_must_be_first<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_default_constructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_copy_constructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_assignment_operator<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_destructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    is_POD_type<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
__STL_TEMPLATE_NULL <span style="color: #0000ff;">struct</span> __type_traits<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">char</span><span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">typedef</span> __true_type    has_trivial_default_constructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __true_type    has_trivial_copy_constructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __true_type    has_trivial_assignment_operator<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __true_type    has_trivial_destructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __true_type    is_POD_type<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">/////////////////////////////////////</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ForwardIterator, <span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span>
__uninitialized_fill_aux<span style="color: #008000;">&#40;</span>ForwardIterator first, ForwardIterator last, 
                         <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, __true_type<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  fill<span style="color: #008000;">&#40;</span>first, last, x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ForwardIterator, <span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">void</span>
__uninitialized_fill_aux<span style="color: #008000;">&#40;</span>ForwardIterator first, ForwardIterator last, 
                         <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, __false_type<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  ForwardIterator cur <span style="color: #000080;">=</span> first<span style="color: #008080;">;</span>
  __STL_TRY <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #008080;">;</span> cur <span style="color: #000040;">!</span><span style="color: #000080;">=</span> last<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>cur<span style="color: #008000;">&#41;</span>
      construct<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span><span style="color: #000040;">*</span>cur, x<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  __STL_UNWIND<span style="color: #008000;">&#40;</span>destroy<span style="color: #008000;">&#40;</span>first, cur<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> ForwardIterator, <span style="color: #0000ff;">class</span> T, <span style="color: #0000ff;">class</span> T1<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span> __uninitialized_fill<span style="color: #008000;">&#40;</span>ForwardIterator first, ForwardIterator last, 
                                 <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> x, T1<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">typename</span> __type_traits<span style="color: #000080;">&lt;</span>T1<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">is_POD_type</span> is_POD<span style="color: #008080;">;</span>
  __uninitialized_fill_aux<span style="color: #008000;">&#40;</span>first, last, x, is_POD<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>如果使用SGI STL的话，可以在程序中使用__type_traits，自行设计一个__type_traits的特化版本，以便于编译器优化</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">__STL_TEMPLATE_NULL <span style="color: #0000ff;">struct</span> __type_traits<span style="color: #000080;">&lt;</span>Shape<span style="color: #000080;">&gt;</span> <span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">typedef</span> __true_type    has_trivial_default_constructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_copy_constructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_assignment_operator<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    has_trivial_destructor<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">typedef</span> __false_type    is_POD_type<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>7，vector就是一个数组，迭代器就是一个指针，插入一个元素可能会导致所有迭代器失效（如果重新申请了内存的话）。vector在需要扩充内存时会申请两倍大的内存，但是删除元素时并不释放内存，可以利用swap的技巧来释放内存。</p>
<p>8，list是一个双向环状列表，迭代器是一个类的实例，插入并不会导致其他的迭代器失效。list每个节点的内存随着插入而申请，删除而释放。</p>
<p>9，deque采用所谓的map作为主控，这里的map是一小块连续控件，其中每个元素都指向另一块较大的连续线性空间，称之为缓冲区，这个缓冲区才是deque的储存控件主体。STL STL允许指定缓冲区的大小。 deque的插入元素可能导致前面的迭代器失效，也可能导致后面的迭代器失效，总之认为全部失效就不会出错了。deque在需要新缓冲区的时候申请内存，在一个缓冲区的内容都被删光光时释放内存。</p>
<p>10，遍历删除时要考虑，序列容器删除时总是返回下一个元素，关联容器删除时下一个元素的迭代器不会失效，所以应在删除前进行后自增。</p>
<p>11，stack和queue均为容器适配器，默认底层以deque实现，也可使用list实现，均没有迭代器。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/10/stlxuexibiji/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>2010.10-2011.9的书单</title>
		<link>http://blog.mtian.net/2011/09/2010-10-2011-9%e7%9a%84%e4%b9%a6%e5%8d%95/</link>
		<comments>http://blog.mtian.net/2011/09/2010-10-2011-9%e7%9a%84%e4%b9%a6%e5%8d%95/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 17:11:13 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[未分类]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=419</guid>
		<description><![CDATA[《重构:改善既有代码的设计》 书不薄，但是感觉东西并不怎么多。 《嘿色幽品•贫僧是去往西天拜佛求亲的》扯淡书，垃圾玩意 《卓有成效的程序员》现在只记得几个快捷操作了。 《卯个人》基本上都是博客上的 《怪诞行为学》未看 《国殇:国民党正面战场抗战纪实》垃圾书，翻了几页就懒得看了 《软件开发沉思录:Thought Works文集》看过，已经完全忘记这本书里面写的什么了。 《IDA Pro权威指南》看了半本，熟悉了IDA的基本操作，如果以后有需求的话再看后半本。 《我的征途是星辰大海》不错 《深入解析Windows操作系统(第5版.英文版)》啃了半本，感觉实际项目中用到很少，对理解windows挺有用，不过现在忘的七七八八了。 《贞观幽明谭》马马虎虎 《慢风景》不错 《C++标准程序库:自修教程与参考手册》，翻了大概3章，不错，正在考虑是否要找时间看完。 《设计模式:可复用面向对象软件的基础(英文版)》，不错，在实际项目中用了四五个，其他的有些还未理解透。 《摩西英语(第1季)》，随手翻了几页。 《代码整洁之道(英文版)》，感觉自己在代码风格上已经很好了，有些托大，不打算看了。 《STL 源码剖析》，未看，考虑是否要看。 《Windows驱动开发技术详解》《寒江独钓——Windows内核安全编程》，看过，未用，忘记。 去年一年看的书其实还算不少，还有一些以前买的书又重新看了以便，譬如effctive c++,more effective c++,effective stl，windows核心编程，深入浅出MFC，mfc windows程序设计。 以及一些电子书或者借的书：深入探索C++对象模型，COM本质论，atl internals。 总结一下： 对于C++，嗯，可以弱弱的说下精通了。 MFC也大概懂得什么意思了。 COM/ATL入门，要用的话再继续看。 驱动相关，我擦，那几本书白看了，完全忘记了。 代码风格，感觉自己已经听好了 设计模式，算个熟悉吧，有空再把书看一遍。 不好意思的说，我甚至还花了两个星期看了下java和android开发，现在也全忘了，白费时间啊。 过去一年的看书，有点迷茫，浪费了不少时间。]]></description>
			<content:encoded><![CDATA[<p>《重构:改善既有代码的设计》 书不薄，但是感觉东西并不怎么多。</p>
<p>《嘿色幽品•贫僧是去往西天拜佛求亲的》扯淡书，垃圾玩意</p>
<p>《卓有成效的程序员》现在只记得几个快捷操作了。</p>
<p>《卯个人》基本上都是博客上的</p>
<p>《怪诞行为学》未看<br />
<span id="more-419"></span><br />
《国殇:国民党正面战场抗战纪实》垃圾书，翻了几页就懒得看了</p>
<p>《软件开发沉思录:Thought Works文集》看过，已经完全忘记这本书里面写的什么了。</p>
<p>《IDA Pro权威指南》看了半本，熟悉了IDA的基本操作，如果以后有需求的话再看后半本。</p>
<p>《我的征途是星辰大海》不错</p>
<p>《深入解析Windows操作系统(第5版.英文版)》啃了半本，感觉实际项目中用到很少，对理解windows挺有用，不过现在忘的七七八八了。</p>
<p>《贞观幽明谭》马马虎虎</p>
<p>《慢风景》不错</p>
<p>《C++标准程序库:自修教程与参考手册》，翻了大概3章，不错，正在考虑是否要找时间看完。</p>
<p>《设计模式:可复用面向对象软件的基础(英文版)》，不错，在实际项目中用了四五个，其他的有些还未理解透。</p>
<p>《摩西英语(第1季)》，随手翻了几页。</p>
<p>《代码整洁之道(英文版)》，感觉自己在代码风格上已经很好了，有些托大，不打算看了。</p>
<p>《STL 源码剖析》，未看，考虑是否要看。</p>
<p>《Windows驱动开发技术详解》《寒江独钓——Windows内核安全编程》，看过，未用，忘记。</p>
<p>去年一年看的书其实还算不少，还有一些以前买的书又重新看了以便，譬如effctive c++,more effective c++,effective stl，windows核心编程，深入浅出MFC，mfc windows程序设计。</p>
<p>以及一些电子书或者借的书：深入探索C++对象模型，COM本质论，atl internals。</p>
<p>总结一下：</p>
<p>对于C++，嗯，可以弱弱的说下精通了。</p>
<p>MFC也大概懂得什么意思了。</p>
<p>COM/ATL入门，要用的话再继续看。</p>
<p>驱动相关，我擦，那几本书白看了，完全忘记了。</p>
<p>代码风格，感觉自己已经听好了</p>
<p>设计模式，算个熟悉吧，有空再把书看一遍。</p>
<p>不好意思的说，我甚至还花了两个星期看了下java和android开发，现在也全忘了，白费时间啊。</p>
<p>过去一年的看书，有点迷茫，浪费了不少时间。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2011/09/2010-10-2011-9%e7%9a%84%e4%b9%a6%e5%8d%95/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>自写了一个MD5校验器</title>
		<link>http://blog.mtian.net/2010/09/%e8%87%aa%e5%86%99%e4%ba%86%e4%b8%80%e4%b8%aamd5%e6%a0%a1%e9%aa%8c%e5%99%a8/</link>
		<comments>http://blog.mtian.net/2010/09/%e8%87%aa%e5%86%99%e4%ba%86%e4%b8%80%e4%b8%aamd5%e6%a0%a1%e9%aa%8c%e5%99%a8/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 14:00:58 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[胡思乱想]]></category>
		<category><![CDATA[MD5]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=395</guid>
		<description><![CDATA[最近写着玩的，计算大文件譬如ISO文件的时候可能比你手头的要快一些，另外界面也比较好看（我承认我扒了CHROME的皮肤）。比较无聊，报告完毕。名字实在懒得想了，就这样吧。 下载地址： http://www.mtian.net/down/MD5.zip 效果图： update: 改了个BUG（这么个小东西还有BUG，真不好意思），感谢林卯的反馈。 支持命令行调用 update: 更新了个图标。感谢林卯为这个小东东做了个图标，并提了一些建议，呵呵。]]></description>
			<content:encoded><![CDATA[<p>最近写着玩的，计算大文件譬如ISO文件的时候可能比你手头的要快一些，另外界面也比较好看（我承认我扒了CHROME的皮肤）。比较无聊，报告完毕。名字实在懒得想了，就这样吧。</p>
<p>下载地址：</p>
<p><a href="http://www.mtian.net/down/MD5.zip">http://www.mtian.net/down/MD5.zip</a></p>
<p><a href="http://log.mtian.net/MD5.zip"> </a></p>
<p>效果图：</p>
<p><a href="http://blog.mtian.net/wp-content/uploads/2010/09/md5.png"><img class="aligncenter size-full wp-image-396" title="md5" src="http://blog.mtian.net/wp-content/uploads/2010/09/md5.png" alt="" width="558" height="205" /></a></p>
<p>update:</p>
<p>改了个BUG（这么个小东西还有BUG，真不好意思），感谢林卯的反馈。</p>
<p>支持命令行调用</p>
<p>update:</p>
<p>更新了个图标。感谢林卯为这个小东东做了个图标，并提了一些建议，呵呵。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2010/09/%e8%87%aa%e5%86%99%e4%ba%86%e4%b8%80%e4%b8%aamd5%e6%a0%a1%e9%aa%8c%e5%99%a8/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DetectDLLHijacking(探测是否有DLLHijacking漏洞的小工具)</title>
		<link>http://blog.mtian.net/2010/08/detectdllhijacking/</link>
		<comments>http://blog.mtian.net/2010/08/detectdllhijacking/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 11:30:44 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[胡思乱想]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=387</guid>
		<description><![CDATA[最近DLLHijacking抄的很热，其实很无聊的一个东西。估计是安稳太久了，闲的蛋疼，于是拿了个老东西出来炒炒，不过开始炒的人似乎是个名人，然后大家一起玩，竟然逼的微软出了安全公告。偶也无聊，练手写了这么一个小工具，打开后放那就可以了，然后打开你系统上的程序，他会检查下是否有DLLHijacking漏洞。不过俺技术不好，不知道为啥有一些检查不到。这里是程序主要原理：挂了个钩子把DLL注入到别的程序里面，然后IATHook了LoadLibraryA、LoadLibraryW、LoadLibraryExA 和LoadLibraryExW。有大牛知道为啥俺这个检查不全的话，请告诉俺，谢谢。 下载地址：http://www.mtian.net/down/DetectDLLHijacking.zip 效果截图： update：问题已查明，setwindowshook的问题，在某些程序那里loadlibrary之前未挂上钩子。]]></description>
			<content:encoded><![CDATA[<p>最近DLLHijacking抄的很热，其实很无聊的一个东西。估计是安稳太久了，闲的蛋疼，于是拿了个老东西出来炒炒，不过开始炒的人似乎是个名人，然后大家一起玩，竟然逼的微软出了安全公告。偶也无聊，练手写了这么一个小工具，打开后放那就可以了，然后打开你系统上的程序，他会检查下是否有DLLHijacking漏洞。不过俺技术不好，不知道为啥有一些检查不到。这里是程序主要原理：挂了个钩子把DLL注入到别的程序里面，然后IATHook了LoadLibraryA、LoadLibraryW、LoadLibraryExA 和LoadLibraryExW。有大牛知道为啥俺这个检查不全的话，请告诉俺，谢谢。</p>
<p>下载地址：<a href="http://www.mtian.net/down/DetectDLLHijacking.zip">http://www.mtian.net/down/DetectDLLHijacking.zip</a></p>
<p>效果截图：</p>
<p><a href="http://blog.mtian.net/wp-content/uploads/2010/08/DetectDLLHijacking.jpg"><img class="aligncenter size-full wp-image-388" title="DetectDLLHijacking" src="http://blog.mtian.net/wp-content/uploads/2010/08/DetectDLLHijacking.jpg" alt="" width="572" height="388" /></a></p>
<p>update：问题已查明，setwindowshook的问题，在某些程序那里loadlibrary之前未挂上钩子。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2010/08/detectdllhijacking/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>MDecoder的隐藏功能及历史版本下载</title>
		<link>http://blog.mtian.net/2010/08/mdecoder%e7%9a%84%e9%9a%90%e8%97%8f%e5%8a%9f%e8%83%bd%e5%8f%8a%e5%8e%86%e5%8f%b2%e7%89%88%e6%9c%ac%e4%b8%8b%e8%bd%bd/</link>
		<comments>http://blog.mtian.net/2010/08/mdecoder%e7%9a%84%e9%9a%90%e8%97%8f%e5%8a%9f%e8%83%bd%e5%8f%8a%e5%8e%86%e5%8f%b2%e7%89%88%e6%9c%ac%e4%b8%8b%e8%bd%bd/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 18:01:03 +0000</pubDate>
		<dc:creator>mtian</dc:creator>
				<category><![CDATA[MDecoder]]></category>
		<category><![CDATA[历史版本]]></category>
		<category><![CDATA[网页木马解密]]></category>

		<guid isPermaLink="false">http://blog.mtian.net/?p=381</guid>
		<description><![CDATA[在配置文件config.ini的[main]中添加这么一行后可以打开MDecoder的隐藏功能：FullFunc=True，打开的两个小功能是： 1，自动保存网页到MDecoder目录的子目录Download下。网页是拼接过的，把网页内包含的JS拼接到了网页内部。 2，解密选项增加三个：Scan，解密页面给扫描用的解密方法；Parser，会进行简单的词法分析来拼接字符串；HookUnescape，没啥好解释的。 MDecoder历史版本下载地址： http://www.mtian.net/down/MDecoder v0.21.zip http://www.mtian.net/down/MDecoder v0.22.zip &#8211;&#62; http://www.mtian.net/down/MDecoder v0.66.zip 另外，短期内没有更新这个工具的打算了。]]></description>
			<content:encoded><![CDATA[<p>在配置文件config.ini的[main]中添加这么一行后可以打开MDecoder的隐藏功能：FullFunc=True，打开的两个小功能是：<br />
1，自动保存网页到MDecoder目录的子目录Download下。网页是拼接过的，把网页内包含的JS拼接到了网页内部。<br />
2，解密选项增加三个：Scan，解密页面给扫描用的解密方法；Parser，会进行简单的词法分析来拼接字符串；HookUnescape，没啥好解释的。</p>
<p>MDecoder历史版本下载地址：</p>
<p><a href="http://www.mtian.net/down/MDecoder v0.21.zip" target="_blank">http://www.mtian.net/down/MDecoder v0.21.zip</a><br />
<a href="http://www.mtian.net/down/MDecoder v0.22.zip" target="_blank">http://www.mtian.net/down/MDecoder v0.22.zip</a><br />
&#8211;&gt;<br />
<a href="http://www.mtian.net/down/MDecoder v0.66.zip" target="_blank">http://www.mtian.net/down/MDecoder v0.66.zip</a></p>
<p>另外，短期内没有更新这个工具的打算了。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mtian.net/2010/08/mdecoder%e7%9a%84%e9%9a%90%e8%97%8f%e5%8a%9f%e8%83%bd%e5%8f%8a%e5%8e%86%e5%8f%b2%e7%89%88%e6%9c%ac%e4%b8%8b%e8%bd%bd/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

