﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-I smell magic in the air-随笔分类-STL</title><link>http://www.cppblog.com/izualzhy/category/15346.html</link><description>坚持 相信自己</description><language>zh-cn</language><lastBuildDate>Tue, 15 Nov 2011 01:40:55 GMT</lastBuildDate><pubDate>Tue, 15 Nov 2011 01:40:55 GMT</pubDate><ttl>60</ttl><item><title>C++ iostream迭代器简介(1)</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/09/159894.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 09 Nov 2011 15:15:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/09/159894.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/159894.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/09/159894.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/159894.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/159894.html</trackback:ping><description><![CDATA[<p>标准库定义的迭代器有很多种，istream_iterator用于读取输入流， ostream_iterator用于写输出流。这些迭代器将它们所对应的流视为特定类型的元素序列。使用流迭代器时，可以用泛型算法从流对象读取数据（或将数据写到流对象）。 <p>这篇笔记主要是其定义和基本的使用： <p>iostream迭代器的构造函数： <p><strong>1. istream_iterator&lt;T&gt; in(strm);</strong> <p>创建从输入流strm读取T类型对象的istream_iterator对象 <p>2<strong>. istream_iterator&lt;T&gt; in;</strong> <p>istream_iterator对象的超出末端迭代器。 <p><strong>3. ostream_iterator&lt;T&gt; out(strm);</strong> <p>创建将T类型的对象写到输出流strm的ostream_iterator对象 <p><strong>4. ostream_iterator&lt;T&gt; out(strm,delim);</strong> <p>创建将T类型的对象写到输出流strm的ostream_iterator对象，在写入过程中使用delim作为元素的分隔符。delim是以空字符结束的字符数组。 <p>先看istream_iterator一个例子：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;vector&gt;<br><br><span style="color: #cc6633">#include</span> &lt;iterator&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">int</span> main()<br><br>{<br><br>istream_iterator&lt;<span style="color: #0000ff">int</span>&gt; cin_it(cin);<span style="color: #008000">//read ints from cin</span><br><br>istream_iterator&lt;<span style="color: #0000ff">int</span>&gt; end_of_stream;<span style="color: #008000">//end iterator value(eof)</span><br><br>vector&lt;<span style="color: #0000ff">int</span>&gt; vec(cin_it, end_of_stream);<br><br><span style="color: #008000">//also should be written like this:</span><br><br><span style="color: #008000">//vector&lt;int&gt; vec;</span><br><br><span style="color: #008000">//while (cin_it != end_of_stream)</span><br><br><span style="color: #008000">//vec.push_back(*cin_it++);</span><br><br>vector&lt;<span style="color: #0000ff">int</span>&gt;::iterator it=vec.begin();<br><br><span style="color: #0000ff">while</span> (it!=vec.end()) {<br><br>cout &lt;&lt; <span style="color: #006080">" "</span> &lt;&lt; *it++;<br><br>}<br><br>cout &lt;&lt; endl;<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br><br></pre><br></div>
<p>该程序读取键盘输入，写到vec，直到遇到非法输入。像下面这样：
<p>1 2 3 4af
<p>1 2 3 4
<p>程序循环从cin读取int型数据，并将读入的内容保存在vec。每次循环都会检查cin_it是否为eof。其中eof迭代器定义为空的istream_iterator对象，用作结束迭代器。绑在流上的迭代器在遇到文件或某个错误时，将等于结束迭代器的值。
<p>如程序注释里的，这部分可以用
<p>vector&lt;int&gt; vec(cin_it, end_of_stream)//construct vec from an iterator range
<p>来代替。
<p>这里，用一对标记元素范围的迭代器构造vec对象。这些迭代器是istream_iterator对象，这就意味着这段范围的元素是通过读取所关联的流来获得的。这个构造函数的效果是读cin，直到到达文件结束或输入的不是int型数值为止。读取的元素用于构造vec对象。
<p>在创建istream_iterator时，可直接将它绑定到一个流上。另一种方法是创建时不提供实参，则该迭代器指向超出末端的位置。ostream_iterator不提供超出末端迭代器。
<p>ostream_iterator对象必须与特定的流绑在一起。在创建ostream_iterator时，可提供第二个（可选的）实参，指定将元素写入输出流时使用的分隔符。分隔符必须是C风格字符串。因为它是C风格字符串，所以必须以空字符结束；否则，其行为将是未定义的。
<p>看个ostream_iterator的例子：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;iterator&gt;<br><br><span style="color: #cc6633">#include</span> &lt;string&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">int</span> main()<br><br>{<br><br>ostream_iterator&lt;string&gt; out_iter(cout, <span style="color: #006080">":test\n"</span>);<span style="color: #008000">//write one string per line to the standard output</span><br><br>istream_iterator&lt;string&gt; in_iter(cin),eof;<span style="color: #008000">//read strings from standard input and the end iterator</span><br><br><span style="color: #0000ff">while</span> (in_iter!=eof) {<br><br><span style="color: #008000">//write value of in_iter to standard output</span><br><br><span style="color: #008000">//and then increment the iterator to get the next value from cin</span><br><br>*out_iter = *in_iter;<span style="color: #008000">//write to standard output here</span><br><br>++out_iter;<br><br>++in_iter;<br><br>}<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br><br></pre><br></div>
<p>这个程序读cin，并将每个读入的值依次写到cout不同的行。
<p>首先，定一个ostream_stream对象，用于将string类型的数据写到cout中，每个string对象后跟一个&#8221;test&#8221;和换行符。定义两个istream_iterator对象，用于从cin中读取string对象。while循环类似于前一个例子。但是这一次不是将读取的数据存储在vector对象，而是将读取的数据付给out_iter,从而输出到cout上。
<p>*out_iter = *in_iter;这一句执行时，会向console打印。
<p>输出类似于这个样子：
<p>123
<p>123:test
<p>abc
<p>abc:test
<p>xyz
<p>xyz:test
<p>参考：
<p>1. 《C++Primer》
 <img src ="http://www.cppblog.com/izualzhy/aggbug/159894.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-09 23:15 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/09/159894.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>STL-Algorithm之for_each函数学习</title><link>http://www.cppblog.com/izualzhy/archive/2011/11/09/159891.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 09 Nov 2011 15:09:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/11/09/159891.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/159891.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/11/09/159891.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/159891.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/159891.html</trackback:ping><description><![CDATA[<p>最近学习一本C++ Library Reference的电子书，感觉跟翻字典似的，本文是抄录下来的学习笔记之一。  <p>函数声明：</p> <div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">class</span> InputInterator, <span style="color: #0000ff">class</span> Function&gt;<br><br>Function for_each(InputIterator first, InputIterator last, Function f);<br></pre><br></div>
<p>函数解释： 
<p>对其输入范围[first,last)的每一个元素应用函数（或函数对象）f。如果f有返回值，就忽略该返回值。迭代器是输入迭代器，所以f不能写元素。 
<p>函数行为类似于这样：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">template</span>&lt;<span style="color: #0000ff">class</span> InputIterator, <span style="color: #0000ff">class</span> Function&gt;<br><br>Function for_each(InputIterator first, InputIterator last, Function f)<br><br>{<br><br><span style="color: #0000ff">while</span> (first!=last)<br><br>f(*first++);<br><br><span style="color: #0000ff">return</span> f;<br><br>}<br></pre><br></div>
<p>关于f的定义： 
<p>Unary function taking an element in the range as argument. This can either be a pointer to a function or an object whose class overloads operator().<br>Its return value, if any, is ignored. 
<p>例子：</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">//for_each example</span><br><br><span style="color: #cc6633">#include</span> &lt;iostream&gt;<br><br><span style="color: #cc6633">#include</span> &lt;algorithm&gt;<br><br><span style="color: #cc6633">#include</span> &lt;vector&gt;<br><br><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;<br><br><span style="color: #0000ff">void</span> myfunction(<span style="color: #0000ff">int</span> i)<br><br>{<br><br>cout &lt;&lt; <span style="color: #006080">"myfunction: "</span> &lt;&lt; i &lt;&lt; endl;<br><br>}<br><br><span style="color: #0000ff">struct</span> myclass<br><br>{<br><br><span style="color: #0000ff">void</span> <span style="color: #0000ff">operator</span>()(<span style="color: #0000ff">int</span> i) { cout &lt;&lt; <span style="color: #006080">"myclass: "</span> &lt;&lt; i &lt;&lt; endl; }<br><br>}myobject;<br><br><span style="color: #0000ff">int</span> main()<br><br>{<br><br>vector&lt;<span style="color: #0000ff">int</span>&gt; myvector;<br><br>myvector.push_back(10);<br><br>myvector.push_back(20);<br><br>myvector.push_back(30);<br><br>cout &lt;&lt; <span style="color: #006080">"myvector contains: "</span> &lt;&lt; endl;<br><br>for_each(myvector.begin(),myvector.end(),myfunction);<br><br>cout &lt;&lt; <span style="color: #006080">"myvector contains: "</span> &lt;&lt; endl;<br><br>for_each(myvector.begin(),myvector.end(),myobject);<br><br><span style="color: #0000ff">return</span> 0;<br><br>}<br><br></pre><br></div>
<p>输出： 
<p>myvector contains: 
<p>myfunction: 10 
<p>myfunction: 20 
<p>myfunction: 30 
<p>myvector contains: 
<p>myclass: 10 
<p>myclass: 20 
<p>myclass: 30
<img src ="http://www.cppblog.com/izualzhy/aggbug/159891.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-11-09 23:09 <a href="http://www.cppblog.com/izualzhy/archive/2011/11/09/159891.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转]关于STL-map讲的很不错的一篇文章</title><link>http://www.cppblog.com/izualzhy/archive/2011/10/20/158783.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Thu, 20 Oct 2011 14:24:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/10/20/158783.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/158783.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/10/20/158783.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/158783.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/158783.html</trackback:ping><description><![CDATA[<p><font color="#ff0000"><strong>一：map是关联式容器，它提供一对一的映射</strong></font>。存储的数据有两个部分，一个是关键字，一个是值，其中关键字只能出现一次，而不同的关键字，可以有相同的值。map中用pair来存储这两个值的。pair是stl定义的一种数据结构，后面会有简述。map内部自建一颗红黑树，所有map里面的数据都是有序的。 <p><font color="#ff0000"><strong>二：方法</strong></font> <p><b>1.</b><b>构造函数</b>，map有6个构造函数。但是我们通常用map&lt;type1,type2&gt; m;这种方法来构造一个map实例。 <p><b>2.</b><b>数据插入</b>，map通常用下面的三种方式插入数据。 <p>1),用insert方法插入pair数据。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffffff; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">void</span> main( VOID )
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt; m;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: m.insert(pair&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;("<span style="color: #8b0000">sa</span>",67));
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: m.insert(make_pair&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;("<span style="color: #8b0000">sd</span>",565));<span style="color: #008000">//用make_pair方法(函数)产生pair对象。</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::iterator it = m.begin();
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: <span style="color: #0000ff">while</span>(it!=m.end())
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: {
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: cout&lt;&lt;it-&gt;first&lt;&lt;"<span style="color: #8b0000"> </span>"&lt;&lt;it-&gt;second&lt;&lt;endl;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: it++;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: }
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: }</pre></pre>
<p>2,)用insert函数插入value_type数据。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffffff; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">void</span> main( VOID )
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt; m;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: m.insert(map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::value_type("<span style="color: #8b0000">sd</span>",5));
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::iterator it = m.begin();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: <span style="color: #0000ff">while</span>(it!=m.end())
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: {
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: cout&lt;&lt;it-&gt;first&lt;&lt;"<span style="color: #8b0000"> </span>"&lt;&lt;it-&gt;second&lt;&lt;endl;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: it++;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: }
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: }</pre></pre>
<p>其实这个方法和上面的本质上是一样的。因为valud_type 就是pair类型的，只是换个方法而已，可以通过 cout&lt;&lt;typeid(map&lt;string,int&gt;::value_type).name()来获得 value_type的类型，得到类型为
<p>struct std::pair&lt;class std::basic_string&lt;char,struct std::char_traits&lt;char&gt;,class std::allocator&lt;char&gt; &gt; const ,int&gt;说明是个struct pair类型的。
<p>3,)通过重载[]来插入数据。</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffffff; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">void</span> main( VOID )
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  2: {
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  3: map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt; m;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  4: cout&lt;&lt;typeid(map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::value_type).name()&lt;&lt;endl;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  5: m["<span style="color: #8b0000">ds</span>"] = 4;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  6: m["<span style="color: #8b0000">ds</span>"] = 10;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  7: map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::iterator it = m.begin();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  8: <span style="color: #0000ff">while</span>(it!=m.end())
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">  9: {
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 10: cout&lt;&lt;it-&gt;first&lt;&lt;"<span style="color: #8b0000"> </span>"&lt;&lt;it-&gt;second&lt;&lt;endl;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 11: it++;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 12: }
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"> 13: }</pre></pre>
<p>这种法式是通过重载[]来实现的，但是需要注意的是，这种法式和前两种有本质的区别，看下面的代码
<p>m.insert(pair&lt;string,int&gt;("ds",5));
<p>m.insert(pair&lt;string,int&gt;("ds",15));
<p>当关键字相同的时候，不会改变他的值，也就是说在调用insert函数的时候，会检查，这个关键字是否存在了，如果存在，那么就不做任何操作。否则插入新的数据。
<p>m["ds"] = 4;
<p>m["ds"] = 10;
<p>这种方式插入数据是不会做检查的，会直接在那个点上写上关键字和值，也就是说。ds项的值将是10.
<p><b>3</b><b>，数据遍历。</b>
<p>stl里的容器的遍历都是通过迭代器来遍历的。即便是用数组的方式，也是通过迭代器。数据遍历也有三种法式，1，向前迭代器，2，用反向迭代器，3，数组，第一种方式前面也就有说明。下面讲第二第三种，
<p>反向迭代器的方式。
<p>这样是以相反的法式输出的。
<p>数组方式</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffffff; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">void</span> main( VOID )
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt; m;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">m["<span style="color: #8b0000">ds</span>"] = 4;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">m["<span style="color: #8b0000">as</span>"] = 10;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">m.insert(pair&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;("<span style="color: #8b0000">cs</span>",5));
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">m["<span style="color: #8b0000">bs</span>"] = 123;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::iterator it = m.begin();
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">for</span>(<span style="color: #0000ff">int</span> i=1;i&lt;=m.size();i++)
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">cout&lt;&lt;m[it-&gt;first]&lt;&lt;endl;<span style="color: #008000">//这里是用重载[]来得到value的。</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">it++;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">}
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">}</pre></pre>
<p><b>4 .</b><b>数据的查找（包括判定这个关键字是否在map</b><b>中出现）</b>
<p>在这里我们将体会，map在数据插入时保证有序的好处。
<p>要判定一个数据（关键字）是否在map中出现的方法比较多，这里标题虽然是数据的查找，在这里将穿插着大量的map基本用法。
<p>这里给出三(2)种数据查找方法
<p>第一种：用count函数来判定关键字是否出现，其缺点是无法定位数据出现位置,由于map的特性，一对一的映射关系，就决定了count函数的返回值只有两个，要么是0，要么是1，出现的情况，当然是返回1了
<p>第二种：用find函数来定位数据出现位置，它返回的一个迭代器，当数据出现时，它返回数据所在位置的迭代器，如果map中没有要查找的数据，它返回的迭代器等于end函数返回的迭代器</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffffff; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">#include &lt;iostream&gt;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">#include &lt;fstream&gt;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">#include &lt;<span style="color: #0000ff">string</span>&gt;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">#include &lt;map&gt;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">void</span> main()
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//定义map 对象</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt; word;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//定义指针</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::iterator it;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//向word 插入元素 ("a",9)</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">word.insert (map&lt;<span style="color: #0000ff">string</span>,<span style="color: #0000ff">int</span>&gt;::value_type("<span style="color: #8b0000">a</span>",9));
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//查找 键是"a"的元素，返回指向元素的指针。</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">it=word.find ("<span style="color: #8b0000">a</span>");
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//如果元素不存在，指针指向word.end().</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">if</span>(it!=word.end ())
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">cout&lt;&lt;it-&gt;second&lt;&lt;endl; <span style="color: #008000">//输出元素的值</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//查找 键是"a"的元素，</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">int</span> result=word.count ("<span style="color: #8b0000">a</span>");
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #008000">//如果键存在返回1，否则返回0</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">if</span>(result)
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">cout&lt;&lt;word["<span style="color: #8b0000">a</span>"]&lt;&lt;endl; <span style="color: #008000">//输出元素的值</span>
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">cout&lt;&lt;endl;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">}</pre></pre>
<p><b>5.</b><b>数据清空，或判空，</b>
<p>清空数据用clear函数，判断数据是否是空用empty函数。
<p><b>6.</b><b>数据删除。</b>
<p>数据删除用erase函数，这个函数有3个重载函数，用的时候，可随机而用。注意用这个函数后，相关的迭代器将会失效。
<p><font color="#ff0000"><strong>三：pair是stl里(目前我知道的)定义的一个struct</strong>，</font>在msdn上查到如下的信息。
<p>template&lt;class Type1, class Type2&gt;
<p>struct pair
<p>{
<p>typedef Type1 first_type;
<p>typedef Type2 second_type
<p>Type1 first;
<p>Type2 second;
<p>pair( );
<p>pair(
<p>const Type1&amp; __Val1,
<p>const Type2&amp; __Val2
<p>);
<p>template&lt;class Other1, class Other2&gt;
<p>pair(
<p>const pair&lt;Other1, Other2&gt;&amp; _Right
<p>);
<p>与之相关的是make_pair函数。是个模板函数。
<p>template&lt;class Type1, class Type2&gt; pair&lt;Type1, Type2&gt; make_pair( Type1 _Val1, Type2 _Val2 );
<p><strong><font color="#ff0000">四：关于map的迭代器，</font></strong>
<p>对数据的插入，遍历，查找等操作，迭代器将不会失效，但是删除操作会失效。这与vector等序列式容器是不一样的。
<p><font color="#ff0000"><strong>五：效率</strong></font>
<p>因为内部RB-TREE所以大多数的操作的时间复杂度都是O(logN)，空间分析，在这些节点不保存数据的情况下就需要，左右孩子指针，指向父节点的指针，说明红黑的枚举值。
<p><font color="#ff0000"><strong>六：map的key的比较：</strong></font>
<p>value_compare value_comp ( ) const;
<p>其返回值是一个比较类的对象，这个类是map::value_compare，并且是map的一个内部类。
<p>返回的这个对象可以用来通过比较两个元素的value来判决它们对应的key在map的位置谁在前面谁在后面。
<p>下面是一个简单的例子，看一下就会更明白了:</p><pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #ffffff; min-height: 40px; padding-left: 5px; width: 650px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px"><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">#include &lt;iostream&gt;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">#include &lt;map&gt;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">using</span> <span style="color: #0000ff">namespace</span> std;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"></pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">int</span> main ()
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">{
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">map&lt;<span style="color: #0000ff">char</span>,<span style="color: #0000ff">int</span>&gt; mymap;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">map&lt;<span style="color: #0000ff">char</span>,<span style="color: #0000ff">int</span>&gt;::iterator it;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">pair&lt;<span style="color: #0000ff">char</span>,<span style="color: #0000ff">int</span>&gt; highest;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">mymap['x']=1001;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">mymap['y']=2002;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">mymap['z']=3003;
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">cout &lt;&lt; "<span style="color: #8b0000">mymap contains:\n</span>";
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">highest=*mymap.rbegin(); <span style="color: #008000">// last element</span>
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">it=mymap.begin();
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">do</span> {
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">cout &lt;&lt; (*it).first &lt;&lt; "<span style="color: #8b0000"> =&gt; </span>" &lt;&lt; (*it).second &lt;&lt; endl;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">} <span style="color: #0000ff">while</span> ( mymap.value_comp()(*it++, highest) );
</pre><pre style="background-color: #c0c0c0; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px"><span style="color: #0000ff">return</span> 0;
</pre><pre style="background-color: #ffff80; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">}</pre></pre>
<p>输出结果:
<p>mymap contains:
<p>x =&gt; 1001
<p>y =&gt; 2002
<p>z =&gt; 3003
<p>解释一下，上面语句while里面的mymap.value_comp()(*it++, highest)在这样的条件下会返回true:
<p>*it++对应的key在map中排在highest对应的key的前面时。</p><img src ="http://www.cppblog.com/izualzhy/aggbug/158783.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-10-20 22:24 <a href="http://www.cppblog.com/izualzhy/archive/2011/10/20/158783.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于size_t与size_type</title><link>http://www.cppblog.com/izualzhy/archive/2011/10/18/158636.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Tue, 18 Oct 2011 14:40:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2011/10/18/158636.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/158636.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2011/10/18/158636.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/158636.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/158636.html</trackback:ping><description><![CDATA[<p>问题起源于这样一段代码：
</p><p style="background: #333333"><span style="font-family:Consolas; font-size:12pt"><span style="color:skyblue">01 </span><span style="color:indianred">#include &lt;algorithm&gt;</span><span style="color:white"><br/></span><span style="color:skyblue">02 </span><span style="color:indianred">#include &lt;stdio.h&gt;</span><span style="color:white"><br/></span><span style="color:skyblue">03 </span><span style="color:white"><br/></span><span style="color:skyblue">04 </span><span style="color:darkkhaki">int</span><span style="color:white"> main()<br/></span><span style="color:#f810b0">05 </span><span style="color:white">{<br/></span><span style="color:skyblue">06 </span><span style="color:white">    size_t indexs = -1;<br/></span><span style="color:skyblue">07 </span><span style="color:white">    size_t ps = 100;<br/></span><span style="color:skyblue">08 </span><span style="color:white">    </span><span style="color:darkkhaki">int</span><span style="color:white"> index = -1;<br/></span><span style="color:skyblue">09 </span><span style="color:white">    </span><span style="color:darkkhaki">int</span><span style="color:white"> p = 100;<br/></span><span style="color:#f810b0">10 </span><span style="color:white">    printf("%d\n",std::min(p,index));<br/></span><span style="color:skyblue">11 </span><span style="color:white">    printf("%d\n",std::min(ps,indexs));<br/></span><span style="color:skyblue">12 </span><span style="color:white">    </span><span style="color:khaki">return</span><span style="color:white"> 0;<br/></span><span style="color:skyblue">13 </span><span style="color:white">}
</span></span></p><p>
 </p><p>其实是很简单的题目，不过要对size_t类型有一个了解才行。
</p><p><strong>关于size_t的来源</strong>：
</p><p>数据类型"socklen_t"和int应该具有相同的长度。否则就会破坏 BSD套接字层的填充.POSIX开始的时候用的是size_t, Linus Torvalds(他希望有更多的人,但显然不是很多) 努力向他们解释使用size_t是完全错误的,因为在64位结构中 size_t和int的长度是不一样的,而这个参数(也就是accept函数的第三参数)的长度必须和int一致,因为这是BSD套接字接口标准.最终POSIX的那帮家伙找到了解决的办法,那就是创造了一个新的类型"socklen_t".Linux Torvalds说这是由于他们发现了自己的错误但又不好意思向大家伙儿承认,所以另外创造了一个新的数据类型 。
</p><p>
 </p><p>size_t在C语言中就有了，size_t和ssize_t是ANSI C提供的标准头文件里定义的一个"数据类型"，其实并不是新的数据类型，不是关键字，是通过typedef从已有数据类型定义而来。
</p><p>ANSI C总共提供了24个头文件。
</p><p>&lt;cstddef&gt;里这么定义了
</p><p>#include &lt;stddef.h&gt;
</p><p>using ::size_t
</p><p>using::ptrdiff_t
</p><p>在/usr/lib/gcc/i486-linux-gnu/4.4/include下面
</p><p>&lt;stddef.h&gt;里面我找到了这么几句：
</p><p>#ifndef __SIZE_TYPE__
</p><p>#define __SIZE__TYPE__ long unsigned int
</p><p>#endif
</p><p>#if !(defined(__GUNU__) &amp;&amp; defined (size_t))
</p><p>typedef __SIZE_TYPE__ size_t;
</p><p>#ifdef __BEOS
</p><p>typedef long ssize_t
</p><p>…
</p><p>没太看明白，不过从网上找到了不错的一个<strong>解释</strong>：
</p><p>
 </p><p>size_t是为了方便系统之间的移植而定义的。
</p><p><span style="color:red">在32位系统上定义为 unsigned int
</span></p><p><span style="color:red">在64位系统上定义为 unsigned long
</span></p><p>
 </p><p>更准确的说法是在32位系统上是32位无符号整型
</p><p>在64位系统上是64位无符号整型
</p><p>
 </p><p>size_t一般用来表示一种计数，比如有多少东西被拷贝等。
</p><p>sizeof操作符的结果类型是size_t,
</p><p>该类型保证能容纳实现所建立的最大对象的字节大小。
</p><p>
 </p><p>它的意义大致是"适于计量内存中可容纳的数据项目的个数的无符号整数类型"。
</p><p>所以，它在数组下标和内存管理函数之类的地方广泛使用
</p><p>
 </p><p>ssize_t:
</p><p>这个数据类型用来表示可以被执行读写操作的数据块的大小。它和size_t类似，但必须是signed。
</p><p>再来看下<strong>size_t与size_type的区别</strong>：
</p><p>我觉得有一句话总结的很好：
</p><p><span style="color:red">size_t是全局的，而size_type是跟容器相关的。
</span></p><p>找了下相关的文件：
</p><p>/c++/4.3/bits/stl_tree.h,stl_list.h,stl_deque.h等直接这么定义了
</p><p>typedef size_t size_type
</p><p>stl_mutiset.h则这么定义的：
</p><p>typedef typename _Rep_type::size_type size_type;
</p><p>
 </p><p>那么size_type到底是一种什么样的类型呢？
</p><p>string类类型和许多其他库类型都定义了一些配套类型（companion type）。通过这些配套类型，库类型的使用就能与机器无关。size_type就是这些配套类型中的一种。
</p><p>size_type被定义为与unsigned型（unsigned int, unsigned long）具有相同的含义，而且可以保证足够大能够存储任意string对象的长度。为而来使用由string类型定义的size_type类型。程序员必须加上作用于操作符来说明所使用的size_type类型是由string类定义的。
</p><p>我们为什么不适用int变量来保存string的size呢？
</p><p>使用int变量的问题是：有些机器上的int变量的表示范围太小，甚至无法存储实际并不长的string对象。如在有16位int型的机器上，int类型变量最大只能表示32767个字符的string对象。而能容纳一个文件内容的string对象轻易就能超过这个数字，因此，为了避免溢出，保存一个string对象的size的最安全的方法就是使用标准库类型string：：size_type().
</p><p><span style="color:red">一点注意</span>：虽然是在学习标准库string的时候巧遇了size_type类型，但是，其实vector库也可以定义size_type类型，在vector库中还有一个difference_type类型，该类型用来存储任何两个迭代器对象间的距离，所以是signed类型的。
</p><p>
 </p><p>啰啰嗦嗦说了这么多，其实关于这个问题文章里红字标注的部分就足够了。其他的看下加深印象即可，最开始的程序结果输出为：
</p><p>-1 100。
</p><p>
 </p><p>
 </p><p>
 </p><p>
 </p><p>
 </p><img src ="http://www.cppblog.com/izualzhy/aggbug/158636.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2011-10-18 22:40 <a href="http://www.cppblog.com/izualzhy/archive/2011/10/18/158636.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>deque顺序容器</title><link>http://www.cppblog.com/izualzhy/archive/2010/11/03/132326.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 03 Nov 2010 12:37:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2010/11/03/132326.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/132326.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2010/11/03/132326.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/132326.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/132326.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
<p>#include &lt;iostream&gt;<br>#include &lt;deque&gt;<br>#include &lt;algorithm&gt;</p>
<p>using namespace std;</p>
<p>int main()<br>{<br>&nbsp;deque&lt;double&gt; values;<br>&nbsp;ostream_iterator&lt;double&gt; output(cout," ");</p>
<p>&nbsp;values.push_front(2.2);//只适用于list和deque,而不适于vector<br>&nbsp;values.push_front(2.3);<br>&nbsp;values.push_back(1.1);</p>
<p>&nbsp;cout &lt;&lt; "values contains: ";<br>&nbsp;for ( int i=0;i&lt;values.size();i++)<br>&nbsp;&nbsp;cout &lt;&lt; values[i] &lt;&lt; ' ';</p>
<p>&nbsp;values.pop_front();<br>&nbsp;cout &lt;&lt; "\nAfter pop_front values contains: ";<br>&nbsp;copy(values.begin(),values.end(),output);</p>
<p>&nbsp;values[1]=5.4;<br>&nbsp;cout &lt;&lt; "\nAfter values[1]=5.4 values contains: ";<br>&nbsp;copy(values.begin(),values.end(),output);<br>&nbsp;cout &lt;&lt; endl;<br>&nbsp;return 0;<br>}</p>
</div>
<img src ="http://www.cppblog.com/izualzhy/aggbug/132326.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2010-11-03 20:37 <a href="http://www.cppblog.com/izualzhy/archive/2010/11/03/132326.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>list顺序容器</title><link>http://www.cppblog.com/izualzhy/archive/2010/11/03/132302.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 03 Nov 2010 09:04:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2010/11/03/132302.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/132302.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2010/11/03/132302.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/132302.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/132302.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 出了常用的函数外，包括了list提供的splice,push_front,po_front,remove,unique,merge,reverse和sort。演示了list类的几个特性。&nbsp;&nbsp;<a href='http://www.cppblog.com/izualzhy/archive/2010/11/03/132302.html'>阅读全文</a><img src ="http://www.cppblog.com/izualzhy/aggbug/132302.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2010-11-03 17:04 <a href="http://www.cppblog.com/izualzhy/archive/2010/11/03/132302.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>迭代器简介</title><link>http://www.cppblog.com/izualzhy/archive/2010/11/03/132285.html</link><dc:creator>izualzhy</dc:creator><author>izualzhy</author><pubDate>Wed, 03 Nov 2010 06:07:00 GMT</pubDate><guid>http://www.cppblog.com/izualzhy/archive/2010/11/03/132285.html</guid><wfw:comment>http://www.cppblog.com/izualzhy/comments/132285.html</wfw:comment><comments>http://www.cppblog.com/izualzhy/archive/2010/11/03/132285.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/izualzhy/comments/commentRss/132285.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/izualzhy/services/trackbacks/132285.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 入门的例子&nbsp;&nbsp;<a href='http://www.cppblog.com/izualzhy/archive/2010/11/03/132285.html'>阅读全文</a><img src ="http://www.cppblog.com/izualzhy/aggbug/132285.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/izualzhy/" target="_blank">izualzhy</a> 2010-11-03 14:07 <a href="http://www.cppblog.com/izualzhy/archive/2010/11/03/132285.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>