{"id":181,"date":"2021-11-21T12:57:44","date_gmt":"2021-11-21T12:57:44","guid":{"rendered":"http:\/\/47.103.123.166\/?p=181"},"modified":"2021-11-21T12:57:44","modified_gmt":"2021-11-21T12:57:44","slug":"%e9%9d%a2%e8%af%951-%e5%89%91%e6%8c%87offer%e7%ac%94%e8%ae%b0%ef%bc%88c%ef%bc%89","status":"publish","type":"post","link":"http:\/\/47.103.123.166\/?p=181","title":{"rendered":"\u9762\u8bd51\u2014\u2014\u5251\u6307Offer\u7b14\u8bb0\uff08C++\uff09"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/zhedahht\/CodingInterviewChinese2\">\u6e90\u7801<\/a><\/p>\n<h1>1 \u57fa\u7840\u77e5\u8bc6<\/h1>\n<h2>1.1 \u7f16\u7a0b\u8bed\u8a00<\/h2>\n<h3>\u56db\u4e2a\u7c7b\u578b\u8f6c\u6362\u5173\u952e\u5b57<\/h3>\n<p><a href=\"https:\/\/blog.csdn.net\/JarrettLife\/article\/details\/51366949\">\u53c2\u8003<\/a><br \/>\n1) static_cast<br \/>\n\u5e95\u5c42\u65e0const\u7684\u4e00\u822c\u9759\u6001\u8f6c\u6362\uff0c\u57fa\u672c\u7c7b\u578b \/ \u5177\u6709\u7ee7\u627f\u5173\u7cfb\u7684\u7c7b\u578b<\/p>\n<pre><code class=\"language-cpp\">\/\/\u57fa\u672c\u7c7b\u578b\u8f6c\u6362\nint i = 0;\ndouble d = static_cast&lt;double&gt;(i);  \/\/\u76f8\u5f53\u4e8edouble d = double(i);\n\/\/\u8f6c\u6362\u7ee7\u627f\u7c7b\u7684\u5bf9\u8c61\u4e3a\u57fa\u7c7b\u5bf9\u8c61\nclass Base{};\nclass Derived: public Base{};\nDerived d;\nBase b = static_cast&lt;Base&gt;(d);  \/\/\u76f8\u5f53\u4e8eBase(d)<\/code><\/pre>\n<p>2) dynamic_cast<br \/>\n\u52a8\u6001\u8f6c\u6362\uff0c\u6d3e\u751f\u7c7b\u6307\u9488(\u5f15\u7528)\u8f6c\u6362\u4e3a\u57fa\u7c7b\u6307\u9488(\u5f15\u7528) \/ \u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\u4f46\u88ab\u8f6c\u6362\u7c7b\u6709\u865a\u51fd\u6570<\/p>\n<pre><code class=\"language-cpp\">class Base{};\nclass Derived: public Base{};\n\/\/\u6d3e\u751f\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u57fa\u7c7b\u6307\u9488\nDerived *pd = new Derived;\nBase *pb = dynamic_cast&lt;Base&gt;(pd);\n\/\/\u68c0\u6d4b\nif(!pb){\n  std::cout &lt;&lt; \"Failed Cast!\";\n}\n\/\/\u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\uff0c\u4f46\u88ab\u8f6c\u6362\u7c7b\u6709\u865a\u51fd\u6570\nclass A{\n    virtual ~A();\n};\nclass B{};\nA *pa = new A;\nB *pb = dynamic_cast&lt;B&gt;(pa);<\/code><\/pre>\n<p>3) const_cast<br \/>\n\u53ea\u6539\u53d8const(\u5e38\u91cf)\u5c5e\u6027\uff0c\u4e0d\u6539\u53d8\u7c7b\u578b<\/p>\n<pre><code class=\"language-cpp\">const int *pci = 0;\n\/\/\u5c06\u5e38\u91cf\u8f6c\u6362\u4e3a\u975e\u5e38\u91cf\nint *pk = const_cast&lt;int*&gt;(pci);  \/\/\u76f8\u5f53\u4e8eint *pk = int*(pci);\nconst A* pca = new A;\nA *pa = const_cast&lt;A*&gt;(pca); \/\/\u76f8\u5f53\u4e8eA *pa = A*(pca);<\/code><\/pre>\n<p>4) reinterpret<br \/>\n\u91cd\u65b0\u89e3\u91ca\u3001\u6307\u9488\u7c7b\u578b\u8f6c\u6362<\/p>\n<pre><code class=\"language-cpp\">\/\/\u57fa\u672c\u7c7b\u578b\u6307\u9488\u7684\u7c7b\u578b\u8f6c\u6362\ndouble d = 9.2;\ndouble *pd = &amp;d;\nint *pi = reinterpret_cast&lt;int*&gt;(pd); \/\/\u76f8\u5f53\u4e8eint *pi = (int*)pd;\n\/\/\u4e0d\u76f8\u5173\u7684\u7c7b\u7684\u6307\u9488\u7c7b\u578b\u8f6c\u6362\nclass A{};\nclass B{};\nA *pa = new A;\nB *pb = reinterpret_cast&lt;B*&gt;(pa); \/\/\u76f8\u5f53\u4e8eB *pb = (B*)pa;\n\/\/\u6307\u9488\u8f6c\u6362\u4e3a\u6574\u6570\nint *pi = 0;\nlong L = reinterpret_cast&lt;long&gt;(pi);  \/\/\u76f8\u5f53\u4e8elong L = (long)pi;<\/code><\/pre>\n<h3>sizeof<\/h3>\n<p>1) \u7a7a\u7c7b\u578b\u4f7f\u7528sizeof\uff0c\u8ba1\u7b97\u8be5\u7c7b\u578b\u5b9e\u4f8b\u7684\u5927\u5c0f\uff0c\u4f7f\u7528\u5b9e\u4f8b\uff0c\u6709\u5927\u5c0f\uff0c\u5927\u5c0f\u7531\u7f16\u8bd1\u5668\u51b3\u5b9a\uff0c\u4e00\u822c\u662f1<br \/>\n2) \u6709\u6784\u9020\u548c\u6790\u6784\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u4e00\u822c\u4e5f\u662f1\uff0c\u5b9e\u4f8b\u8c03\u7528\u8fd9\u4fe9\u51fd\u6570\u53ea\u9700\u77e5\u9053\u51fd\u6570\u5730\u5740\uff0c\u51fd\u6570\u5730\u5740\u4e0e\u7c7b\u578b\u6709\u5173\uff0c\u4e0e\u5b9e\u4f8b\u65e0\u5173<br \/>\n3) \u6709\u865a\u51fd\u6570\u7684\u7c7b\u578b\uff0c\u5927\u5c0f\u4e3a\u4e00\u4e2a\u6307\u9488\u7684\u5927\u5c0f\uff0832\/64-&gt;4\/8\uff09\uff0c\u7f16\u8bd1\u5668\u4f1a\u5728\u5b9e\u4f8b\u4e2d\u6dfb\u52a0\u6307\u5411\u865a\u51fd\u6570\u8868\u7684\u6307\u9488<\/p>\n<h3>\u590d\u5236\u6784\u9020\u51fd\u6570<\/h3>\n<p>1) C++\u6807\u51c6\u4e0d\u5141\u8bb8\u590d\u5236\u6784\u9020\u51fd\u6570\u8fdb\u884c\u503c\u4f20\u9012\uff08\u9012\u5f52\u6ea2\u51fa\uff09\uff0c\u5141\u8bb8\u5e38\u91cf\u5f15\u7528<\/p>\n<h3>\u9762\u8bd5\u98981 \u8d4b\u503c\u8fd0\u7b97\u7b26\u51fd\u6570<\/h3>\n<p>1) \u8fd4\u56de\u5f15\u7528\uff08\u8fde\u7eed\u8d4b\u503c\uff09\uff0c\u4f20\u5165\u5e38\u91cf\u5f15\u7528\uff08\u9ad8\u6548\u3001\u4fdd\u62a4\uff09\uff0c\u91ca\u653e\u5df2\u6709\u5185\u5b58\uff08\u6cc4\u9732\uff09\uff0c\u5224\u65ad\u540c\u4e00\u4e2a\u5b9e\u4f8b<br \/>\n2) \u5f02\u5e38\u5b89\u5168<br \/>\n\u5148\u91ca\u653e\u5185\u5b58\u8fd8\u662f\u5148\u7533\u8bf7\u5185\u5b58\uff1a\u5185\u5b58\u4e0d\u8db3\u7684\u60c5\u51b5\u4e0b\uff0c\u8981\u4fdd\u8bc1\u539f\u6570\u636e\u4e0d\u53d8\uff08\u4e0d\u88ab\u63d0\u524d\u91ca\u653e\uff09\uff0c\u521b\u5efa\u4e34\u65f6\u5b9e\u4f8b\uff0c\u6784\u9020\u51fd\u6570new\uff0cnew\u51fa\u6765\u5c31\u4ea4\u6362\uff0cnew\u4e0d\u51fa\u6765\u5c31\u629b\u51fa\u5f02\u5e38bad_alloc<\/p>\n<h2>1.2 \u6570\u636e\u7ed3\u6784<\/h2>\n<h3>\u6570\u7ec4\u7684\u6548\u7387<\/h3>\n<p>1) \u9759\u6001\u6570\u7ec4\uff1a\u9ad8\u65f6\u95f4\u6548\u7387\uff0c\u4f4e\u7a7a\u95f4\u6548\u7387<br \/>\n2) \u52a8\u6001\u6570\u7ec4\uff1a\u6269\u5bb9\u64cd\u4f5c\u589e\u52a0\u65f6\u95f4\u590d\u6742\u5ea6\uff08STL,vector\uff09<br \/>\n3) \u6570\u7ec4\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u4f20\u9012\u65f6\uff0c\u6570\u7ec4\u540d\u5c06\u9000\u5316\u6210\u540c\u7c7b\u578b\u7684\u6307\u9488<\/p>\n<h3>\u9762\u8bd5\u98983 \u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u6570\u5b57<\/h3>\n<p>1) \u98981\u627e\u51fa\u91cd\u590d\u6570\u5b57\uff0c\u601d\u8def\uff1a\u628a\u6570\u5b57\u653e\u5230\u5bf9\u5e94\u7684\u4e0b\u6807\u7684\u4f4d\u7f6e\u4e0a\uff0c\u5f53\u53d1\u73b0\u5bf9\u5e94\u4f4d\u7f6e\u5df2\u6709\u8be5\u6570\u5b57\uff0c\u5219\u627e\u5230\u91cd\u590d\u6570\u5b57\uff0c\u5bf9\u5e94\u7a7a\u95f4O(1)\uff0c\u65f6\u95f4O(n)<br \/>\n2) \u98982\u4e0d\u4fee\u6539\u6570\u7ec4\u627e\u51fa\u91cd\u590d\u6570\u5b57\uff0c\u601d\u8def\uff1a\u4e8c\u5206\u6cd5\uff0c\u79cd\u7c7b\u4e8c\u5206\uff0c\u786e\u5b9a\u6bcf\u4e00\u6bb5\u5185\u7684\u79cd\u7c7b\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u662f\u5426\u4e3a\u8be5\u6bb5\u7684\u79cd\u7c7b\u6570\u3002<br \/>\n3) \u660e\u786e\u8981\u6c42\u65f6\u95f4\u8fd8\u662f\u8981\u6c42\u7a7a\u95f4\u6548\u7387<\/p>\n<h3>\u9762\u8bd5\u98984 \u4e8c\u7ef4\u6570\u7ec4\u67e5\u627e<\/h3>\n<p>1) \u89c4\u5f8b\uff0c\u7f29\u5c0f\u67e5\u627e\u8303\u56f4<\/p>\n<h3>\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u5730\u5740<\/h3>\n<p>1) \u5e38\u91cf\u5b57\u7b26\u4e32\u521d\u59cb\u5316\u5b57\u7b26\u6570\u7ec4\uff0c\u4e0d\u540c\u6570\u7ec4\u7684\u5730\u5740\u4e0d\u540c\uff0c\u539f\u56e0\u662f\u4e0d\u540c\u5b57\u7b26\u6570\u7ec4\u5df2\u5206\u914d\u4e86\u4e0d\u540c\u4f4d\u7f6e\u7684\u5185\u5b58\u4ee5<strong>\u62f7\u8d1d<\/strong>\u5e38\u91cf\u5b57\u7b26\u4e32 P49<br \/>\n2) \u5e38\u91cf\u5b57\u7b26\u4e32\u521d\u59cb\u5316\u5b57\u7b26\u6307\u9488\uff0c\u4e0d\u540c\u6307\u9488\u6307\u5411\u7684\u5730\u5740\u76f8\u540c\uff0c\u539f\u56e0\u662f\u65e0\u9700\u91cd\u65b0\u5206\u914d\u5185\u5b58\uff0c\u76f4\u63a5\u6307\u5411\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u7684\u5730\u5740<\/p>\n<h3>\u9762\u8bd5\u98985 \u66ff\u6362\u7a7a\u683c<\/h3>\n<p>1) \u4e00\u6b21\u79fb\u52a8\u5230\u4f4d\uff0cO(n)\uff0c\uff08\u6b64\u9898\u4e0d\u7528\u81ea\u5df1\u6269\u5bb9\uff09<br \/>\n2) \u8003\u8651\u5185\u5b58\u8986\u76d6<br \/>\n3) \u4ece\u5c3e\u90e8\u5230\u5934\u90e8\uff0c\u4f9d\u6b21\u586b\u5165\u6392\u5e8f\u503c<\/p>\n<h3>\u94fe\u8868<\/h3>\n<p>1) \u94fe\u8868\u662f\u8003\u5bdf\u91cd\u70b9<br \/>\n2) <a href=\"https:\/\/blog.csdn.net\/shen_jz2012\/article\/details\/50631317\">\u4e3a\u4ec0\u4e48\u662f\u4f20\u5165\u6307\u5411\u6307\u9488\u7684\u6307\u9488<\/a><br \/>\n\u8981\u4fee\u6539\u7684\u662f\u6307\u9488\u7684\u5185\u5bb9\uff08<em>pHead\uff0c\u4e5f\u5c31\u662f\u4fee\u6539\u4e00\u4e2a\u5730\u5740\uff09\uff0c\u800c\u4e0d\u662f\u4fee\u6539\u6307\u9488\u6307\u5411\u7684\u5185\u5bb9\uff0c\u56e0\u6b64\u4f20\u5165\u7684\u662f\u6307\u5411\u6307\u9488\u7684\u6307\u9488\u3002<br \/>\n\u7c7b\u6bd4\u4e8e\uff0c\u4f20\u5165\u6574\u578b int a \u4e0e\u4f20\u5165\u5176\u6307\u9488 int <\/em>a\u7684\u5173\u7cfb\uff0c\u8fd9\u91cc\u662f\u4f20\u5165\u6307\u9488 ListNode *pHead \u548c\u6307\u5411\u6307\u9488\u7684\u6307\u9488 ListNode **pHead\u3002<\/p>\n<h3>\u9762\u8bd5\u98986 \u4ece\u5c3e\u5230\u5934\u6253\u5370\u94fe\u8868<\/h3>\n<p>1) \u6808<br \/>\n2) \u9012\u5f52  \u94fe\u8868\u8fc7\u957f\uff0c\u53ef\u80fd\u5bfc\u81f4\u51fd\u6570\u8c03\u7528\u6808\u6ea2\u51fa<\/p>\n<pre><code class=\"language-cpp\">void PrintListReversingly_Recursively(ListNode* pHead)\n{\n    if(pHead != nullptr)\n    {\n        if (pHead-&gt;m_pNext != nullptr)\n        {\n            PrintListReversingly_Recursively(pHead-&gt;m_pNext);\n        }\n\n        printf(\"%d\\t\", pHead-&gt;m_nValue);\n    }\n}<\/code><\/pre>\n<h3>\u6811<\/h3>\n<p>1) \u4e8c\u53c9\u6811\u7279\u4f8b<br \/>\n\u4e8c\u53c9\u641c\u7d22\u6811<br \/>\n\u5806<br \/>\n\u7ea2\u9ed1\u6811<\/p>\n<h3>\u9762\u8bd5\u98987 \u91cd\u5efa\u4e8c\u53c9\u6811<\/h3>\n<p>1) \u5df2\u77e5\u524d\u5e8f\u548c\u4e2d\u5e8f\u3001\u6216\u8005\u5df2\u77e5\u540e\u5e8f\u548c\u4e2d\u5e8f\uff0c\u90fd\u53ef\u4ee5\u9012\u5f52\u7684\u5f62\u5f0f\uff0c\u901a\u8fc7\u4e0d\u65ad\u5730 <strong>\u5212\u5206\u5de6\u53f3\u5b50\u6811\u7136\u540e\u5206\u522b\u91cd\u5efa<\/strong>\uff0c<a href=\"https:\/\/blog.csdn.net\/fangfang12138\/article\/details\/114749224\">\u8fd9\u91cc\u65b9\u6cd5\u66f4\u7b80\u6d01<\/a>\u3002<\/p>\n<h3>\u9762\u8bd5\u98988 \u4e8c\u53c9\u6811\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9<\/h3>\n<p>1) \u4e2d\u5e8f\u904d\u5386\uff1a\u901a\u8fc7\u753b\u56fe\uff0c\u5206\u522b\u8003\u8651<br \/>\n2) case 1\uff1a\u5b58\u5728\u53f3\u5b50\u6811\uff1a\u4e0b\u4e00\u4e2a\u662f\u5176\u53f3\u5b50\u6811\u7684\u6700\u5de6\u5b50\u7ed3\u70b9\uff08E -&gt; K\uff09<br \/>\n3) case 2\uff1a\u4e0d\u5b58\u5728\u53f3\u5b50\u6811\uff1a<br \/>\n4) case 2.1\uff1a\u8be5\u7ed3\u70b9\u662f\u5176\u7236\u8282\u70b9\u7684\u5de6\u5b50\u7ed3\u70b9\uff0c\u5219\u4e0b\u4e00\u4e2a\u662f\u5176\u7236\u8282\u70b9\uff08H -&gt; D\uff09<br \/>\n5) case 2.2\uff1a\u8be5\u7ed3\u70b9\u662f\u5176\u7236\u8282\u70b9\u7684\u53f3\u5b50\u7ed3\u70b9\uff0c\u5219\u4e0b\u4e00\u4e2a\u662f\u5411\u4e0a\u6eaf\u6e90\uff0c\u76f4\u5230\u6eaf\u6e90\u7ed3\u70b9\u662f\u5176\u7236\u8282\u70b9\u7684\u5de6\u5b50\u7ed3\u70b9\uff0c\u5219\u4e0b\u4e00\u4e2a\u662f\u6eaf\u6e90\u7ed3\u70b9\u7684\u7236\u8282\u70b9\uff08K -&gt; A\uff09<br \/>\n<img src=\"http:\/\/47.103.123.166\/wp-content\/uploads\/2021\/11\/post-181-619a424904044.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>\u6808\u548c\u961f\u5217<\/h3>\n<h3>\u9762\u8bd5\u98989 \u7528\u4e24\u4e2a\u6808\u5b9e\u73b0\u961f\u5217<\/h3>\n<p>1) \u7528\u4e24\u4e2a\u961f\u5217\u5b9e\u73b0\u6808<\/p>\n<pre><code class=\"language-cpp\">template &lt;class T&gt;\nclass CStack\n{\n    public: \n        CStack(void);\n        ~CStack(void);\n        void appendHead(const T&amp; node);\n        T deleteHead();\n\n    private:\n        queue&lt;T&gt; queue1;\n        queue&lt;T&gt; queue2;\n};\n\n\/\/\u5165\u6808\u548c\u51fa\u6808\u65f6\uff0c\u81f3\u5c11\u6709\u4e00\u4e2a\u961f\u5217\u4e3a\u7a7a\ntemplate &lt;class T&gt;\nvoid CStack&lt;T&gt;::appendHead(const T&amp; node)\n{\n    if(0 == queue1.size())      \/\/\u5411\u975e\u7a7a\u7684\u961f\u5217push\n        queue2.push(node);\n    else if(0 == queue2.size())\n        queue1.push(node);  \n}\n\ntemplate &lt;class T&gt;\nT CStack&lt;T&gt;::deleteHead()\n{\n    if(0 == queue1.size() &amp;&amp; 0 == queue2.size())\n        throw new exception(\"stack is empty\");\n\n    if(0 == queue1.size())      \/\/\u4ece\u975e\u7a7a\u961f\u5217\u8f6c\u79fbn-1\u4e2a\u6570\u636e\u5230\u7a7a\u961f\u5217\n    {\n        while(1 != queue2.size())\n        {\n            T&amp; data = queue2.top();\n            queue2.pop();\n            queue1.push(data);\n        }\n        T head = queue2.top();\n        queue2.pop();\n        return head;\n    }\n    else\n    {\n        while(1 != queue1.size())\n        {\n            T&amp; data = queue1.top();\n            queue1.pop();\n            queue2.push(data);\n        }\n        T head = queue1.top();\n        queue1.pop();\n        return head;\n    }\n}<\/code><\/pre>\n<h2>1.3 \u7b97\u6cd5\u4e0e\u6570\u636e\u64cd\u4f5c<\/h2>\n<h3>\u9012\u5f52\u548c\u5faa\u73af<\/h3>\n<p>1) \u9012\u5f52\u4f18\u5148<br \/>\n2) \u9012\u5f52\uff0c\u8c03\u7528\u6808\u6ea2\u51fa\uff0c\u5b50\u95ee\u9898\u91cd\u590d\u8ba1\u7b97<br \/>\n3) \u81ea\u4e0a\u800c\u4e0b\u7684\u9012\u5f52\uff0c\u53ef\u80fd\u51fa\u73b0\u5b50\u95ee\u9898\u91cd\u590d\u8ba1\u7b97\u7684\u95ee\u9898\uff0c\u8f6c\u800c\u91c7\u7528\u81ea\u4e0b\u800c\u4e0a\u7684\u5faa\u73af<\/p>\n<h3>\u9762\u8bd5\u989810 \u6590\u6ce2\u90a3\u5951\u6570\u5217<\/h3>\n<p>1) \u5c0f\u9752\u86d9\uff0c2^(n-1)<br \/>\n2) \u8986\u76d6\u77e9\u5f62\uff1af(1)=2\uff0cf(0)=0 ?<\/p>\n<h3>\u67e5\u627e\u548c\u6392\u5e8f<\/h3>\n<p>1) \u67e5\u627e\u91cd\u70b9\uff1a\u987a\u5e8f\u67e5\u627e\u3001\u4e8c\u5206\u67e5\u627e\u3001\u54c8\u5e0c\u8868\u67e5\u627e\u3001\u4e8c\u53c9\u6392\u5e8f\u6811\uff08\u4e8c\u53c9\u641c\u7d22\u6811\uff09<br \/>\n2) \u6392\u5e8f\u5404\u7b97\u6cd5\u6bd4\u8f83<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6392\u5e8f<\/th>\n<th>\u63d2\u5165\u6392\u5e8f<\/th>\n<th>\u5192\u6ce1\u6392\u5e8f<\/th>\n<th>\u5f52\u5e76\u6392\u5e8f<\/th>\n<th>\u5feb\u901f\u6392\u5e8f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u57fa\u672c\u601d\u60f3<\/td>\n<td>\u81ea\u7b2c\u4e8c\u4e2a\u8bb0\u5f55\u5f00\u59cb\uff0c\u6709\u5e8f\u524d\u63d2<\/td>\n<td>\u76f8\u90bb\u5143\u7d20\u6bd4\u8f83\uff0c\u9006\u5e8f\u4ea4\u6362\uff0c\u9010\u904d\u628a\u6700\u5927\u503c\u79fb\u81f3\u6700\u540e<\/td>\n<td>\u76f8\u90bb\u4e24\u4e2a\/\u591a\u4e2a\u987a\u5e8f\u8868\u5408\u5e76\uff0c\u521d\u59cb\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u987a\u5e8f\u8868<\/td>\n<td>\u4e24\u4fa7\u4f9d\u6b21\u626b\u63cf\uff0c\u5c06\u8f74\u503c\u79fb\u81f3\u6b63\u786e\u4f4d\u7f6e<\/td>\n<\/tr>\n<tr>\n<td>\u65f6\u95f4\u590d\u6742\u5ea6<\/td>\n<td>n~n^2<\/td>\n<td>n~n^2<\/td>\n<td>nlbn<\/td>\n<td>nlbn~n^2<\/td>\n<\/tr>\n<tr>\n<td>\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6<\/td>\n<td>n^2<\/td>\n<td>n^2<\/td>\n<td>nlbn<\/td>\n<td>nlbn<\/td>\n<\/tr>\n<tr>\n<td>\u7a7a\u95f4\u590d\u6742\u5ea6<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>n<\/td>\n<td>lbn~n<\/td>\n<\/tr>\n<tr>\n<td>\u5e94\u7528\/\u4f18\u5316<\/td>\n<td>\u9002\u7528\u4e8e\u5143\u7d20\u6570\u91cf\u5c11\uff0c\u4e14\u57fa\u672c\u6709\u5e8f\uff0c\u6539\u8fdb\uff1a\u5206\u5272\uff0c\u5206\u522b\u76f4\u63d2\uff0c\u57fa\u672c\u6709\u5e8f\u540e\u5168\u4f53\u76f4\u63d2<\/td>\n<td>\u53cc\u5411\u8d77\u6ce1<\/td>\n<td>\u76f8\u90bb-\u4e00\u8d9f-\u9012\u5f52<\/td>\n<td>\u9012\u5f52\u6b21\u6570-\u7a7a\u95f4\u590d\u6742\u5ea6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>3) \u4e8c\u5206\u67e5\u627e<\/p>\n<pre><code class=\"language-cpp\">bool BinarySearch(const int r[], const int length, const int searchData)\n{\n    int start = 0;\n    int end = length-1;\n    int mid = (start+end)\/2;\n\n    while(start&lt;=end)\n    {\n        if(r[mid]==searchData)\n            return true;\n        else if(r[mid]&gt;searchData)\n            end = mid - 1;\n        else\n            start = mid + 1;\n\n        mid = (start+end)\/2;\n    }\n\n    return false;\n}<\/code><\/pre>\n<p>4) \u5feb\u901f\u6392\u5e8f<\/p>\n<pre><code class=\"language-cpp\">\/\/\u8fdb\u884c\u4e00\u6b21\u6392\u5e8f\nint Partition(int r[], int length, int start, int end)\n{\n    if((r==nullptr) || (length&lt;0) || (start&lt;0) || (end&gt;=length))\n        throw new std::exception(\"invalid parameter.\");\n\n    int pivot = r[start];   \/\/\u53d6\u7b2c\u4e00\u4e2a\u503c\u4e3a\u8f74\u503c\uff0c\u6316\u4e00\u4e2a\u5751\n    int i = start;          \/\/\u5de6\u4fa7\u626b\u63cf\u6307\u9488\n    int j = end;            \/\/\u53f3\u4fa7\u626b\u63cf\u6307\u9488\n\n    while(i&lt;j)\n    {\n        \/\/\u53f3\u4fa7\u626b\u63cf\uff1a\u5c06\u53f3\u4fa7\u4e00\u4e2a\u5927\u503c\u79fb\u5230\u5de6\u4fa7\u5751\u4f4d\uff0c\u7a7a\u51fa\u4e00\u4e2a\u53f3\u4fa7\u5751\u4f4d\n        while(i&lt;j &amp;&amp; pivot&lt;r[j])\n            j--;\n\n        if(i&lt;j)\n        {\n            r[i] = r[j];\n            i++;\n        }\n\n        \/\/\u5de6\u4fa7\u626b\u63cf\uff1a\u5c06\u5de6\u4fa7\u4e00\u4e2a\u5c0f\u503c\u79fb\u5230\u53f3\u4fa7\u5751\u4f4d\uff0c\u7a7a\u51fa\u4e00\u4e2a\u5de6\u4fa7\u5751\u4f4d\n        while(i&lt;j &amp;&amp; pivot&gt;r[i])\n            i++;\n\n        if(i&lt;j)\n        {\n            r[j] = r[i];\n            j--;\n        }\n    }\n\n    r[i] = pivot;   \/\/\u628a\u5751\u4f4d\u8865\u9f50\n    return i;\n}\n\nvoid QuickSort(int r[], int length, int start, int end)\n{\n    if((r==nullptr) || (length&lt;0) || (start&lt;0) || (end&gt;=length))\n        throw new std::exception(\"invalid parameter.\");\n\n    if(start &lt; end)\n    {\n        int index = Partition(r, length, start, end);\n        if(start &lt; index) \n            QuickSort(r, length, start, index-1);\n        if(index &lt; end)\n            QuickSort(r, length, index+1, end);\n    }\n}<\/code><\/pre>\n<h3>\u9762\u8bd5\u989811 \u65cb\u8f6c\u6570\u7ec4\u7684\u6700\u5c0f\u6570\u5b57<\/h3>\n<p>1) \u4e8c\u5206\uff0c\u65cb\u8f6c0\u4e2a\uff0c\u987a\u5e8f\u67e5\u627e\uff0c\u7279\u4f8b<\/p>\n<h3>\u56de\u6eaf\u6cd5<\/h3>\n<h3>\u9762\u8bd5\u989812 \u77e9\u9635\u4e2d\u7684\u8def\u5f84<\/h3>\n<pre><code class=\"language-cpp\">#include &lt;cstring&gt;\nvoid* memset( void* dest, int ch, std::size_t count );\n\/*\nParameters\ndest    -   pointer to the object to fill\nch  -   fill byte\ncount   -   number of bytes to fill\n*\/<\/code><\/pre>\n<p>1) \u8d77\u59cb\u4f4d\u7f6e\uff0cfor\/for<br \/>\n2) \u7ed3\u675f\u6761\u4ef6\uff0c\u2018\\0\u2019\u5b57\u7b26\u8f6c\u7ed3\u5c3e<br \/>\n3) \u7ea6\u675f\u6761\u4ef6\uff0c\u4e0d\u80fd\u91cd\u590d\u8d70visited<br \/>\n4) \u9012\u5f52\uff0c\u5468\u56f4\u56db\u4e2a\u683c\u5b50<br \/>\n5) \u56de\u6eaf\uff0cpathLength&#8211;<\/p>\n<h3>\u9762\u8bd5\u989813 \u673a\u5668\u4eba\u7684\u8fd0\u52a8\u8303\u56f4<\/h3>\n<h3>\u52a8\u6001\u89c4\u5212\u4e0e\u8d2a\u5a6a\u7b97\u6cd5<\/h3>\n<p>1) \u52a8\u6001\u89c4\u5212\uff1a<br \/>\n\u6700\u4f18\u89e3\u95ee\u9898<br \/>\n\u53ef\u5206\u89e3\u6210\u5b50\u95ee\u9898\uff0c\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u7ec4\u5408\u8d77\u6765\u5f97\u5230\u6574\u4e2a\u95ee\u9898\u7684\u6700\u4f18\u89e3<br \/>\n\u53ef\u80fd\u5b58\u5728\u91cd\u590d\u7684\u5b50\u95ee\u9898<br \/>\n\u81ea\u4e0a\u800c\u4e0b\u5206\u6790\uff0c\u81ea\u4e0b\u800c\u4e0a\u6c42\u89e3<br \/>\n2) \u8d2a\u5a6a\u7b97\u6cd5\uff1a\u6570\u5b66\u8bc1\u660e<\/p>\n<h3>\u9762\u8bd5\u989814 \u526a\u7ef3\u5b50<\/h3>\n<h3>\u4f4d\u8fd0\u7b97<\/h3>\n<p>1) <a href=\"https:\/\/blog.csdn.net\/weixin_42315600\/article\/details\/80567944\">\u79fb\u4f4d\u64cd\u4f5chttps:\/\/blog.csdn.net\/weixin_42315600\/article\/details\/80567944<\/a><br \/>\n2) \u65e0\u7b26\u53f7\u6570\uff0c\u5de6\u79fb\u53f3\u6dfb0\uff0c\u53f3\u79fb\u5de6\u6dfb0<br \/>\n\u6709\u7b26\u53f7\u6570\uff0c\u5de6\u79fb\u53f3\u6dfb0\uff0c\u53f3\u79fb\u5de6\u8865\u7b26\u53f7\u4f4d<br \/>\n3) \u79fb\u4f4d\u6548\u7387\u9ad8\u4e8e\u4e58\u9664<\/p>\n<h3>\u9762\u8bd5\u989815 \u4e8c\u8fdb\u5236\u4e2d1\u7684\u4e2a\u6570<\/h3>\n<p>1) \u6574\u6570\u51cf1\uff0c\u518d\u4e0e\u539f\u6570\u4e0e\u8fd0\u7b97<\/p>\n<h1>2 \u4ee3\u7801\u8d28\u91cf<\/h1>\n<h2>2.1 \u5b8c\u6574\u6027<\/h2>\n<h3>\u4ee3\u7801\u5b8c\u6574\u6027<\/h3>\n<p>1) \u6d4b\u8bd5\u7528\u4f8b<br \/>\n\u529f\u80fd\u6d4b\u8bd5\/\u8fb9\u754c\u6d4b\u8bd5\/\u8d1f\u9762\u6d4b\u8bd5<br \/>\n2) \u9519\u8bef\u5904\u7406\uff1a\u8fd4\u56de\u503c\u3001\u5168\u5c40\u53d8\u91cf\u3001<a href=\"http:\/\/c.biancheng.net\/view\/422.html\">C++\u5f02\u5e38\u5904\u7406<\/a> <\/p>\n<pre><code class=\"language-cpp\">\/*\u4f7f\u7528\u5f02\u5e38*\/\n#include &lt;iostream&gt;\nusing namespace std;\nint main()\n{\n    double m, n;\n    cin &gt;&gt; m &gt;&gt; n;\n    try \n    {\n        cout &lt;&lt; \"before dividing.\" &lt;&lt; endl;\n        if (n == 0)\n            throw - 1;  \/\/\u629b\u51fa\u6574\u578b\u5f02\u5e38\n        else if (m == 0)\n            throw - 1.0;  \/\/\u62cb\u51fa double \u578b\u5f02\u5e38\n        else\n            cout &lt;&lt; m \/ n &lt;&lt; endl;\n        cout &lt;&lt; \"after dividing.\" &lt;&lt; endl;\n    }\n    catch (double d) \n    {\n        cout &lt;&lt; \"catch (double)\" &lt;&lt; d &lt;&lt; endl;\n    }\n    catch (...) \/\/\u6355\u83b7\u4efb\u4f55\u5f02\u5e38\n    {\n        cout &lt;&lt; \"catch (...)\" &lt;&lt; endl;\n    }\n    cout &lt;&lt; \"finished\" &lt;&lt; endl;\n    return 0;\n}\n\n\/*\u5f02\u5e38\u518d\u629b\u51fa\uff1a\n1\u3001\u4e0d\u5904\u7406\n\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u62cb\u51fa\u7684\u5f02\u5e38\u5728\u672c\u51fd\u6570\u5185\u5c31\u88ab catch \u5757\u6355\u83b7\u5e76\u5904\u7406\uff0c\u90a3\u4e48\u8be5\u5f02\u5e38\u5c31\u4e0d\u4f1a\u62cb\u7ed9\u8fd9\u4e2a\u51fd\u6570\u7684\u8c03\u7528\u8005\uff08\u4e5f\u79f0\u4e3a\u201c\u4e0a\u4e00\u5c42\u7684\u51fd\u6570\u201d\uff09\uff1b\n\u5982\u679c\u5f02\u5e38\u5728\u672c\u51fd\u6570\u4e2d\u6ca1\u6709\u88ab\u5904\u7406\uff0c\u5219\u5b83\u5c31\u4f1a\u88ab\u62cb\u7ed9\u4e0a\u4e00\u5c42\u7684\u51fd\u6570\u3002\n2\u3001\u5904\u7406\uff0c\u4f46\u7ee7\u7eed\u629b\u51fa\uff0c\u5982\u4e0b*\/\ncatch (string s ) \n{\n     cout &lt;&lt; \"CountTax error : \" &lt;&lt; s &lt;&lt; endl;\n     throw; \/\/\u7ee7\u7eed\u629b\u51fa\u6355\u83b7\u7684\u5f02\u5e38\uff0c\u4e0a\u7ea7\u8c03\u7528\u51fd\u6570\u518d\u6b21\u53bb\u6355\u83b7\n}\n\n\/*C++\u6807\u51c6\u5f02\u5e38\u7c7b\nexception \u7c7b\u7684\u6d3e\u751f\u7c7b:bad_typeid\u3001bad_cast\u3001bad_alloc\u3001ios_base::failure\u3001out_of_range...\nC++ \u7a0b\u5e8f\u5728\u78b0\u5230\u67d0\u4e9b\u5f02\u5e38\u65f6\uff0c\u5373\u4f7f\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u5199 throw \u8bed\u53e5\uff0c\u4e5f\u4f1a\u81ea\u52a8\u62cb\u51fa\u4e0a\u8ff0\u5f02\u5e38\u7c7b\u7684\u5bf9\u8c61\u3002\n\u8fd9\u4e9b\u5f02\u5e38\u7c7b\u8fd8\u90fd\u6709\u540d\u4e3a what \u7684\u6210\u5458\u51fd\u6570\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u5f02\u5e38\u63cf\u8ff0\u4fe1\u606f\u3002*\/\n#include &lt;exception&gt; \/\/exception\u7c7b\uff0c\u5f02\u5e38\u7c7b\u7684\u57fa\u7c7b\n#include &lt;stdexcept&gt; \/\/logic_error\u548cruntime_error\u4e24\u4e2a\u516c\u6d3e\u81eaexception\u7684\u7c7b\n#include &lt;new&gt;  \/\/bad_alloc\u516c\u6d3e\u81eaexception\u7684\u7c7b<\/code><\/pre>\n<h3>\u9762\u8bd5\u989816 \u6570\u503c\u7684\u6574\u6570\u6b21\u65b9<\/h3>\n<p>1) \u8003\u8651\uff1a\u5e95\u6570\u4e3a0\uff0c\u6307\u6570\u4e3a\u8d1f<br \/>\n2) \u9012\u5f52\u4e58\u65b9<br \/>\n3) \u4f4d\u8fd0\u7b97\u6548\u7387<\/p>\n<h3>\u9762\u8bd5\u989817 \u6253\u5370\u4ece1\u5230\u6700\u5927\u7684n\u4f4d\u6570<\/h3>\n<p>1) \u5927\u6570\u95ee\u9898<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6e90\u7801 1 \u57fa\u7840\u77e5\u8bc6 1.1 \u7f16\u7a0b\u8bed\u8a00 \u56db\u4e2a\u7c7b\u578b\u8f6c\u6362\u5173\u952e\u5b57 \u53c2\u8003 1) static_cast \u5e95\u5c42\u65e0const\u7684\u4e00\u822c\u9759\u6001\u8f6c\u6362\uff0c\u57fa\u672c\u7c7b\u578b \/ \u5177\u6709\u7ee7\u627f\u5173\u7cfb\u7684\u7c7b\u578b \/\/\u57fa\u672c\u7c7b\u578b\u8f6c\u6362 int i = 0; double d = static_cast&lt;double&gt;(i); \/\/\u76f8\u5f53\u4e8edouble d = double(i); \/\/\u8f6c\u6362\u7ee7\u627f\u7c7b\u7684\u5bf9\u8c61\u4e3a\u57fa\u7c7b\u5bf9\u8c61 class Base{}; class Derived: public Base{}; Derived d; Base b = static_cast&lt;Base&gt;(d); \/\/\u76f8\u5f53\u4e8eBase(d) 2) dynamic_cast \u52a8\u6001\u8f6c\u6362\uff0c\u6d3e\u751f\u7c7b\u6307\u9488(\u5f15\u7528)\u8f6c\u6362\u4e3a\u57fa\u7c7b\u6307\u9488(\u5f15\u7528) \/ \u6ca1\u6709\u7ee7\u627f\u5173\u7cfb\u4f46\u88ab\u8f6c\u6362\u7c7b\u6709\u865a\u51fd\u6570 class Base{}; class Derived: public Base{}; \/\/\u6d3e\u751f\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u57fa\u7c7b\u6307\u9488 Derived *pd = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts\/181"}],"collection":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=181"}],"version-history":[{"count":1,"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions"}],"predecessor-version":[{"id":182,"href":"http:\/\/47.103.123.166\/index.php?rest_route=\/wp\/v2\/posts\/181\/revisions\/182"}],"wp:attachment":[{"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/47.103.123.166\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}