{"id":203,"date":"2012-10-23T14:16:00","date_gmt":"2012-10-23T06:16:00","guid":{"rendered":"http:\/\/nike0good.jp1.rpvhost.net\/108"},"modified":"2012-10-23T14:16:00","modified_gmt":"2012-10-23T06:16:00","slug":"using_stl","status":"publish","type":"post","link":"https:\/\/nike0good.com\/?p=203","title":{"rendered":"using STL"},"content":{"rendered":"<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" width=\"100%\">\n<tbody>\n<tr>\n<td colspan=\"2\">\n<table border=\"2\" cellspacing=\"2\" cellpadding=\"2\" width=\"100%\" height=\"58\">\n<tbody>\n<tr>\n<td bgcolor=\"#d0e0ff\">\u4e09\u5341\u5206\u949f\u638c\u63e1STL<\/td>\n<\/tr>\n<tr>\n<td>\u8fd9\u662f\u672c\u5c0f\u4eba\u4e66\u3002\u539f\u540d\u662f\u300ausing stl\u300b\uff0c\u4e0d\u77e5\u9053\u662f\u8c01\u5199\u7684\u3002\u4e0d\u8fc7\u6211\u5012\u89c9\u5f97\u5f88\u6709\u8da3\uff0c\u6240\u4ee5\u5316\u4e86\u4e24\u4e2a\u665a\u4e0a\u628a\u5b83\u7ffb\u8bd1\u51fa\u6765\u3002\u6211\u6ca1\u6709\u5bf9\u7ffb\u8bd1\u51fa\u6765\u7684\u5185\u5bb9\u6821\u9a8c\u8fc7\u3002\u5982\u679c\u4f60\u6ca1\u6cd5\u5728\u4e09\u5341\u5206\u949f\u5185\u89c9\u5f97\u6709\u6240\u6536\u83b7\uff0c\u90a3\u4e48\u8d76\u7d27\u6254\u4e86\u5b83\u3002\u6587\u4e2d\u6211\u7701\u7565\u4e86\u5f88\u591a\u4e1c\u897f\u3002\u5fc3\u75bc\u90a3\uff0c\u6d6a\u8d39\u6211\u4e24\u4e2a\u665a\u4e0a\u3002<\/p>\n<p>\u8bd1\u8005\uff1akary<\/p>\n<p>contact:karymay@163.net<\/p>\n<h2>STL\u6982\u8ff0<\/h2>\n<p>STL\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u70b9\u662f\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u5206\u79bb\u3002\u5c3d\u7ba1\u8fd9\u662f\u4e2a\u7b80\u5355\u7684\u6982\u5ff5\uff0c\u4f46\u8fd9\u79cd\u5206\u79bb\u786e\u5b9e\u4f7f\u5f97STL\u53d8\u5f97\u975e\u5e38\u901a\u7528\u3002\u4f8b\u5982\uff0c\u7531\u4e8eSTL\u7684sort()\u51fd\u6570\u662f\u5b8c\u5168\u901a\u7528\u7684\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u6765\u64cd\u4f5c\u51e0\u4e4e\u4efb\u4f55\u6570\u636e\u96c6\u5408\uff0c\u5305\u62ec\u94fe\u8868\uff0c\u5bb9\u5668\u548c\u6570\u7ec4\u3002<\/p>\n<h3>\u8981\u70b9<\/h3>\n<p>STL\u7b97\u6cd5\u4f5c\u4e3a\u6a21\u677f\u51fd\u6570\u63d0\u4f9b\u3002\u4e3a\u4e86\u548c\u5176\u4ed6\u7ec4\u4ef6\u76f8\u533a\u522b\uff0c\u5728\u672c\u4e66\u4e2dSTL\u7b97\u6cd5\u4ee5\u540e\u63a5\u4e00\u5bf9\u5706\u62ec\u5f27\u7684\u65b9\u5f0f\u8868\u793a\uff0c\u4f8b\u5982sort()\u3002<\/p>\n<p>STL\u53e6\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u5b83\u4e0d\u662f\u9762\u5411\u5bf9\u8c61\u7684\u3002\u4e3a\u4e86\u5177\u6709\u8db3\u591f\u901a\u7528\u6027\uff0cSTL\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6a21\u677f\u800c\u4e0d\u662f\u5c01\u88c5\uff0c\u7ee7\u627f\u548c\u865a\u51fd\u6570\uff08\u591a\u6001\u6027\uff09\u2014\u2014OOP\u7684\u4e09\u4e2a\u8981\u7d20\u3002\u4f60\u5728STL\u4e2d\u627e\u4e0d\u5230\u4efb\u4f55\u660e\u663e\u7684\u7c7b\u7ee7\u627f\u5173\u7cfb\u3002\u8fd9\u597d\u50cf\u662f\u4e00\u79cd\u5012\u9000\uff0c\u4f46\u8fd9\u6b63\u597d\u662f\u4f7f\u5f97STL\u7684\u7ec4\u4ef6\u5177\u6709\u5e7f\u6cdb\u901a\u7528\u6027\u7684\u5e95\u5c42\u7279\u5f81\u3002\u53e6\u5916\uff0c\u7531\u4e8eSTL\u662f\u57fa\u4e8e\u6a21\u677f\uff0c\u5185\u8054\u51fd\u6570\u7684\u4f7f\u7528\u4f7f\u5f97\u751f\u6210\u7684\u4ee3\u7801\u77ed\u5c0f\u9ad8\u6548\u3002<\/p>\n<p>\u63d0\u793a<\/p>\n<p>\u786e\u4fdd\u5728\u7f16\u8bd1\u4f7f\u7528\u4e86STL\u7684\u7a0b\u5e8f\u4e2d\u81f3\u5c11\u8981\u4f7f\u7528-O\u4f18\u5316\u6765\u4fdd\u8bc1\u5185\u8054\u6269\u5c55\u3002STL\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6a21\u677f\u7c7b\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u5728OOP\u548c\u5e38\u89c4\u7f16\u7a0b\u4e2d\u4f7f\u7528\u3002\u6240\u6709\u7684STL\u7684\u5927\u7ea650\u4e2a\u7b97\u6cd5\u90fd\u662f\u5b8c\u5168\u901a\u7528\u7684\uff0c\u800c\u4e14\u4e0d\u4f9d\u8d56\u4e8e\u4efb\u4f55\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\u3002\u4e0b\u9762\u7684\u5c0f\u8282\u8bf4\u660e\u4e86\u4e09\u4e2a\u57fa\u672c\u7684STL\u7ec4\u4ef6\uff1a<\/p>\n<p>1\uff09&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u8bbf\u95ee\u5bb9\u5668\u4e2d\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u5bf9\u8fed\u4ee3\u5668\u6307\u5b9alist\u6216vector\u4e2d\u7684\u4e00\u5b9a\u8303\u56f4\u7684\u5bf9\u8c61\u3002\u8fed\u4ee3\u5668\u5c31\u5982\u540c\u4e00\u4e2a\u6307\u9488\u3002\u4e8b\u5b9e\u4e0a\uff0cC&#43;&#43;\u7684\u6307\u9488\u4e5f\u662f\u4e00\u79cd\u8fed\u4ee3\u5668\u3002\u4f46\u662f\uff0c\u8fed\u4ee3\u5668\u4e5f\u53ef\u4ee5\u662f\u90a3\u4e9b\u5b9a\u4e49\u4e86operator*()\u4ee5\u53ca\u5176\u4ed6\u7c7b&#20284;\u4e8e\u6307\u9488\u7684\u64cd\u4f5c\u7b26\u5730\u65b9\u6cd5\u7684\u7c7b\u5bf9\u8c61\u3002<\/p>\n<p>2\uff09&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u5bb9\u5668\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5982list\uff0cvector\uff0c\u548cdeques \uff0c\u4ee5\u6a21\u677f\u7c7b\u7684\u65b9\u6cd5\u63d0\u4f9b\u3002\u4e3a\u4e86\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7531\u5bb9\u5668\u7c7b\u8f93\u51fa\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<p>3\uff09&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \u7b97\u6cd5\u662f\u7528\u6765\u64cd\u4f5c\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u7684\u6a21\u677f\u51fd\u6570\u3002\u4f8b\u5982\uff0cSTL\u7528sort()\u6765\u5bf9\u4e00\u4e2avector\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u7528find()\u6765\u641c\u7d22\u4e00\u4e2alist\u4e2d\u7684\u5bf9\u8c61\u3002\u51fd\u6570\u672c\u8eab\u4e0e\u4ed6\u4eec\u64cd\u4f5c\u7684\u6570\u636e\u7684\u7ed3\u6784\u548c\u7c7b\u578b\u65e0\u5173\uff0c\u56e0\u6b64\u4ed6\u4eec\u53ef\u4ee5\u5728\u4ece\u7b80\u5355\u6570\u7ec4\u5230\u9ad8\u5ea6\u590d\u6742\u5bb9\u5668\u7684\u4efb\u4f55\u6570\u636e\u7ed3\u6784\u4e0a\u4f7f\u7528\u3002<\/p>\n<h3>\u5934\u6587\u4ef6<\/h3>\n<p>\u4e3a\u4e86\u907f\u514d\u548c\u5176\u4ed6\u5934\u6587\u4ef6\u51b2\u7a81\uff0c STL\u7684\u5934\u6587\u4ef6\u4e0d\u518d\u4f7f\u7528\u5e38\u89c4\u7684.h\u6269\u5c55\u3002\u4e3a\u4e86\u5305\u542b\u6807\u51c6\u7684string\u7c7b\uff0c\u8fed\u4ee3\u5668\u548c\u7b97\u6cd5\uff0c\u7528\u4e0b\u9762\u7684\u6307\u793a\u7b26\uff1a<\/p>\n<p>#include &lt;string&gt;<\/p>\n<p>#include &lt;iterator&gt;<\/p>\n<p>#include &lt;algorithm&gt;<\/p>\n<\/p>\n<p>\u5982\u679c\u4f60\u67e5\u770bSTL\u7684\u5934\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u770b\u5230\u8c61iterator.h\u548cstl_iterator.h\u8fd9\u6837\u7684\u5934\u6587\u4ef6\u3002\u7531\u4e8e\u8fd9\u4e9b\u540d\u5b57\u5728\u5404\u79cdSTL\u5b9e\u73b0\u4e4b\u95f4\u90fd\u53ef\u80fd\u4e0d\u540c\uff0c\u4f60\u5e94\u8be5\u907f\u514d\u4f7f\u7528\u8fd9\u4e9b\u540d\u5b57\u6765\u5f15\u7528\u8fd9\u4e9b\u5934\u6587\u4ef6\u3002\u4e3a\u4e86\u786e\u4fdd\u53ef\u79fb\u690d\u6027\uff0c\u4f7f\u7528\u76f8\u5e94\u7684\u6ca1\u6709.h\u540e\u7f00\u7684\u6587\u4ef6\u540d\u3002\u88681\u5217\u51fa\u4e86\u6700\u5e38\u4f7f\u7528\u7684\u5404\u79cd\u5bb9\u5668\u7c7b\u7684\u5934\u6587\u4ef6\u3002\u8be5\u8868\u5e76\u4e0d\u5b8c\u6574\uff0c\u5bf9\u4e8e\u5176\u4ed6\u5934\u6587\u4ef6\uff0c\u6211\u5c06\u5728\u672c\u7ae0\u548c\u540e\u9762\u7684\u4e24\u7ae0\u4e2d\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p align=\"center\"><strong>\u8868 1. STL\u5934\u6587\u4ef6\u548c\u5bb9\u5668\u7c7b<\/strong><\/p>\n<div align=\"center\">\n<table border=\"1\" cellspacing=\"3\" cellpadding=\"0\">\n<thead>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt><strong>#include<\/strong><\/tt><strong><\/strong><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><strong>Container Class<\/strong><\/p>\n<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;deque&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>deque<\/tt><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;list&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>list<\/tt><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;map&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>map, multimap<\/tt><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;queue&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>queue, priority_queue<\/tt><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;set&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>set, multiset<\/tt><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;stack&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>stack<\/tt><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\n<p align=\"center\"><tt>&lt;vector&gt;<\/tt><\/p>\n<\/td>\n<td valign=\"top\">\n<p align=\"center\"><tt>vector, vector&lt;bool&gt;<\/tt><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>\u540d\u5b57\u7a7a\u95f4<\/h3>\n<p>\u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u4e0d\u80fd\u8bc6\u522b\u540d\u5b57\u7a7a\u95f4\u3002\u540d\u5b57\u7a7a\u95f4\u5c31\u597d\u50cf\u4e00\u4e2a\u4fe1\u5c01\uff0c\u5c06\u6807\u5fd7\u7b26\u5c01\u88c5\u5728\u53e6\u4e00\u4e2a\u540d\u5b57\u4e2d\u3002\u6807\u5fd7\u7b26\u53ea\u5728\u540d\u5b57\u7a7a\u95f4\u4e2d\u5b58\u5728\uff0c\u56e0\u800c\u907f\u514d\u4e86\u548c\u5176\u4ed6\u6807\u5fd7\u7b26\u51b2\u7a81\u3002\u4f8b\u5982\uff0c\u53ef\u80fd\u6709\u5176\u4ed6\u5e93\u548c\u7a0b\u5e8f\u6a21\u5757\u5b9a\u4e49\u4e86sort()\u51fd\u6570\uff0c\u4e3a\u4e86\u907f\u514d\u548cSTL\u5730sort()\u7b97\u6cd5\u51b2\u7a81\uff0cSTL\u7684sort()\u4ee5\u53ca\u5176\u4ed6\u6807\u5fd7\u7b26\u90fd\u5c01\u88c5\u5728\u540d\u5b57\u7a7a\u95f4std\u4e2d\u3002STL\u7684sort()\u7b97\u6cd5\u7f16\u8bd1\u4e3astd::sort()\uff0c\u4ece\u800c\u907f\u514d\u4e86\u540d\u5b57\u51b2\u7a81\u3002<\/p>\n<p>\u5c3d\u7ba1\u4f60\u7684\u7f16\u8bd1\u5668\u53ef\u80fd\u6ca1\u6709\u5b9e\u73b0\u540d\u5b57\u7a7a\u95f4\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u4ed6\u4eec\u3002\u4e3a\u4e86\u4f7f\u7528STL\uff0c\u53ef\u4ee5\u5c06\u4e0b\u9762\u7684\u6307\u793a\u7b26\u63d2\u5165\u5230\u4f60\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u4e2d\uff0c\u5178\u578b\u5730\u662f\u5728\u6240\u6709\u7684#include\u6307\u793a\u7b26\u7684\u540e\u9762\uff1a<\/p>\n<\/p>\n<p>using namespace std;<\/p>\n<\/p>\n<\/p>\n<h2>\u8fed\u4ee3\u5668<\/h2>\n<p>\u8fed\u4ee3\u5668\u63d0\u4f9b\u5bf9\u4e00\u4e2a\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u7684\u8bbf\u95ee\u65b9\u6cd5\uff0c\u5e76\u4e14\u5b9a\u4e49\u4e86\u5bb9\u5668\u4e2d\u5bf9\u8c61\u7684\u8303\u56f4\u3002\u8fed\u4ee3\u5668\u5c31\u5982\u540c\u4e00\u4e2a\u6307\u9488\u3002\u4e8b\u5b9e\u4e0a\uff0cC&#43;&#43;\u7684\u6307\u9488\u4e5f\u662f\u4e00\u79cd\u8fed\u4ee3\u5668\u3002\u4f46\u662f\uff0c\u8fed\u4ee3\u5668\u4e0d\u4ec5\u4ec5\u662f\u6307\u9488\uff0c\u56e0\u6b64\u4f60\u4e0d\u80fd\u8ba4\u4e3a\u4ed6\u4eec\u4e00\u5b9a\u5177\u6709\u5730\u5740&#20540;\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u6570\u7ec4\u7d22\u5f15\uff0c\u4e5f\u53ef\u4ee5\u8ba4\u4e3a\u662f\u4e00\u79cd\u8fed\u4ee3\u5668\u3002<\/p>\n<p>\u8fed\u4ee3\u5668\u6709\u5404\u79cd\u4e0d\u540c\u7684\u521b\u5efa\u65b9\u6cd5\u3002\u7a0b\u5e8f\u53ef\u80fd\u628a\u8fed\u4ee3\u5668\u4f5c\u4e3a\u4e00\u4e2a\u53d8\u91cf\u521b\u5efa\u3002\u4e00\u4e2aSTL\u5bb9\u5668\u7c7b\u53ef\u80fd\u4e3a\u4e86\u4f7f\u7528\u4e00\u4e2a\u7279\u5b9a\u7c7b\u578b\u7684\u6570\u636e\u800c\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4f5c\u4e3a\u6307\u9488\uff0c\u5fc5\u987b\u80fd\u591f\u4f7f\u7528*\u64cd\u4f5c\u7b26\u7c7b\u83b7\u53d6\u6570\u636e\u3002\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u6570\u5b66\u64cd\u4f5c\u7b26\u5982&#43;&#43;\u3002\u5178\u578b\u7684\uff0c&#43;&#43;\u64cd\u4f5c\u7b26\u7528\u6765\u9012\u589e\u8fed\u4ee3\u5668\uff0c\u4ee5\u8bbf\u95ee\u5bb9\u5668\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5bf9\u8c61\u3002\u5982\u679c\u8fed\u4ee3\u5668\u5230\u8fbe\u4e86\u5bb9\u5668\u4e2d\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u540e\u9762\uff0c\u5219\u8fed\u4ee3\u5668\u53d8\u6210past-the-end&#20540;\u3002\u4f7f\u7528\u4e00\u4e2apast-the-end&#20540;\u5f97\u6307\u9488\u6765\u8bbf\u95ee\u5bf9\u8c61\u662f\u975e\u6cd5\u7684\uff0c\u5c31\u597d\u50cf\u4f7f\u7528NULL\u6216\u4e3a\u521d\u59cb\u5316\u7684\u6307\u9488\u4e00\u6837\u3002<\/p>\n<p>\u63d0\u793a<\/p>\n<p>STL\u4e0d\u4fdd\u8bc1\u53ef\u4ee5\u4ece\u53e6\u4e00\u4e2a\u8fed\u4ee3\u5668\u6765\u62b5\u8fbe\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4f8b\u5982\uff0c\u5f53\u5bf9\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u5bf9\u8c61\u6392\u5e8f\u65f6\uff0c\u5982\u679c\u4f60\u5728\u4e0d\u540c\u7684\u7ed3\u6784\u4e2d\u6307\u5b9a\u4e86\u4e24\u4e2a\u8fed\u4ee3\u5668\uff0c\u7b2c\u4e8c\u4e2a\u8fed\u4ee3\u5668\u65e0\u6cd5\u4ece\u7b2c\u4e00\u4e2a\u8fed\u4ee3\u5668\u62b5\u8fbe\uff0c\u6b64\u65f6\u7a0b\u5e8f\u6ce8\u5b9a\u8981\u5931\u8d25\u3002\u8fd9\u662fSTL\u7075\u6d3b\u6027\u7684\u4e00\u4e2a\u4ee3\u4ef7\u3002<em>STL\u4e0d\u4fdd\u8bc1\u68c0\u6d4b\u6beb\u65e0\u9053\u7406\u7684\u9519\u8bef\u3002<\/em><\/p>\n<h3>\u8fed\u4ee3\u5668\u7684\u7c7b\u578b<\/h3>\n<p>\u5bf9\u4e8eSTL\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e94\u79cd\u8fed\u4ee3\u5668\u3002\u4e0b\u9762\u7b80\u8981\u8bf4\u660e\u4e86\u8fd9\u4e94\u79cd\u7c7b\u578b\uff1a<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Input iterators<\/em> \u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u53ea\u8bfb\u8bbf\u95ee\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Output iterators<\/em> \u63d0\u4f9b\u5bf9\u6570\u636e\u7684\u53ea\u5199\u8bbf\u95ee<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Forward iterators<\/em> \u63d0\u4f9b\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e76\u80fd\u5411\u524d\u63a8\u8fdb\u8fed\u4ee3\u5668\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Bidirectional iterators<\/em>\u63d0\u4f9b\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e76\u80fd\u5411\u524d\u548c\u5411\u540e\u64cd\u4f5c\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Random access iterators<\/em>\u63d0\u4f9b\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e76\u80fd\u5728\u6570\u636e\u4e2d\u968f\u673a\u79fb\u52a8\u3002<\/p>\n<p>\u5c3d\u7ba1\u5404\u79cd\u4e0d\u540c\u7684STL\u5b9e\u73b0\u7ec6\u8282\u65b9\u9762\u6709\u6240\u4e0d\u540c\uff0c\u8fd8\u662f\u53ef\u4ee5\u5c06\u4e0a\u9762\u7684\u8fed\u4ee3\u5668\u60f3\u8c61\u4e3a\u4e00\u79cd\u7c7b\u7ee7\u627f\u5173\u7cfb\u3002\u4ece\u8fd9\u4e2a\u610f\u4e49\u4e0a\u8bf4\uff0c\u4e0b\u9762\u7684\u8fed\u4ee3\u5668\u7ee7\u627f\u81ea\u4e0a\u9762\u7684\u8fed\u4ee3\u5668\u3002\u7531\u4e8e\u8fd9\u79cd\u7ee7\u627f\u5173\u7cfb\uff0c\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2aForward\u8fed\u4ee3\u5668\u4f5c\u4e3a\u4e00\u4e2aoutput\u6216input\u8fed\u4ee3\u5668\u4f7f\u7528\u3002\u540c\u6837\uff0c\u5982\u679c\u4e00\u4e2a\u7b97\u6cd5\u8981\u6c42\u662f\u4e00\u4e2abidirectional \u8fed\u4ee3\u5668\uff0c\u90a3\u4e48\u53ea\u80fd\u4f7f\u7528\u8be5\u79cd\u7c7b\u578b\u548c\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\u3002\n<\/p>\n<h3>\u6307\u9488\u8fed\u4ee3\u5668<\/h3>\n<p>\u6b63\u5982\u4e0b\u9762\u7684\u5c0f\u7a0b\u5e8f\u663e\u793a\u7684\uff0c\u4e00\u4e2a\u6307\u9488\u4e5f\u662f\u4e00\u79cd\u8fed\u4ee3\u5668\u3002\u8be5\u7a0b\u5e8f\u540c\u6837\u663e\u793a\u4e86STL\u7684\u4e00\u4e2a\u4e3b\u8981\u7279\u6027\u2014\u2014\u5b83\u4e0d\u53ea\u662f\u80fd\u591f\u7528\u4e8e\u5b83\u81ea\u5df1\u7684\u7c7b\u7c7b\u578b\uff0c\u800c\u4e14\u4e5f\u80fd\u7528\u4e8e\u4efb\u4f55C\u6216C&#43;&#43;\u7c7b\u578b\u3002<a href=\"http:\/\/www.csdn.net\/editor\/Editor.htm#Listing 1\">Listing 1<\/a>, iterdemo.cpp, \u663e\u793a\u4e86\u5982\u4f55\u628a\u6307\u9488\u4f5c\u4e3a\u8fed\u4ee3\u5668\u7528\u4e8eSTL\u7684find()\u7b97\u6cd5\u6765\u641c\u7d22\u666e\u901a\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>\u8868 1. iterdemo.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;algorithm&gt;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>#define SIZE 100<\/pre>\n<pre>int iarray[SIZE];<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; iarray[20] = 50;<\/pre>\n<pre>&nbsp; int* ip = find(iarray, iarray &#43; SIZE, 50);<\/pre>\n<pre>&nbsp; if (ip == iarray &#43; SIZE)<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &quot;50 not found in array&quot; &lt;&lt; endl;<\/pre>\n<pre>&nbsp; else<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; cout &lt;&lt; *ip &lt;&lt; &quot; found in array&quot; &lt;&lt; endl;<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<p>\u5728\u5f15\u7528\u4e86I\/O\u6d41\u5e93\u548cSTL\u7b97\u6cd5\u5934\u6587\u4ef6\uff08\u6ce8\u610f\u6ca1\u6709.h\u540e\u7f00\uff09\uff0c\u8be5\u7a0b\u5e8f\u544a\u8bc9\u7f16\u8bd1\u5668\u4f7f\u7528std\u540d\u5b57\u7a7a\u95f4\u3002\u4f7f\u7528std\u540d\u5b57\u7a7a\u95f4\u7684\u8fd9\u884c\u662f\u53ef\u9009\u7684\uff0c\u56e0\u4e3a\u53ef\u4ee5\u5220\u9664\u8be5\u884c\u5bf9\u4e8e\u8fd9\u4e48\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\u6765\u8bf4\u4e0d\u4f1a\u5bfc\u81f4\u540d\u5b57\u51b2\u7a81\u3002<\/p>\n<p>\u7a0b\u5e8f\u4e2d\u5b9a\u4e49\u4e86\u5c3a\u5bf8\u4e3aSIZE\u7684\u5168\u5c40\u6570\u7ec4\u3002\u7531\u4e8e\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\u8fd0\u884c\u65f6\u6570\u7ec4\u81ea\u52a8\u521d\u59cb\u5316\u4e3a\u96f6\u3002\u4e0b\u9762\u7684\u8bed\u53e5\u5c06\u5728\u7d22\u5f1520\u4f4d\u7f6e\u5904\u5730\u5143\u7d20\u8bbe\u7f6e\u4e3a50,\u5e76\u4f7f\u7528find()\u7b97\u6cd5\u6765\u641c\u7d22&#20540;50:\n<\/p>\n<pre>iarray[20] = 50;<\/pre>\n<pre>int* ip = find(iarray, iarray &#43; SIZE, 50);<\/pre>\n<p><tt>find()\u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\u3002\u5934\u4e24\u4e2a\u5b9a\u4e49\u4e86\u641c\u7d22\u7684\u8303\u56f4\u3002\u7531\u4e8eC\u548cC&#43;&#43;\u6570\u7ec4\u7b49\u540c\u4e8e\u6307\u9488\uff0c\u8868\u8fbe\u5f0fiarray\u6307\u5411\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u3002\u800c\u7b2c\u4e8c\u4e2a\u53c2\u6570iarray &#43; SIZE\u7b49\u540c\u4e8e<\/tt><em>past-the-end<\/em> &#20540;\uff0c\u4e5f\u5c31\u662f\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u540e\u9762\u4f4d\u7f6e\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u5f85\u5b9a\u4f4d\u7684&#20540;\uff0c\u4e5f\u5c31\u662f50\u3002<tt>find()\u51fd\u6570\u8fd4\u56de\u548c\u524d\u4e24\u4e2a\u53c2\u6570\u76f8\u540c\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\uff0c\u8fd9\u513f\u662f\u4e00\u4e2a\u6307\u5411\u6574\u6570\u7684\u6307\u9488ip\u3002<\/tt>\n<\/p>\n<p><strong>\u63d0\u793a<\/strong><\/p>\n<p>\u5fc5\u987b\u8bb0\u4f4fSTL\u4f7f\u7528\u6a21\u677f\u3002\u56e0\u6b64\uff0cSTL\u51fd\u6570\u81ea\u52a8\u6839\u636e\u5b83\u4eec\u4f7f\u7528\u7684\u6570\u636e\u7c7b\u578b\u6765\u6784\u9020\u3002<\/p>\n<p>\u4e3a\u4e86\u5224\u65ad<tt>find()\u662f\u5426\u6210\u529f\uff0c\u4f8b\u5b50\u4e2d\u6d4b\u8bd5ip\u548c<\/tt> <em>past-the-end<\/em> &#20540;\u662f\u5426\u76f8\u7b49\uff1a <\/p>\n<pre>if (ip == iarray &#43; SIZE) ...<\/pre>\n<p>\u5982\u679c\u8868\u8fbe\u5f0f\u4e3a\u771f\uff0c\u5219\u8868\u793a\u5728\u641c\u7d22\u7684\u8303\u56f4\u5185\u6ca1\u6709\u6307\u5b9a\u7684&#20540;\u3002\u5426\u5219\u5c31\u662f\u6307\u5411\u4e00\u4e2a\u5408\u6cd5\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u8fd9\u65f6\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u8bed\u53e5\u663e\u793a\uff1a:<\/p>\n<pre>cout &lt;&lt; *ip &lt;&lt; &quot; found in array&quot; &lt;&lt; endl;<\/pre>\n<p>\u6d4b\u8bd5\u51fd\u6570\u8fd4\u56de&#20540;\u548cNULL\u662f\u5426\u76f8\u7b49\u662f\u4e0d\u6b63\u786e\u7684\u3002\u4e0d\u8981\u8c61\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre>int* ip = find(iarray, iarray &#43; SIZE, 50);<\/pre>\n<pre>if (ip != NULL) ...&nbsp; \/\/ ??? incorrect<\/pre>\n<p>\u5f53\u4f7f\u7528STL\u51fd\u6570\u65f6\uff0c\u53ea\u80fd\u6d4b\u8bd5ip\u662f\u5426\u548c<em>past-the-end<\/em> &#20540;\u662f\u5426\u76f8\u7b49\u3002\u5c3d\u7ba1\u5728\u672c\u4f8b\u4e2dip\u662f\u4e00\u4e2aC&#43;&#43;\u6307\u9488,\u5176\u7528\u6cd5\u4e5f\u5fc5\u987b\u7b26\u5408STL\u8fed\u4ee3\u5668\u7684\u89c4\u5219\u3002<\/p>\n<h3>\u5bb9\u5668\u8fed\u4ee3\u5668<\/h3>\n<p>\u5c3d\u7ba1C&#43;&#43;\u6307\u9488\u4e5f\u662f\u8fed\u4ee3\u5668\uff0c\u4f46\u7528\u7684\u66f4\u591a\u7684\u662f\u5bb9\u5668\u8fed\u4ee3\u5668\u3002\u5bb9\u5668\u8fed\u4ee3\u5668\u7528\u6cd5\u548citerdemo.cpp\u4e00\u6837\uff0c\u4f46\u548c\u5c06\u8fed\u4ee3\u5668\u7533\u660e\u4e3a\u6307\u9488\u53d8\u91cf\u4e0d\u540c\u7684\u662f\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5bb9\u5668\u7c7b\u65b9\u6cd5\u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002\u4e24\u4e2a\u5178\u578b\u7684\u5bb9\u5668\u7c7b\u65b9\u6cd5\u662fbegin()\u548cend()\u3002\u5b83\u4eec\u5728\u5927\u591a\u6570\u5bb9\u5668\u4e2d\u8868\u793a\u6574\u4e2a\u5bb9\u5668\u8303\u56f4\u3002\u5176\u4ed6\u4e00\u4e9b\u5bb9\u5668\u8fd8\u4f7f\u7528rbegin()\u548crend()\u65b9\u6cd5\u63d0\u4f9b\u53cd\u5411\u8fed\u4ee3\u5668\uff0c\u4ee5\u6309\u53cd\u5411\u987a\u5e8f\u6307\u5b9a\u5bf9\u8c61\u8303\u56f4\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u7a0b\u5e8f\u521b\u5efa\u4e86\u4e00\u4e2a\u77e2\u91cf\u5bb9\u5668\uff08STL\u7684\u548c\u6570\u7ec4\u7b49\u4ef7\u7684\u5bf9\u8c61\uff09\uff0c\u5e76\u4f7f\u7528\u8fed\u4ee3\u5668\u5728\u5176\u4e2d\u641c\u7d22\u3002\u8be5\u7a0b\u5e8f\u548c\u524d\u4e00\u7ae0\u4e2d\u7684\u7a0b\u5e8f\u76f8\u540c\u3002<\/p>\n<p><strong>Listing 2. vectdemo.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;algorithm&gt;<\/pre>\n<pre>#include &lt;vector&gt;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>vector&lt;int&gt; intVector(100);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>void main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; intVector[20] = 50;<\/pre>\n<pre>&nbsp; vector&lt;int&gt;::iterator intIter =<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; find(intVector.begin(), intVector.end(), 50);<\/pre>\n<pre>&nbsp; if (intIter != intVector.end())<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &quot;Vector contains value &quot; &lt;&lt; *intIter &lt;&lt; endl;<\/pre>\n<pre>&nbsp; else<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; cout &lt;&lt; &quot;Vector does not contain 50&quot; &lt;&lt; endl;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<p>\u6ce8\u610f\u7528\u4e0b\u9762\u7684\u65b9\u6cd5\u663e\u793a\u641c\u7d22\u5230\u7684\u6570\u636e\uff1a<\/p>\n<pre>cout &lt;&lt; &quot;Vector contains value &quot; &lt;&lt; *intIter &lt;&lt; endl;<\/pre>\n<h3>\u5e38\u91cf\u8fed\u4ee3\u5668<\/h3>\n<p>\u548c\u6307\u9488\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u7ed9\u4e00\u4e2a\u8fed\u4ee3\u5668\u8d4b&#20540;\u3002\u4f8b\u5982\uff0c\u9996\u5148\u7533\u660e\u4e00\u4e2a\u8fed\u4ee3\u5668\uff1a<\/p>\n<pre>vector&lt;int&gt;::iterator first;<\/pre>\n<p>\u8be5\u8bed\u53e5\u521b\u5efa\u4e86\u4e00\u4e2avector&lt;int&gt;\u7c7b\u7684\u8fed\u4ee3\u5668\u3002\u4e0b\u9762\u7684\u8bed\u53e5\u5c06\u8be5\u8fed\u4ee3\u5668\u8bbe\u7f6e\u5230intVector\u7684\u7b2c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u5c06\u5b83\u6307\u5411\u7684\u5bf9\u8c61&#20540;\u8bbe\u7f6e\u4e3a123\uff1a:<\/p>\n<pre>first = intVector.begin();<\/pre>\n<pre>*first = 123;<\/pre>\n<p>\u8fd9\u79cd\u8d4b&#20540;\u5bf9\u4e8e\u5927\u591a\u6570\u5bb9\u5668\u7c7b\u90fd\u662f\u5141\u8bb8\u7684\uff0c\u9664\u4e86\u53ea\u8bfb\u53d8\u91cf\u3002\u4e3a\u4e86\u9632\u6b62\u9519\u8bef\u8d4b&#20540;\uff0c\u53ef\u4ee5\u7533\u660e\u8fed\u4ee3\u5668\u4e3a\uff1a<\/p>\n<pre>const vector&lt;int&gt;::iterator result;<\/pre>\n<pre>result = find(intVector.begin(), intVector.end(), value);<\/pre>\n<pre>if (result != intVector.end())<\/pre>\n<pre>&nbsp; *result = 123;&nbsp; \/\/ ???<\/pre>\n<p><strong>\u8b66\u544a<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u9632\u6b62\u6570\u636e\u88ab\u6539\u53d8\u5f97\u65b9\u6cd5\u662f\u5c06\u5bb9\u5668\u7533\u660e\u4e3aconst\u7c7b\u578b\u3002<\/p>\n<p>\u300e\u5440\uff01\u5728VC\u4e2d\u6d4b\u8bd5\u51fa\u9519,\u6b63\u786e\u7684\u542b\u4e49\u662fresult\u6210\u4e3a\u5e38\u91cf\u800c\u4e0d\u662f\u5b83\u6307\u5411\u7684\u5bf9\u8c61\u4e0d\u5141\u8bb8\u6539\u53d8\uff0c\u5982\u540cint *const p;\u770b\u6765\u8fd9\u4f5c\u8005\u81ea\u5df1\u4e5f\u4e0d\u61c2\u300f <\/p>\n<h2>\u4f7f\u7528\u8fed\u4ee3\u5668\u7f16\u7a0b<\/h2>\n<p>\u4f60\u5df2\u7ecf\u89c1\u5230\u4e86\u8fed\u4ee3\u5668\u7684\u4e00\u4e9b\u4f8b\u5b50\uff0c\u73b0\u5728\u6211\u4eec\u5c06\u5173\u6ce8\u6bcf\u79cd\u7279\u5b9a\u7684\u8fed\u4ee3\u5668\u5982\u4f55\u4f7f\u7528\u3002\u7531\u4e8e\u4f7f\u7528\u8fed\u4ee3\u5668\u9700\u8981\u5173\u4e8eSTL\u5bb9\u5668\u7c7b\u548c\u7b97\u6cd5\u7684\u77e5\u8bc6\uff0c\u5728\u9605\u8bfb\u4e86\u540e\u9762\u7684\u4e24\u7ae0\u540e\u4f60\u53ef\u80fd\u9700\u8981\u91cd\u65b0\u590d\u4e60\u4e00\u4e0b\u672c\u7ae0\u5185\u5bb9\u3002<\/p>\n<h3>\u8f93\u5165\u8fed\u4ee3\u5668<\/h3>\n<p>\u8f93\u5165\u8fed\u4ee3\u5668\u662f\u6700\u666e\u901a\u7684\u7c7b\u578b\u3002\u8f93\u5165\u8fed\u4ee3\u5668\u81f3\u5c11\u80fd\u591f\u4f7f\u7528==\u548c!=\u6d4b\u8bd5\u662f\u5426\u76f8\u7b49\uff1b\u4f7f\u7528*\u6765\u8bbf\u95ee\u6570\u636e\uff1b\u4f7f\u7528&#43;&#43;\u64cd\u4f5c\u6765\u9012\u63a8\u8fed\u4ee3\u5668\u5230\u4e0b\u4e00\u4e2a\u5143\u7d20\u6216\u5230\u8fbe<em>past-the-end<\/em> &#20540;\u3002<\/p>\n<p>\u4e3a\u4e86\u7406\u89e3\u8fed\u4ee3\u5668\u548cSTL\u51fd\u6570\u662f\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u7684\uff0c\u73b0\u5728\u6765\u770b\u4e00\u4e0bfind()\u6a21\u677f\u51fd\u6570\u7684\u5b9a\u4e49\uff1a<\/p>\n<pre>template &lt;class InputIterator, class T&gt;<\/pre>\n<pre>InputIterator find(<\/pre>\n<pre>&nbsp; InputIterator first, InputIterator last, const T&amp; value) {<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; while (first != last &amp;&amp; *first != value) &#43;&#43;first;<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; return first;<\/pre>\n<pre>&nbsp; }<\/pre>\n<\/p>\n<p><strong>\u6ce8\u610f<\/strong><\/p>\n<p>\u5728find()\u7b97\u6cd5\u4e2d\uff0c\u6ce8\u610f\u5982\u679cfirst\u548clast\u6307\u5411\u4e0d\u540c\u7684\u5bb9\u5668\uff0c\u8be5\u7b97\u6cd5\u53ef\u80fd\u9677\u5165\u6b7b\u5faa\u73af\u3002<\/p>\n<h3>\u8f93\u51fa\u8fed\u4ee3\u5668<\/h3>\n<p>\u8f93\u51fa\u8fed\u4ee3\u5668\u7f3a\u7701\u53ea\u5199\uff0c\u901a\u5e38\u7528\u4e8e\u5c06\u6570\u636e\u4ece\u4e00\u4e2a\u4f4d\u7f6e\u62f7\u8d1d\u5230\u53e6\u4e00\u4e2a\u4f4d\u7f6e\u3002\u7531\u4e8e\u8f93\u51fa\u8fed\u4ee3\u5668\u65e0\u6cd5\u8bfb\u53d6\u5bf9\u8c61\uff0c\u56e0\u6b64\u4f60\u4e0d\u4f1a\u5728\u4efb\u4f55\u641c\u7d22\u548c\u5176\u4ed6\u7b97\u6cd5\u4e2d\u4f7f\u7528\u5b83\u3002\u8981\u60f3\u8bfb\u53d6\u4e00\u4e2a\u62f7\u8d1d\u7684&#20540;\uff0c\u5fc5\u987b\u4f7f\u7528\u53e6\u4e00\u4e2a\u8f93\u5165\u8fed\u4ee3\u5668\uff08\u6216\u5b83\u7684\u7ee7\u627f\u8fed\u4ee3\u5668\uff09\u3002<\/p>\n<p><strong>Listing 3. outiter.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;algorithm&gt;&nbsp;&nbsp; \/\/ Need copy()<\/pre>\n<pre>#include &lt;vector&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need vector<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>double darray[10] =<\/pre>\n<pre>&nbsp; {1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9};<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>vector&lt;double&gt; vdouble(10);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; vector&lt;double&gt;::iterator outputIterator = vdouble.begin();<\/pre>\n<pre>&nbsp; copy(darray, darray &#43; 10, outputIterator);<\/pre>\n<pre>&nbsp; while (outputIterator != vdouble.end()) {<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; cout &lt;&lt; *outputIterator &lt;&lt; endl;<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; outputIterator&#43;&#43;;<\/pre>\n<pre>&nbsp; }<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<p><strong>\u6ce8\u610f<\/strong> <\/p>\n<p>\u5f53\u4f7f\u7528copy()\u7b97\u6cd5\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u786e\u4fdd\u76ee\u6807\u5bb9\u5668\u6709\u8db3\u591f\u5927\u7684\u7a7a\u95f4\uff0c\u6216\u8005\u5bb9\u5668\u672c\u8eab\u662f\u81ea\u52a8\u6269\u5c55\u7684\u3002<\/p>\n<h3>\u524d\u63a8\u8fed\u4ee3\u5668<\/h3>\n<p>\u524d\u63a8\u8fed\u4ee3\u5668\u80fd\u591f\u8bfb\u5199\u6570\u636e&#20540;\uff0c\u5e76\u80fd\u591f\u5411\u524d\u63a8\u8fdb\u5230\u4e0b\u4e00\u4e2a&#20540;\u3002\u4f46\u662f\u6ca1\u6cd5\u9012\u51cf\u3002<tt>replace()\u7b97\u6cd5\u663e\u793a\u4e86\u524d\u63a8\u8fed\u4ee3\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/tt> <\/p>\n<pre>template &lt;class ForwardIterator, class T&gt;<\/pre>\n<pre>void replace (ForwardIterator first,<\/pre>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForwardIterator last,<\/pre>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const T&amp; old_value,<\/pre>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const T&amp; new_value);<\/pre>\n<p>\u4f7f\u7528<tt>replace()\u5c06[first,last]\u8303\u56f4\u5185\u7684\u6240\u6709&#20540;\u4e3aold_value\u7684\u5bf9\u8c61\u66ff\u6362\u4e3anew_value\u3002<\/tt><em>:<\/em><\/p>\n<pre>replace(vdouble.begin(), vdouble.end(), 1.5, 3.14159);<\/pre>\n<h3>\u53cc\u5411\u8fed\u4ee3\u5668<\/h3>\n<p>\u53cc\u5411\u8fed\u4ee3\u5668\u8981\u6c42\u80fd\u591f\u589e\u51cf\u3002\u5982reverse()\u7b97\u6cd5\u8981\u6c42\u4e24\u4e2a\u53cc\u5411\u8fed\u4ee3\u5668\u4f5c\u4e3a\u53c2\u6570:<\/p>\n<pre>template &lt;class BidirectionalIterator&gt;<\/pre>\n<pre>void reverse (BidirectionalIterator first,<\/pre>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BidirectionalIterator last);<\/pre>\n<p>\u4f7f\u7528<tt>reverse()\u51fd\u6570\u6765\u5bf9\u5bb9\u5668\u8fdb\u884c\u9006\u5411\u6392\u5e8f<\/tt>:<\/p>\n<pre>reverse(vdouble.begin(), vdouble.end());<\/pre>\n<h3>\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668<\/h3>\n<p>\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\u80fd\u591f\u4ee5\u4efb\u610f\u987a\u5e8f\u8bbf\u95ee\u6570\u636e\uff0c\u5e76\u80fd\u7528\u4e8e\u8bfb\u5199\u6570\u636e\uff08\u4e0d\u662fconst\u7684C&#43;&#43;\u6307\u9488\u4e5f\u662f\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\uff09\u3002STL\u7684\u6392\u5e8f\u548c\u641c\u7d22\u51fd\u6570\u4f7f\u7528\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\u3002\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\u53ef\u4ee5\u4f7f\u7528\u5173\u7cfb\u64cd\u4f5c\u7b26\u4f5c\u6bd4\u8f83\u3002<\/p>\n<p><tt>random_shuffle()<\/tt> \u51fd\u6570\u968f\u673a\u6253\u4e71\u539f\u5148\u7684\u987a\u5e8f\u3002\u7533\u660e\u4e3a\uff1a<\/p>\n<pre>template &lt;class RandomAccessIterator&gt;<\/pre>\n<pre>void random_shuffle (RandomAccessIterator first,<\/pre>\n<pre>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RandomAccessIterator last);<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre>random_shuffle(vdouble.begin(), vdouble.end());<\/pre>\n<h2>\u8fed\u4ee3\u5668\u6280\u672f<\/h2>\n<p>\u8981\u5b66\u4f1a\u4f7f\u7528\u8fed\u4ee3\u5668\u548c\u5bb9\u5668\u4ee5\u53ca\u7b97\u6cd5\uff0c\u9700\u8981\u5b66\u4e60\u4e0b\u9762\u7684\u65b0\u6280\u672f\u3002<\/p>\n<h3>\u6d41\u548c\u8fed\u4ee3\u5668<\/h3>\n<p>\u672c\u4e66\u7684\u5f88\u591a\u4f8b\u5b50\u7a0b\u5e8f\u4f7f\u7528I\/O\u6d41\u8bed\u53e5\u6765\u8bfb\u5199\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>int value;<\/pre>\n<pre>cout &lt;&lt; &quot;Enter value: &quot;;<\/pre>\n<pre>cin &gt;&gt; value;<\/pre>\n<pre>cout &lt;&lt; &quot;You entered &quot; &lt;&lt; value &lt;&lt; endl;<\/pre>\n<p>\u5bf9\u4e8e\u8fed\u4ee3\u5668\uff0c\u6709\u53e6\u4e00\u79cd\u65b9\u6cd5\u4f7f\u7528\u6d41\u548c\u6807\u51c6\u51fd\u6570\u3002\u7406\u89e3\u7684\u8981\u70b9\u662f\u5c06\u8f93\u5165\/\u8f93\u51fa\u6d41\u4f5c\u4e3a\u5bb9\u5668\u770b\u5f85\u3002\u56e0\u6b64\uff0c\u4efb\u4f55\u63a5\u53d7\u8fed\u4ee3\u5668\u53c2\u6570\u7684\u7b97\u6cd5\u90fd\u53ef\u4ee5\u548c\u6d41\u4e00\u8d77\u5de5\u4f5c\u3002 <\/p>\n<p><strong>Listing 4. outstrm.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;stdlib.h&gt;&nbsp;&nbsp;&nbsp; \/\/ Need random(), srandom()<\/pre>\n<pre>#include &lt;time.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need time()<\/pre>\n<pre>#include &lt;algorithm&gt;&nbsp;&nbsp; \/\/ Need sort(), copy()<\/pre>\n<pre>#include &lt;vector&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need vector<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>void Display(vector&lt;int&gt;&amp; v, const char* s);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; \/\/ Seed the random number generator<\/pre>\n<pre>&nbsp; srandom( time(NULL) );<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Construct vector and fill with random integer values<\/pre>\n<pre>&nbsp; vector&lt;int&gt; collection(10);<\/pre>\n<pre>&nbsp; for (int i = 0; i &lt; 10; i&#43;&#43;)<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; collection[i] = random() % 10000;;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Display, sort, and redisplay<\/pre>\n<pre>&nbsp; Display(collection, &quot;Before sorting&quot;);<\/pre>\n<pre>&nbsp; sort(collection.begin(), collection.end());<\/pre>\n<pre>&nbsp; Display(collection, &quot;After sorting&quot;);<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Display label s and contents of integer vector v<\/pre>\n<pre>void Display(vector&lt;int&gt;&amp; v, const char* s)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl &lt;&lt; s &lt;&lt; endl;<\/pre>\n<pre>&nbsp; copy(v.begin(), v.end(),<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; ostream_iterator&lt;int&gt;(cout, &quot;t&quot;));<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl;<\/pre>\n<pre>}<\/pre>\n<p>\u51fd\u6570Display()\u663e\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u4e00\u4e2a\u8f93\u51fa\u6d41\u8fed\u4ee3\u5668\u3002\u4e0b\u9762\u7684\u8bed\u53e5\u5c06\u5bb9\u5668\u4e2d\u7684&#20540;\u4f20\u8f93\u5230cout\u8f93\u51fa\u6d41\u5bf9\u8c61\u4e2d:<\/p>\n<pre>copy(v.begin(), v.end(),<\/pre>\n<pre>&nbsp; ostream_iterator&lt;int&gt;(cout, &quot;t&quot;));<\/pre>\n<p>\u7b2c\u4e09\u4e2a\u53c2\u6570\u5b9e\u4f8b\u5316\u4e86ostream_iterator&lt;int&gt;\u7c7b\u578b\uff0c\u5e76\u5c06\u5b83\u4f5c\u4e3acopy()\u51fd\u6570\u7684\u8f93\u51fa\u76ee\u6807\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002\u201ct\u201d\u5b57\u7b26\u4e32\u662f\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre>$ g&#43;&#43; outstrm.cpp<\/pre>\n<pre>$ .\/a.out<\/pre>\n<pre>Before sorting<\/pre>\n<pre>677&nbsp;&nbsp; 722&nbsp;&nbsp; 686&nbsp;&nbsp; 238&nbsp;&nbsp; 964&nbsp;&nbsp; 397&nbsp;&nbsp; 251&nbsp;&nbsp; 118&nbsp;&nbsp; 11&nbsp;&nbsp;&nbsp; 312<\/pre>\n<pre>After sorting<\/pre>\n<pre>11&nbsp;&nbsp;&nbsp; 118&nbsp;&nbsp; 238&nbsp;&nbsp; 251&nbsp;&nbsp; 312&nbsp;&nbsp; 397&nbsp;&nbsp; 677&nbsp;&nbsp; 686&nbsp;&nbsp; 722&nbsp;&nbsp; 964<\/pre>\n<p>\u8fd9\u662fSTL\u795e\u5947\u7684\u4e00\u9762\u300e\u786e\u5b9e\u795e\u5947\u300f\u3002\u4e3a\u5b9a\u4e49\u8f93\u51fa\u6d41\u8fed\u4ee3\u5668\uff0cSTL\u63d0\u4f9b\u4e86\u6a21\u677f\u7c7b<tt>ostream_iterator<\/tt>\u3002\u8fd9\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2aostream\u5bf9\u8c61\u548c\u4e00\u4e2astring&#20540;\u3002\u56e0\u6b64\u53ef\u4ee5\u8c61\u4e0b\u9762\u4e00\u6837\u7b80\u5355\u5730\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\uff1a<\/p>\n<pre>ostream_iterator&lt;int&gt;(cout, &quot;n&quot;)<\/pre>\n<p>\u8be5\u8fed\u4ee3\u8d77\u53ef\u4ee5\u548c\u4efb\u4f55\u63a5\u53d7\u4e00\u4e2a\u8f93\u51fa\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<h3>\u63d2\u5165\u8fed\u4ee3\u5668<\/h3>\n<p>\u63d2\u5165\u8fed\u4ee3\u5668\u7528\u4e8e\u5c06&#20540;\u63d2\u5165\u5230\u5bb9\u5668\u4e2d\u3002\u5b83\u4eec\u4e5f\u53eb\u505a\u9002\u914d\u5668\uff0c\u56e0\u4e3a\u5b83\u4eec\u5c06\u5bb9\u5668\u9002\u914d\u6216\u8f6c\u5316\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5e76\u7528\u4e8ecopy()\u8fd9\u6837\u7684\u7b97\u6cd5\u4e2d\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u94fe\u8868\u548c\u4e00\u4e2a\u77e2\u91cf\u5bb9\u5668:<\/p>\n<pre>list&lt;double&gt; dList;<\/pre>\n<pre>vector&lt;double&gt; dVector;<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528front_inserter\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\u53ef\u4ee5\u53ea\u7528\u5355\u4e2acopy()\u8bed\u53e5\u5c31\u5b8c\u6210\u5c06\u77e2\u91cf\u4e2d\u7684\u5bf9\u8c61\u63d2\u5165\u5230\u94fe\u8868\u524d\u7aef\u7684\u64cd\u4f5c\uff1a<\/p>\n<pre>copy(dVector.begin(), dVector.end(), front_inserter(dList));<\/pre>\n<p>\u4e09\u79cd\u63d2\u5165\u8fed\u4ee3\u5668\u5982\u4e0b\uff1a<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>\u666e\u901a\u63d2\u5165\u5668<\/em> \u5c06\u5bf9\u8c61\u63d2\u5165\u5230\u5bb9\u5668\u4efb\u4f55\u5bf9\u8c61\u7684\u524d\u9762\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Front inserters<\/em> \u5c06\u5bf9\u8c61\u63d2\u5165\u5230\u6570\u636e\u96c6\u7684\u524d\u9762\u2014\u2014\u4f8b\u5982\uff0c\u94fe\u8868\u8868\u5934\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>Back inserters<\/em> \u5c06\u5bf9\u8c61\u63d2\u5165\u5230\u96c6\u5408\u7684\u5c3e\u90e8\u2014\u2014\u4f8b\u5982\uff0c\u77e2\u91cf\u7684\u5c3e\u90e8\uff0c\u5bfc\u81f4\u77e2\u91cf\u5bb9\u5668\u6269\u5c55\u3002<\/p>\n<p>\u4f7f\u7528\u63d2\u5165\u8fed\u4ee3\u5668\u53ef\u80fd\u5bfc\u81f4\u5bb9\u5668\u4e2d\u7684\u5176\u4ed6\u5bf9\u8c61\u79fb\u52a8\u4f4d\u7f6e\uff0c\u56e0\u800c\u4f7f\u5f97\u73b0\u5b58\u7684\u8fed\u4ee3\u5668\u975e\u6cd5\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u5bf9\u8c61\u63d2\u5165\u5230\u77e2\u91cf\u5bb9\u5668\u5c06\u5bfc\u81f4\u5176\u4ed6&#20540;\u79fb\u52a8\u4f4d\u7f6e\u4ee5\u817e\u51fa\u7a7a\u95f4\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u63d2\u5165\u5230\u8c61\u94fe\u8868\u8fd9\u6837\u7684\u7ed3\u6784\u4e2d\u66f4\u4e3a\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e0d\u4f1a\u5bfc\u81f4\u5176\u4ed6\u5bf9\u8c61\u79fb\u52a8\u3002<\/p>\n<p><strong>Listing 5. insert.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;algorithm&gt;<\/pre>\n<pre>#include &lt;list&gt;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int iArray[5] = { 1, 2, 3, 4, 5 };<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>void Display(list&lt;int&gt;&amp; v, const char* s);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; list&lt;int&gt; iList;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Copy iArray backwards into iList<\/pre>\n<pre>&nbsp; copy(iArray, iArray &#43; 5, front_inserter(iList));<\/pre>\n<pre>&nbsp; Display(iList, &quot;Before find and copy&quot;);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Locate value 3 in iList<\/pre>\n<pre>&nbsp; list&lt;int&gt;::iterator p =<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; find(iList.begin(), iList.end(), 3);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Copy first two iArray values to iList ahead of p<\/pre>\n<pre>&nbsp; copy(iArray, iArray &#43; 2, inserter(iList, p));<\/pre>\n<pre>&nbsp; Display(iList, &quot;After find and copy&quot;);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>void Display(list&lt;int&gt;&amp; a, const char* s)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; cout &lt;&lt; s &lt;&lt; endl;<\/pre>\n<pre>&nbsp; copy(a.begin(), a.end(),<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; ostream_iterator&lt;int&gt;(cout, &quot; &quot;));<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl;<\/pre>\n<pre>}<\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>$ g&#43;&#43; insert.cpp<\/pre>\n<pre>$ .\/a.out<\/pre>\n<pre>Before find and copy<\/pre>\n<pre>5 4 3 2 1<\/pre>\n<pre>After find and copy<\/pre>\n<pre>5 4 1 2 3 2 1<\/pre>\n<p>\u53ef\u4ee5\u5c06front_inserter\u66ff\u6362\u4e3aback_inserter\u8bd5\u8bd5\u3002<\/p>\n<p>\u5982\u679c\u7528find()\u53bb\u67e5\u627e\u5728\u5217\u8868\u4e2d\u4e0d\u5b58\u5728\u7684&#20540;\uff0c\u4f8b\u598299\u3002\u7531\u4e8e\u8fd9\u65f6\u5c06p\u8bbe\u7f6e\u4e3a<em>past-the-end<\/em> &#20540;\u3002\u6700\u540e\u7684copy()\u51fd\u6570\u5c06iArray\u7684&#20540;\u9644\u52a0\u5230\u94fe\u8868\u7684\u540e\u90e8\u3002<\/p>\n<h3>\u6df7\u5408\u8fed\u4ee3\u5668\u51fd\u6570<\/h3>\n<p>\u5728\u6d89\u53ca\u5230\u5bb9\u5668\u548c\u7b97\u6cd5\u7684\u64cd\u4f5c\u4e2d\uff0c\u8fd8\u6709\u4e24\u4e2a\u8fed\u4ee3\u5668\u51fd\u6570\u975e\u5e38\u6709\u7528\uff1a<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>advance()<\/tt> \u6309\u6307\u5b9a\u7684\u6570\u76ee\u589e\u51cf\u8fed\u4ee3\u5668\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>distance()<\/tt> \u8fd4\u56de\u5230\u8fbe\u4e00\u4e2a\u8fed\u4ee3\u5668\u6240\u9700\uff08\u9012\u589e\uff09\u64cd\u4f5c\u7684\u6570\u76ee\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>list&lt;int&gt; iList;<\/pre>\n<pre>list&lt;int&gt;::iterator p =<\/pre>\n<pre>&nbsp; find(iList.begin(), iList.end(), 2);<\/pre>\n<pre>cout &lt;&lt; &quot;before: p == &quot; &lt;&lt; *p &lt;&lt; endl;<\/pre>\n<pre>advance(p, 2);&nbsp; \/\/ same as p = p &#43; 2;<\/pre>\n<pre>cout &lt;&lt; &quot;after : p == &quot; &lt;&lt; *p &lt;&lt; endl;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int k = 0;<\/pre>\n<pre>distance(p, iList.end(), k);<\/pre>\n<pre>cout &lt;&lt; &quot;k == &quot; &lt;&lt; k &lt;&lt; endl;<\/pre>\n<pre>&nbsp;<\/pre>\n<p><tt>advance()\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5411\u524d\u63a8\u8fdb\u7684\u6570\u76ee\u3002\u5bf9\u4e8e\u524d\u63a8\u8fed\u4ee3\u5668\uff0c\u8be5&#20540;\u5fc5\u987b\u4e3a\u6b63\uff0c\u800c\u5bf9\u4e8e\u53cc\u5411\u8fed\u4ee3\u5668\u548c\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\uff0c\u8be5&#20540;\u53ef\u4ee5\u4e3a\u8d1f\u3002<\/tt><\/p>\n<pre>\u4f7f\u7528 <tt><span style=\"font-family:\u65b0\u5b8b\u4f53\">distance()\u51fd\u6570\u6765\u8fd4\u56de\u5230\u8fbe\u53e6\u4e00\u4e2a\u8fed\u4ee3\u5668\u6240\u9700\u8981\u7684\u6b65\u9aa4\u3002<\/span><\/tt><\/pre>\n<p><strong>\u6ce8\u610f<\/strong><\/p>\n<p><tt>distance()\u51fd\u6570\u662f\u8fed\u4ee3\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u9012\u589e\u7b2c\u4e09\u4e2a\u53c2\u6570\u3002\u56e0\u6b64\uff0c\u4f60\u5fc5\u987b\u521d\u59cb\u5316\u8be5\u53c2\u6570\u3002\u672a\u521d\u59cb\u5316\u8be5\u53c2\u6570\u51e0\u4e4e\u6ce8\u5b9a\u8981\u5931\u8d25\u3002<\/tt><\/p>\n<h2>\u51fd\u6570\u548c\u51fd\u6570\u5bf9\u8c61<\/h2>\n<p>STL\u4e2d\uff0c\u51fd\u6570\u88ab\u79f0\u4e3a\u7b97\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4eec\u548c\u6807\u51c6C\u5e93\u51fd\u6570\u76f8\u6bd4\uff0c\u5b83\u4eec\u66f4\u4e3a\u901a\u7528\u3002STL\u7b97\u6cd5\u901a\u8fc7\u91cd\u8f7doperator()\u51fd\u6570\u5b9e\u73b0\u4e3a\u6a21\u677f\u7c7b\u6216\u6a21\u677f\u51fd\u6570\u3002\u8fd9\u4e9b\u7c7b\u7528\u4e8e\u521b\u5efa\u51fd\u6570\u5bf9\u8c61\uff0c\u5bf9\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5404\u79cd\u5404\u6837\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u7684\u51e0\u8282\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u548c\u51fd\u6570\u5bf9\u8c61\u3002<\/p>\n<h3>\u51fd\u6570\u548c\u65ad\u8a00<\/h3>\n<p>\u7ecf\u5e38\u9700\u8981\u5bf9\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u904d\u5386\u4e00\u4e2a\u5bb9\u5668\u4e2d\u6240\u6709\u5bf9\u8c61\u7684STL\u7b97\u6cd5\u80fd\u591f\u56de\u8c03\u81ea\u5df1\u7684\u51fd\u6570\u3002\u4f8b\u5982<\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;stdlib.h&gt;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need random(), srandom()<\/pre>\n<pre>#include &lt;time.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need time()<\/pre>\n<pre>#include &lt;vector&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need vector<\/pre>\n<pre>#include &lt;algorithm&gt;&nbsp;&nbsp;&nbsp; \/\/ Need for_each()<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>#define VSIZE 24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Size of vector<\/pre>\n<pre>vector&lt;long&gt; v(VSIZE);&nbsp; \/\/ Vector object<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Function prototypes<\/pre>\n<pre>void initialize(long &amp;ri);<\/pre>\n<pre>void show(const long &amp;ri);<\/pre>\n<pre>bool isMinus(const long &amp;ri);&nbsp; \/\/ Predicate function<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; srandom( time(NULL) );&nbsp; \/\/ Seed random generator<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; for_each(v.begin(), v.end(), initialize);\/\/\u8c03\u7528\u666e\u901a\u51fd\u6570<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Vector of signed long integers&quot; &lt;&lt; endl;<\/pre>\n<pre>&nbsp; for_each(v.begin(), v.end(), show);<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Use predicate function to count negative values<\/pre>\n<pre>&nbsp; \/\/<\/pre>\n<pre>&nbsp; int count = 0;<\/pre>\n<pre>&nbsp; vector&lt;long&gt;::iterator p;<\/pre>\n<pre>&nbsp; p = find_if(v.begin(), v.end(), isMinus);\/\/\u8c03\u7528\u65ad\u8a00\u51fd\u6570<\/pre>\n<pre>&nbsp; while (p != v.end()) {<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; count&#43;&#43;;<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; p = find_if(p &#43; 1, v.end(), isMinus);<\/pre>\n<pre>&nbsp; }<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Number of values: &quot; &lt;&lt; VSIZE &lt;&lt; endl;<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Negative values : &quot; &lt;&lt; count &lt;&lt; endl;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Set ri to a signed integer value<\/pre>\n<pre>void initialize(long &amp;ri)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; ri = ( random() - (RAND_MAX \/ 2) );<\/pre>\n<pre>&nbsp; \/\/&nbsp; ri = random();<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Display value of ri<\/pre>\n<pre>void show(const long &amp;ri)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; cout &lt;&lt; ri &lt;&lt; &quot;&nbsp; &quot;;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Returns true if ri is less than 0<\/pre>\n<pre>bool isMinus(const long &amp;ri)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; return (ri &lt; 0);<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<p>\u6240\u8c13\u65ad\u8a00\u51fd\u6570\uff0c\u5c31\u662f\u8fd4\u56debool&#20540;\u7684\u51fd\u6570\u3002<\/p>\n<h3>\u51fd\u6570\u5bf9\u8c61<\/h3>\n<p>\u9664\u4e86\u7ed9STL\u7b97\u6cd5\u4f20\u9012\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u4f60\u8fd8\u53ef\u80fd\u9700\u8981\u4f20\u9012\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u4ee5\u4fbf\u6267\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\u3002\u8fd9\u6837\u7684\u4e00\u4e2a\u5bf9\u8c61\u5c31\u53eb\u505a\u51fd\u6570\u5bf9\u8c61\u3002\u5b9e\u9645\u4e0a\u51fd\u6570\u5bf9\u8c61\u5c31\u662f\u4e00\u4e2a\u7c7b\uff0c\u4f46\u5b83\u548c\u56de\u8c03\u51fd\u6570\u4e00\u6837\u53ef\u4ee5\u88ab\u56de\u8c03\u3002\u4f8b\u5982\uff0c\u5728\u51fd\u6570\u5bf9\u8c61\u6bcf\u6b21\u88abfor_each()\u6216find_if()\u51fd\u6570\u8c03\u7528\u65f6\u53ef\u4ee5\u4fdd\u7559\u7edf\u8ba1\u4fe1\u606f\u3002\u51fd\u6570\u5bf9\u8c61\u662f\u901a\u8fc7\u91cd\u8f7doperator()()\u5b9e\u73b0\u7684\u3002\u5982\u679cTanyClass\u5b9a\u4e49\u4e86opeator()(),\u90a3\u4e48\u5c31\u53ef\u4ee5\u8fd9\u4e48\u4f7f\u7528\uff1a<\/p>\n<pre>TAnyClass object;&nbsp; \/\/ Construct object<\/pre>\n<pre>object();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Calls TAnyClass::operator()() function<\/pre>\n<pre>for_each(v.begin(), v.end(), object);<\/pre>\n<p>STL\u5b9a\u4e49\u4e86\u51e0\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u7531\u4e8e\u5b83\u4eec\u662f\u6a21\u677f\uff0c\u6240\u4ee5\u80fd\u591f\u7528\u4e8e\u4efb\u4f55\u7c7b\u578b\uff0c\u5305\u62ecC\/C&#43;&#43;\u56fa\u6709\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982long\u3002\u6709\u4e9b\u51fd\u6570\u5bf9\u8c61\u4ece\u540d\u5b57\u4e2d\u5c31\u53ef\u4ee5\u770b\u51fa\u5b83\u7684\u7528\u9014\uff0c\u5982plus()\u548cmultiplies()\u3002\u7c7b&#20284;\u7684greater()\u548cless-equal()\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a&#20540;\u3002\n<\/p>\n<p><strong>\u6ce8\u610f<\/strong><\/p>\n<p>\u6709\u4e9b\u7248\u672c\u7684ANSI C&#43;&#43;\u5b9a\u4e49\u4e86times()\u51fd\u6570\u5bf9\u8c61\uff0c\u800cGNU C&#43;&#43;\u628a\u5b83\u547d\u540d\u4e3amultiplies()\u3002\u4f7f\u7528\u65f6\u5fc5\u987b\u5305\u542b\u5934\u6587\u4ef6<tt>&lt;functional&gt;\u3002<\/tt><\/p>\n<p>\u4e00\u4e2a\u6709\u7528\u7684\u51fd\u6570\u5bf9\u8c61\u7684\u5e94\u7528\u662f<tt>accumulate()<\/tt> \u7b97\u6cd5\u3002\u8be5\u51fd\u6570\u8ba1\u7b97\u5bb9\u5668\u4e2d\u6240\u6709&#20540;\u7684\u603b\u548c\u3002\u8bb0\u4f4f\u8fd9\u6837\u7684&#20540;\u4e0d\u4e00\u5b9a\u662f\u7b80\u5355\u7684\u7c7b\u578b\uff0c\u901a\u8fc7\u91cd\u8f7doperator&#43;()\uff0c\u4e5f\u53ef\u4ee5\u662f\u7c7b\u5bf9\u8c61\u3002<\/p>\n<p><strong>Listing 8. accum.cpp<\/strong> &nbsp;<\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;numeric&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need accumulate()<\/pre>\n<pre>#include &lt;vector&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need vector<\/pre>\n<pre>#include &lt;functional&gt;&nbsp;&nbsp; \/\/ Need multiplies() (or times())<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>#define MAX 10<\/pre>\n<pre>vector&lt;long&gt; v(MAX);&nbsp;&nbsp;&nbsp; \/\/ Vector object<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; \/\/ Fill vector using conventional loop<\/pre>\n<pre>&nbsp; \/\/<\/pre>\n<pre>&nbsp; for (int i = 0; i &lt; MAX; i&#43;&#43;)<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; v[i] = i &#43; 1;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Accumulate the sum of contained values<\/pre>\n<pre>&nbsp; \/\/<\/pre>\n<pre>&nbsp; long sum =<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; accumulate(v.begin(), v.end(), 0);<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Sum of values == &quot; &lt;&lt; sum &lt;&lt; endl;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; \/\/ Accumulate the product of contained values<\/pre>\n<pre>&nbsp; \/\/<\/pre>\n<pre>&nbsp; long product =<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; accumulate(v.begin(), v.end(), 1, multiplies&lt;long&gt;());\/\/\u6ce8\u610f\u8fd9\u884c<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Product of values == &quot; &lt;&lt; product &lt;&lt; endl;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<p>\u7f16\u8bd1\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre>$ g&#43;&#43; accum.cpp<\/pre>\n<pre>$ .\/a.out<\/pre>\n<pre>Sum of values == 55<\/pre>\n<pre>Product of values == 3628800<\/pre>\n<p><tt>\u300e\u6ce8\u610f\u4f7f\u7528\u4e86\u51fd\u6570\u5bf9\u8c61\u7684accumulate()\u7684\u7528\u6cd5\u3002accumulate() \u5728\u5185\u90e8\u5c06\u6bcf\u4e2a\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u548c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4f5c\u4e3amultiplies\u51fd\u6570\u5bf9\u8c61\u7684\u53c2\u6570,multiplies(1,v)\u8ba1\u7b97\u4e58\u79ef\u3002VC\u4e2d\u7684\u8fd9\u4e9b\u6a21\u677f\u7684\u6e90\u4ee3\u7801\u5982\u4e0b\uff1a<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ TEMPLATE FUNCTION accumulate<\/tt><\/p>\n<p><tt>template&lt;class _II, class _Ty&gt; inline<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; _Ty accumulate(_II _F, _II _L, _Ty _V)<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; {for (; _F != _L; &#43;&#43;_F)<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _V = _V &#43; *_F;<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; return (_V); }<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ TEMPLATE FUNCTION accumulate WITH BINOP<\/tt><\/p>\n<p><tt>template&lt;class _II, class _Ty, class _Bop&gt; inline<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; _Ty accumulate(_II _F, _II _L, _Ty _V, _Bop _B)<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; {for (; _F != _L; &#43;&#43;_F)<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _V = _B(_V, *_F);<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; return (_V); }<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ TEMPLATE STRUCT binary_function<\/tt><\/p>\n<p><tt>template&lt;class _A1, class _A2, class _R&gt;<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; struct binary_function {<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; typedef _A1 first_argument_type;<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; typedef _A2 second_argument_type;<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; typedef _R result_type;<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; };<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ TEMPLATE STRUCT multiplies<\/tt><\/p>\n<p><tt>template&lt;class _Ty&gt;<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; struct multiplies : binary_function&lt;_Ty, _Ty, _Ty&gt; {<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; _Ty operator()(const _Ty&amp; _X, const _Ty&amp; _Y) const<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {return (_X * _Y); }<\/tt><\/p>\n<p><tt>&nbsp;&nbsp;&nbsp; };<\/tt><\/p>\n<p><tt>\u5f15\u8a00\uff1a\u5982\u679c\u4f60\u60f3\u6df1\u5165\u4e86\u89e3STL\u5230\u5e95\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u6700\u597d\u7684\u529e\u6cd5\u662f\u5199\u4e2a\u7b80\u5355\u7684\u7a0b\u5e8f\uff0c\u5c06\u7a0b\u5e8f\u4e2d\u6d89\u53ca\u5230\u7684\u6a21\u677f\u6e90\u7801\u7ed9copy\u4e0b\u6765\uff0c\u7a0d\u4f5c\u6574\u7406\uff0c\u5c31\u80fd\u770b\u61c2\u4e86\u3002\u6240\u4ee5\u6ca1\u6709\u5fc5\u8981\u53bb\u4e70\u4ec0\u4e48\u300aSTL\u6e90\u7801\u5256\u6790\u300b\u4e4b\u7c7b\u7684\u4e66\u7c4d\uff0c\u90a3\u4e9b\u4e66\u53ef\u80fd\u53cd\u800c\u6d6a\u8d39\u65f6\u95f4\u3002\u300f<\/tt><\/p>\n<h3>\u53d1\u751f\u5668\u51fd\u6570\u5bf9\u8c61<\/h3>\n<p>\u6709\u4e00\u7c7b\u6709\u7528\u7684\u51fd\u6570\u5bf9\u8c61\u662f\u201c\u53d1\u751f\u5668\u201d(generator)\u3002\u8fd9\u7c7b\u51fd\u6570\u6709\u81ea\u5df1\u7684\u5185\u5b58\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u80fd\u591f\u4ece\u5148\u524d\u7684\u8c03\u7528\u4e2d\u8bb0\u4f4f\u4e00\u4e2a&#20540;\u3002\u4f8b\u5982\u968f\u673a\u6570\u53d1\u751f\u5668\u51fd\u6570\u3002<\/p>\n<p>\u666e\u901a\u7684C\u7a0b\u5e8f\u5458\u4f7f\u7528\u9759\u6001\u6216\u5168\u5c40\u53d8\u91cf \u201c\u8bb0\u5fc6\u201d\u4e0a\u6b21\u8c03\u7528\u7684\u7ed3\u679c\u3002\u4f46\u8fd9\u6837\u505a\u7684\u7f3a\u70b9\u662f\u8be5\u51fd\u6570\u65e0\u6cd5\u548c\u5b83\u7684\u6570\u636e\u76f8\u5206\u79bb\u300e\u8fd8\u6709\u4e2a\u7f3a\u70b9\u662f\u8981\u7528TLS\u624d\u80fd\u7ebf\u7a0b\u5b89\u5168\u300f\u3002\u663e\u7136\uff0c\u4f7f\u7528\u7c7b\u6765\u5c01\u88c5\u4e00\u5757\uff1a\u201c\u5185\u5b58\u201d\u66f4\u5b89\u5168\u53ef\u9760\u3002\u5148\u770b\u4e00\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<p><strong>Listing 9. randfunc.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;stdlib.h&gt;&nbsp;&nbsp;&nbsp; \/\/ Need random(), srandom()<\/pre>\n<pre>#include &lt;time.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need time()<\/pre>\n<pre>#include &lt;algorithm&gt;&nbsp;&nbsp; \/\/ Need random_shuffle()<\/pre>\n<pre>#include &lt;vector&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need vector<\/pre>\n<pre>#include &lt;functional&gt;&nbsp; \/\/ Need ptr_fun()<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Data to randomize<\/pre>\n<pre>int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};<\/pre>\n<pre>vector&lt;int&gt; v(iarray, iarray &#43; 10);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Function prototypes<\/pre>\n<pre>void Display(vector&lt;int&gt;&amp; vr, const char *s);<\/pre>\n<pre>unsigned int RandInt(const unsigned int n);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; srandom( time(NULL) );&nbsp; \/\/ Seed random generator<\/pre>\n<pre>&nbsp; Display(v, &quot;Before shuffle:&quot;);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; pointer_to_unary_function&lt;unsigned int, unsigned int&gt;<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; ptr_RandInt = ptr_fun(RandInt);&nbsp; \/\/ Pointer to RandInt()\/\/\u6ce8\u610f\u8fd9\u884c<\/pre>\n<pre>&nbsp; random_shuffle(v.begin(), v.end(), ptr_RandInt);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp; Display(v, &quot;After shuffle:&quot;);<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Display contents of vector vr<\/pre>\n<pre>void Display(vector&lt;int&gt;&amp; vr, const char *s)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl &lt;&lt; s &lt;&lt; endl;<\/pre>\n<pre>&nbsp; copy(vr.begin(), vr.end(), ostream_iterator&lt;int&gt;(cout, &quot; &quot;));<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Return next random value in sequence modulo n<\/pre>\n<pre>unsigned int RandInt(const unsigned int n)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; return random() % n;<\/pre>\n<pre>}<\/pre>\n<p>\u7f16\u8bd1\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>$ g&#43;&#43; randfunc.cpp<\/pre>\n<pre>$ .\/a.out<\/pre>\n<pre>Before shuffle:<\/pre>\n<pre>1 2 3 4 5 6 7 8 9 10<\/pre>\n<pre>After shuffle:<\/pre>\n<pre>6 7 2 8 3 5 10 1 9 4<\/pre>\n<p>\u9996\u5148\u7528\u4e0b\u9762\u7684\u8bed\u53e5\u7533\u660e\u4e00\u4e2a\u5bf9\u8c61\uff1a<\/p>\n<pre>pointer_to_unary_function&lt;unsigned int, unsigned int&gt;<\/pre>\n<pre>&nbsp; ptr_RandInt = ptr_fun(RandInt);<\/pre>\n<p>\u8fd9\u513f\u4f7f\u7528STL\u7684\u5355\u76ee\u51fd\u6570\u6a21\u677f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cfptr_RandInt\uff0c\u5e76\u5c06\u5730\u5740\u521d\u59cb\u5316\u5230\u6211\u4eec\u7684\u51fd\u6570RandInt()\u3002\u5355\u76ee\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a&#20540;\u3002\u73b0\u5728random_shuffle()\u53ef\u4ee5\u5982\u4e0b\u8c03\u7528\uff1a<\/p>\n<pre>random_shuffle(v.begin(), v.end(), ptr_RandInt);<\/pre>\n<pre>\u5728\u672c\u4f8b\u5b50\u4e2d\uff0c\u53d1\u751f\u5668\u53ea\u662f\u7b80\u5355\u7684\u8c03\u7528rand()\u51fd\u6570\u3002<\/pre>\n<pre>&nbsp;<\/pre>\n<p><strong>\u5173\u4e8e\u5e38\u91cf\u5f15\u7528\u7684\u4e00\u70b9\u5c0f\u9ebb\u70e6\uff08\u4e0d\u7ffb\u8bd1\u4e86\uff0cVC\u4e0b\u5c06\u4f8b\u5b50\u4e2d\u7684const\u53bb\u6389\uff09<\/strong> <\/p>\n<h3>\u53d1\u751f\u5668\u51fd\u6570\u7c7b\u5bf9\u8c61<\/h3>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u8bf4\u660e\u53d1\u751f\u5668\u51fd\u6570\u7c7b\u5bf9\u8c61\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>Listing 10. fiborand.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;algorithm&gt;&nbsp;&nbsp; \/\/ Need random_shuffle()<\/pre>\n<pre>#include &lt;vector&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ Need vector<\/pre>\n<pre>#include &lt;functional&gt;&nbsp; \/\/ Need unary_function<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Data to randomize<\/pre>\n<pre>int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};<\/pre>\n<pre>vector&lt;int&gt; v(iarray, iarray &#43; 10);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Function prototype<\/pre>\n<pre>void Display(vector&lt;int&gt;&amp; vr, const char *s);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ The FiboRand template function-object class<\/pre>\n<pre>template &lt;class Arg&gt;<\/pre>\n<pre>class FiboRand : public unary_function&lt;Arg, Arg&gt; {<\/pre>\n<pre>&nbsp; int i, j;<\/pre>\n<pre>&nbsp; Arg sequence[18];<\/pre>\n<pre>public:<\/pre>\n<pre>&nbsp; FiboRand();<\/pre>\n<pre>&nbsp; Arg operator()(const Arg&amp; arg);<\/pre>\n<pre>};<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>void main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; FiboRand&lt;int&gt; fibogen;&nbsp; \/\/ Construct generator object<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Fibonacci random number generator&quot; &lt;&lt; endl;<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;using random_shuffle and a function object&quot; &lt;&lt; endl;<\/pre>\n<pre>&nbsp; Display(v, &quot;Before shuffle:&quot;);<\/pre>\n<pre>&nbsp; random_shuffle(v.begin(), v.end(), fibogen);<\/pre>\n<pre>&nbsp; Display(v, &quot;After shuffle:&quot;);<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Display contents of vector vr<\/pre>\n<pre>void Display(vector&lt;int&gt;&amp; vr, const char *s)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl &lt;&lt; s &lt;&lt; endl;<\/pre>\n<pre>&nbsp; copy(vr.begin(), vr.end(),<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; ostream_iterator&lt;int&gt;(cout, &quot; &quot;));<\/pre>\n<pre>&nbsp; cout &lt;&lt; endl;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ FiboRand class constructor<\/pre>\n<pre>template&lt;class Arg&gt;<\/pre>\n<pre>FiboRand&lt;Arg&gt;::FiboRand()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; sequence[17] = 1;<\/pre>\n<pre>&nbsp; sequence[16] = 2;<\/pre>\n<pre>&nbsp; for (int n = 15; n &gt; 0; n\u2014)<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; sequence[n] = sequence[n &#43; 1] &#43; sequence[n &#43; 2];<\/pre>\n<pre>&nbsp; i = 17;<\/pre>\n<pre>&nbsp; j = 5;<\/pre>\n<pre>}<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ FiboRand class function operator<\/pre>\n<pre>template&lt;class Arg&gt;<\/pre>\n<pre>Arg FiboRand&lt;Arg&gt;::operator()(const Arg&amp; arg)<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; Arg k = sequence[i] &#43; sequence[j];<\/pre>\n<pre>&nbsp; sequence[i] = k;<\/pre>\n<pre>&nbsp; i--;<\/pre>\n<pre>&nbsp; j--;<\/pre>\n<pre>&nbsp; if (i == 0) i = 17;<\/pre>\n<pre>&nbsp; if (j == 0) j = 17;<\/pre>\n<pre>&nbsp; return k % arg;<\/pre>\n<pre>}<\/pre>\n<p>\u7f16\u8bd1\u8fd0\u884c\u8f93\u51fa\u5982\u4e0b:<\/p>\n<pre>$ g&#43;&#43; fiborand.cpp<\/pre>\n<pre>$ .\/a.out<\/pre>\n<pre>Fibonacci random number generator<\/pre>\n<pre>using random_shuffle and a function object<\/pre>\n<pre>Before shuffle:<\/pre>\n<pre>1 2 3 4 5 6 7 8 9 10<\/pre>\n<pre>After shuffle:<\/pre>\n<pre>6 8 5 4 3 7 10 1 9<\/pre>\n<p>\u8be5\u7a0b\u5e8f\u7528\u5b8c\u5168\u4e0d\u901a\u7684\u65b9\u6cd5\u4f7f\u7528\u4f7f\u7528rand_shuffle\u3002Fibonacci \u53d1\u751f\u5668\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u8be5\u7c7b\u80fd\u4ece\u5148\u524d\u7684\u201c\u4f7f\u7528\u201d\u4e2d\u8bb0\u5fc6\u8fd0\u884c\u7ed3\u679c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u7c7b<tt>FiboRand<\/tt> \u7ef4\u62a4\u4e86\u4e00\u4e2a\u6570\u7ec4\u548c\u4e24\u4e2a\u7d22\u5f15\u53d8\u91cfI\u548cj\u3002<\/p>\n<p><tt>FiboRand\u7c7b\u7ee7\u627f\u81eaunary_function()<\/tt> \u6a21\u677f:<\/p>\n<pre>template &lt;class Arg&gt;<\/pre>\n<pre>class FiboRand : public unary_function&lt;Arg, Arg&gt; {...<\/pre>\n<p><tt>Arg\u662f\u7528\u6237\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002\u8be5\u7c7b\u8fd8\u5b9a\u4ee5\u4e86\u4e24\u4e2a\u6210\u5458\u51fd\u6570\uff0c\u4e00\u4e2a\u662f\u6784\u9020\u51fd\u6570\uff0c\u53e6\u4e00\u4e2a\u662foperator()\uff08\uff09\u51fd\u6570\uff0c\u8be5\u64cd\u4f5c\u7b26\u5141\u8bb8random_shuffle()\u7b97\u6cd5\u8c61\u4e00\u4e2a\u51fd\u6570\u4e00\u6837\u201c\u8c03\u7528\u201d\u4e00\u4e2aFiboRand\u5bf9\u8c61\u3002<\/tt>\n<\/p>\n<h3>\u7ed1\u5b9a\u5668\u51fd\u6570\u5bf9\u8c61<\/h3>\n<p>\u4e00\u4e2a\u7ed1\u5b9a\u5668\u4f7f\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61f()\u548c\u53c2\u6570&#20540;V\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u88ab\u7ed1\u5b9a\u51fd\u6570\u5bf9\u8c61\u5fc5\u987b\u4e3a\u53cc\u76ee\u51fd\u6570\uff0c\u4e5f\u5c31\u662f\u8bf4\u6709\u4e24\u4e2a\u53c2\u6570,A\u548cB\u3002STL \u4e2d\u7684\u5e2e\u5b9a\u5668\u6709\uff1a<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>bind1st()<\/tt> \u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u5bf9\u8c61\u5c06&#20540;V\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570A\u3002<\/p>\n<p>\u00b7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <tt>bind2nd()<\/tt>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u8be5\u51fd\u6570\u5bf9\u8c61\u5c06&#20540;V\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570B\u3002<\/p>\n<p>\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p><strong>Listing 11. binder.cpp<\/strong> <\/p>\n<pre>#include &lt;iostream.h&gt;<\/pre>\n<pre>#include &lt;algorithm&gt;<\/pre>\n<pre>#include &lt;functional&gt;<\/pre>\n<pre>#include &lt;list&gt;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>using namespace std;<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>\/\/ Data<\/pre>\n<pre>int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};<\/pre>\n<pre>list&lt;int&gt; aList(iarray, iarray &#43; 10);<\/pre>\n<pre>&nbsp;<\/pre>\n<pre>int main()<\/pre>\n<pre>{<\/pre>\n<pre>&nbsp; int k = 0;<\/pre>\n<pre>&nbsp; count_if(aList.begin(), aList.end(),<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; bind1st(greater&lt;int&gt;(), 8), k);<\/pre>\n<pre>&nbsp; cout &lt;&lt; &quot;Number elements &lt; 8 == &quot; &lt;&lt; k &lt;&lt; endl;<\/pre>\n<pre>&nbsp; return 0;<\/pre>\n<pre>}<\/pre>\n<p>Algorithm <tt>count_if()\u8ba1\u7b97\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u7684\u6570\u76ee\u3002<\/tt> \u8fd9\u662f\u901a\u8fc7\u5c06\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u548c\u4e00\u4e2a\u53c2\u6570\u6346\u7ed1\u5230\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u5c06\u8be5\u5bf9\u8c61\u4f5c\u4e3a\u7b97\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u5b9e\u73b0\u7684\u3002 \u6ce8\u610f\u8fd9\u4e2a\u8868\u8fbe\u5f0f:<\/p>\n<pre>bind1st(greater&lt;int&gt;(), 8)<\/pre>\n<p>\u8be5\u8868\u8fbe\u5f0f\u5c06greater&lt;int&gt;()\u548c\u4e00\u4e2a\u53c2\u6570&#20540;8\u6346\u7ed1\u4e3a\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u3002\u7531\u4e8e\u4f7f\u7528\u4e86bind1st()\uff0c\u6240\u4ee5\u8be5\u51fd\u6570\u76f8\u5f53\u4e8e\u8ba1\u7b97\u4e0b\u8ff0\u8868\u8fbe\u5f0f\uff1a <\/p>\n<pre>8 &gt; q<\/pre>\n<p>\u8868\u8fbe\u5f0f\u4e2d\u7684q\u662f\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u5b8c\u6574\u7684\u8868\u8fbe\u5f0f<\/p>\n<pre>count_if(aList.begin(), aList.end(),<\/pre>\n<pre>&nbsp; bind1st(greater&lt;int&gt;(), 8), k);<\/pre>\n<p>\u8ba1\u7b97\u6240\u6709\u5c0f\u4e8e\u6216\u7b49\u4e8e8\u7684\u5bf9\u8c61\u7684\u6570\u76ee\u3002 <\/p>\n<h3>\u5426\u5b9a\u51fd\u6570\u5bf9\u8c61<\/h3>\n<p>\u6240\u8c13\u5426\u5b9a(negator)\u51fd\u6570\u5bf9\u8c61\uff0c\u5c31\u662f\u5b83\u4ece\u53e6\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u521b\u5efa\u800c\u6765\uff0c\u5982\u679c\u539f\u5148\u7684\u51fd\u6570\u8fd4\u56de\u771f\uff0c\u5219\u5426\u5b9a\u51fd\u6570\u5bf9\u8c61\u8fd4\u56de\u5047\u3002\u6709\u4e24\u4e2a\u5426\u5b9a\u51fd\u6570\u5bf9\u8c61\uff1anot1()\u548cnot2()\u3002<tt>not1()\u63a5\u53d7\u5355\u76ee\u51fd\u6570\u5bf9\u8c61\uff0cnot2()\u63a5\u53d7\u53cc\u76ee\u51fd\u6570\u5bf9\u8c61\u3002\u5426\u5b9a\u51fd\u6570\u5bf9\u8c61\u901a\u5e38\u548c\u5e2e\u5b9a\u5668\u4e00\u8d77\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4e0a\u8282\u4e2d\u7528bind1nd\u6765\u641c\u7d22q&lt;=8\u7684&#20540;\uff1a<\/tt><\/p>\n<pre>&nbsp; count_if(aList.begin(), aList.end(),<\/pre>\n<pre>&nbsp;&nbsp;&nbsp; bind1st(greater&lt;int&gt;(), 8), k);<\/pre>\n<p>\u5982\u679c\u8981\u641c\u7d22q&gt;8\u7684\u5bf9\u8c61\uff0c\u5219\u7528bind2st\u3002\u800c\u73b0\u5728\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre>start = find_if(aList.begin(), aList.end(),\n&nbsp; not1(bind1nd(greater&lt;int&gt;(), 6)));<\/pre>\n<p>\u4f60\u5fc5\u987b\u4f7f\u7528not1\uff0c\u56e0\u4e3abind1nd\u8fd4\u56de\u5355\u76ee\u51fd\u6570\u3002 <\/p>\n<h2>\u603b\u7ed3\uff1a\u4f7f\u7528\u6807\u51c6\u6a21\u677f\u5e93 (STL)<\/h2>\n<p>\u5c3d\u7ba1\u5f88\u591a\u7a0b\u5e8f\u5458\u4ecd\u7136\u5728\u4f7f\u7528\u6807\u51c6C\u51fd\u6570\uff0c\u4f46\u662f\u8fd9\u5c31\u597d\u50cf\u9a91\u7740\u6bdb\u9a74\u5bfb\u627eMercedes\u4e00\u6837\u3002\u4f60\u5f53\u7136\u6700\u7ec8\u4e5f\u4f1a\u5230\u8fbe\u76ee\u6807\uff0c\u4f46\u662f\u4f60\u6d6a\u8d39\u4e86\u5f88\u591a\u65f6\u95f4\u3002<\/p>\n<p>\u5c3d\u7ba1\u6709\u65f6\u5019\u4f7f\u7528\u6807\u51c6C\u51fd\u6570\u786e\u5b9e\u65b9\u4fbf(\u5982\u4f7f\u7528sprintf()\u8fdb\u884c&#26684;\u5f0f\u5316\u8f93\u51fa)\u3002\u4f46\u662fC\u51fd\u6570\u4e0d\u4f7f\u7528\u5f02\u5e38\u673a\u5236\u6765\u62a5\u544a\u9519\u8bef\uff0c\u4e5f\u4e0d\u9002\u5408\u5904\u7406\u65b0\u7684\u6570\u636e\u7c7b\u578b\u3002\u800c\u4e14\u6807\u51c6C\u51fd\u6570\u7ecf\u5e38\u4f7f\u7528\u5185\u5b58\u5206\u914d\u6280\u672f\uff0c\u6ca1\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u5f88\u5bb9\u6613\u5199\u51fabug\u6765\u3002.<\/p>\n<p>C&#43;&#43;\u6807\u51c6\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5b89\u5168\uff0c\u66f4\u4e3a\u7075\u6d3b\u7684\u6570\u636e\u96c6\u5904\u7406\u65b9\u5f0f\u3002STL\u6700\u521d\u7531HP\u5b9e\u9a8c\u5ba4\u7684Alexander Stepanov\u548cMeng Lee\u5f00\u53d1\u3002\u6700\u8fd1\uff0cC&#43;&#43;\u6807\u51c6\u59d4\u5458\u4f1a\u91c7\u7eb3\u4e86STL\uff0c\u5c3d\u7ba1\u5728\u4e0d\u540c\u7684\u5b9e\u73b0\u4e4b\u95f4\u4ecd\u6709\u7ec6\u8282\u5dee\u522b\u3002<\/p>\n<p>STL\u7684\u6700\u4e3b\u8981\u7684\u4e24\u4e2a\u7279\u70b9\uff1a\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u7684\u5206\u79bb\uff0c\u975e\u9762\u5411\u5bf9\u8c61\u672c\u8d28\u3002\u8bbf\u95ee\u5bf9\u8c61\u662f\u901a\u8fc7\u8c61\u6307\u9488\u4e00\u6837\u7684\u8fed\u4ee3\u5668\u5b9e\u73b0\u7684\uff1b\u5bb9\u5668\u662f\u8c61\u94fe\u8868\uff0c\u77e2\u91cf\u4e4b\u7c7b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e76\u6309\u6a21\u677f\u65b9\u5f0f\u63d0\u4f9b\uff1b\u7b97\u6cd5\u662f\u51fd\u6570\u6a21\u677f\uff0c\u7528\u4e8e\u64cd\u4f5c\u5bb9\u5668\u4e2d\u7684\u6570\u636e\u3002\u7531\u4e8eSTL\u4ee5\u6a21\u677f\u4e3a\u57fa\u7840\uff0c\u6240\u4ee5\u80fd\u7528\u4e8e\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u548c\u7ed3\u6784\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u5341\u5206\u949f\u638c\u63e1STL \u8fd9\u662f\u672c\u5c0f\u4eba\u4e66\u3002\u539f\u540d\u662f\u300ausing stl\u300b\uff0c\u4e0d\u77e5\u9053\u662f\u8c01\u5199\u7684\u3002\u4e0d\u8fc7\u6211\u5012\u89c9\u5f97\u5f88\u6709\u8da3\uff0c\u6240\u4ee5\u5316\u4e86\u4e24 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[3],"tags":[29],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-defaultcategory","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>using STL - nike0good<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nike0good.com\/?p=203\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"using STL - nike0good\" \/>\n<meta property=\"og:description\" content=\"\u4e09\u5341\u5206\u949f\u638c\u63e1STL \u8fd9\u662f\u672c\u5c0f\u4eba\u4e66\u3002\u539f\u540d\u662f\u300ausing stl\u300b\uff0c\u4e0d\u77e5\u9053\u662f\u8c01\u5199\u7684\u3002\u4e0d\u8fc7\u6211\u5012\u89c9\u5f97\u5f88\u6709\u8da3\uff0c\u6240\u4ee5\u5316\u4e86\u4e24 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nike0good.com\/?p=203\" \/>\n<meta property=\"og:site_name\" content=\"nike0good\" \/>\n<meta property=\"article:published_time\" content=\"2012-10-23T06:16:00+00:00\" \/>\n<meta name=\"author\" content=\"nike0good\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"nike0good\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nike0good.com\\\/?p=203#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nike0good.com\\\/?p=203\"},\"author\":{\"name\":\"nike0good\",\"@id\":\"https:\\\/\\\/nike0good.com\\\/#\\\/schema\\\/person\\\/4defa38da89de87e400861e73396baad\"},\"headline\":\"using STL\",\"datePublished\":\"2012-10-23T06:16:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nike0good.com\\\/?p=203\"},\"wordCount\":742,\"commentCount\":0,\"keywords\":[\"C++\"],\"articleSection\":[\"DefaultCategory\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nike0good.com\\\/?p=203#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nike0good.com\\\/?p=203\",\"url\":\"https:\\\/\\\/nike0good.com\\\/?p=203\",\"name\":\"using STL - nike0good\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nike0good.com\\\/#website\"},\"datePublished\":\"2012-10-23T06:16:00+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nike0good.com\\\/#\\\/schema\\\/person\\\/4defa38da89de87e400861e73396baad\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nike0good.com\\\/?p=203#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nike0good.com\\\/?p=203\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nike0good.com\\\/?p=203#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nike0good.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"using STL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nike0good.com\\\/#website\",\"url\":\"https:\\\/\\\/nike0good.com\\\/\",\"name\":\"nike0good\",\"description\":\"\u6709\u6240\u4f5c\u4e3a\u662f\u4eba\u751f\u7684\u6700\u9ad8\u5883\u754c\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nike0good.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nike0good.com\\\/#\\\/schema\\\/person\\\/4defa38da89de87e400861e73396baad\",\"name\":\"nike0good\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8e5fa08d5c367a1a6fb5ff13bb10ed5a331f981513256951290ae42322da6854?s=96&d=identicon&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8e5fa08d5c367a1a6fb5ff13bb10ed5a331f981513256951290ae42322da6854?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8e5fa08d5c367a1a6fb5ff13bb10ed5a331f981513256951290ae42322da6854?s=96&d=identicon&r=g\",\"caption\":\"nike0good\"},\"sameAs\":[\"https:\\\/\\\/nike0good.com\"],\"url\":\"https:\\\/\\\/nike0good.com\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"using STL - nike0good","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nike0good.com\/?p=203","og_locale":"zh_CN","og_type":"article","og_title":"using STL - nike0good","og_description":"\u4e09\u5341\u5206\u949f\u638c\u63e1STL \u8fd9\u662f\u672c\u5c0f\u4eba\u4e66\u3002\u539f\u540d\u662f\u300ausing stl\u300b\uff0c\u4e0d\u77e5\u9053\u662f\u8c01\u5199\u7684\u3002\u4e0d\u8fc7\u6211\u5012\u89c9\u5f97\u5f88\u6709\u8da3\uff0c\u6240\u4ee5\u5316\u4e86\u4e24 [&hellip;]","og_url":"https:\/\/nike0good.com\/?p=203","og_site_name":"nike0good","article_published_time":"2012-10-23T06:16:00+00:00","author":"nike0good","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"nike0good","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nike0good.com\/?p=203#article","isPartOf":{"@id":"https:\/\/nike0good.com\/?p=203"},"author":{"name":"nike0good","@id":"https:\/\/nike0good.com\/#\/schema\/person\/4defa38da89de87e400861e73396baad"},"headline":"using STL","datePublished":"2012-10-23T06:16:00+00:00","mainEntityOfPage":{"@id":"https:\/\/nike0good.com\/?p=203"},"wordCount":742,"commentCount":0,"keywords":["C++"],"articleSection":["DefaultCategory"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nike0good.com\/?p=203#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nike0good.com\/?p=203","url":"https:\/\/nike0good.com\/?p=203","name":"using STL - nike0good","isPartOf":{"@id":"https:\/\/nike0good.com\/#website"},"datePublished":"2012-10-23T06:16:00+00:00","author":{"@id":"https:\/\/nike0good.com\/#\/schema\/person\/4defa38da89de87e400861e73396baad"},"breadcrumb":{"@id":"https:\/\/nike0good.com\/?p=203#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nike0good.com\/?p=203"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nike0good.com\/?p=203#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nike0good.com\/"},{"@type":"ListItem","position":2,"name":"using STL"}]},{"@type":"WebSite","@id":"https:\/\/nike0good.com\/#website","url":"https:\/\/nike0good.com\/","name":"nike0good","description":"\u6709\u6240\u4f5c\u4e3a\u662f\u4eba\u751f\u7684\u6700\u9ad8\u5883\u754c","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nike0good.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/nike0good.com\/#\/schema\/person\/4defa38da89de87e400861e73396baad","name":"nike0good","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/secure.gravatar.com\/avatar\/8e5fa08d5c367a1a6fb5ff13bb10ed5a331f981513256951290ae42322da6854?s=96&d=identicon&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8e5fa08d5c367a1a6fb5ff13bb10ed5a331f981513256951290ae42322da6854?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8e5fa08d5c367a1a6fb5ff13bb10ed5a331f981513256951290ae42322da6854?s=96&d=identicon&r=g","caption":"nike0good"},"sameAs":["https:\/\/nike0good.com"],"url":"https:\/\/nike0good.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/nike0good.com\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nike0good.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nike0good.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nike0good.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nike0good.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":0,"href":"https:\/\/nike0good.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/nike0good.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nike0good.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nike0good.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}