{"id":881,"date":"2024-10-14T11:49:49","date_gmt":"2024-10-14T03:49:49","guid":{"rendered":"http:\/\/114.55.108.251\/?p=881"},"modified":"2024-10-14T20:26:59","modified_gmt":"2024-10-14T12:26:59","slug":"c%e7%ae%97%e6%b3%95%e6%a8%a1%e6%9d%bf","status":"publish","type":"post","link":"https:\/\/guapicoding.com\/?p=881","title":{"rendered":"C++\u7b97\u6cd5\u6a21\u677f"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">C++<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Array\/Vector<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e8c\u5206<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">int search(vector&lt;int&gt;&amp; nums, int target) {<br> &nbsp; &nbsp;int left = 0;<br> &nbsp; &nbsp;int right = nums.size() - 1; \/\/ \u5b9a\u4e49target\u5728\u5de6\u95ed\u53f3\u95ed\u7684\u533a\u95f4\u91cc\uff0c[left, right]<br> &nbsp; &nbsp;while (left &lt;= right) { \/\/ \u5f53left==right\uff0c\u533a\u95f4[left, right]\u4f9d\u7136\u6709\u6548\uff0c\u6240\u4ee5\u7528 &lt;=<br> &nbsp; &nbsp; &nbsp; &nbsp;int middle = left + ((right - left) \/ 2);\/\/ \u9632\u6b62\u6ea2\u51fa \u7b49\u540c\u4e8e(left + right)\/2<br> &nbsp; &nbsp; &nbsp; &nbsp;if (nums[middle] &gt; target) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;right = middle - 1; \/\/ target \u5728\u5de6\u533a\u95f4\uff0c\u6240\u4ee5[left, middle - 1]<br> &nbsp; &nbsp; &nbsp;  } else if (nums[middle] &lt; target) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;left = middle + 1; \/\/ target \u5728\u53f3\u533a\u95f4\uff0c\u6240\u4ee5[middle + 1, right]<br> &nbsp; &nbsp; &nbsp;  } else { \/\/ nums[middle] == target<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return middle; \/\/ \u6570\u7ec4\u4e2d\u627e\u5230\u76ee\u6807\u503c\uff0c\u76f4\u63a5\u8fd4\u56de\u4e0b\u6807<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/ \u672a\u627e\u5230\u76ee\u6807\u503c<br> &nbsp; &nbsp;return -1;<br>}<br>\u200b<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u6ed1\u52a8\u7a97\u53e3<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">int minSubArrayLen(int s, vector&lt;int&gt;&amp; nums) {<br> &nbsp; &nbsp;int result = INT32_MAX;<br> &nbsp; &nbsp;int sum = 0; \/\/ \u6ed1\u52a8\u7a97\u53e3\u6570\u503c\u4e4b\u548c<br> &nbsp; &nbsp;int i = 0; \/\/ \u6ed1\u52a8\u7a97\u53e3\u8d77\u59cb\u4f4d\u7f6e<br> &nbsp; &nbsp;int subLength = 0; \/\/ \u6ed1\u52a8\u7a97\u53e3\u7684\u957f\u5ea6<br> &nbsp; &nbsp;for (int j = 0; j &lt; nums.size(); j++) {<br> &nbsp; &nbsp; &nbsp; &nbsp;sum += nums[j];<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6ce8\u610f\u8fd9\u91cc\u4f7f\u7528while\uff0c\u6bcf\u6b21\u66f4\u65b0 i\uff08\u8d77\u59cb\u4f4d\u7f6e\uff09\uff0c\u5e76\u4e0d\u65ad\u6bd4\u8f83\u5b50\u5e8f\u5217\u662f\u5426\u7b26\u5408\u6761\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp;while (sum &gt;= s) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;subLength = (j - i + 1); \/\/ \u53d6\u5b50\u5e8f\u5217\u7684\u957f\u5ea6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result = result &lt; subLength ? result : subLength;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum -= nums[i++]; \/\/ \u8fd9\u91cc\u4f53\u73b0\u51fa\u6ed1\u52a8\u7a97\u53e3\u7684\u7cbe\u9ad3\u4e4b\u5904\uff0c\u4e0d\u65ad\u53d8\u66f4i\uff08\u5b50\u5e8f\u5217\u7684\u8d77\u59cb\u4f4d\u7f6e\uff09<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/ \u5982\u679cresult\u6ca1\u6709\u88ab\u8d4b\u503c\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de0\uff0c\u8bf4\u660e\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5b50\u5e8f\u5217<br> &nbsp; &nbsp;return result == INT32_MAX ? 0 : result;<br>}<\/pre>\n\n\n\n<p>\u6700\u5c0f\u6ed1\u7a97\u6a21\u677f\uff1a\u7ed9\u5b9a\u6570\u7ec4 nums\uff0c\u5b9a\u4e49\u6ed1\u7a97\u7684\u5de6\u53f3\u8fb9\u754c i, j\uff0c\u6c42\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u6ed1\u7a97\u7684\u6700\u5c0f\u957f\u5ea6\uff08\u4e0a\u65b9\u4ee3\u7801\u4fbf\u662f\u6700\u5c0f\u6ed1\u7a97\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while j &lt; len(nums):<br> &nbsp; &nbsp;\u5224\u65ad[i, j]\u662f\u5426\u6ee1\u8db3\u6761\u4ef6<br> &nbsp; &nbsp;while \u6ee1\u8db3\u6761\u4ef6\uff1a<br> &nbsp; &nbsp; &nbsp; &nbsp;\u4e0d\u65ad\u66f4\u65b0\u7ed3\u679c(\u6ce8\u610f\u5728while\u5185\u66f4\u65b0\uff01)<br> &nbsp; &nbsp; &nbsp; &nbsp;i += 1 \uff08\u6700\u5927\u7a0b\u5ea6\u7684\u538b\u7f29i\uff0c\u4f7f\u5f97\u6ed1\u7a97\u5c3d\u53ef\u80fd\u7684\u5c0f\uff09<br> &nbsp; &nbsp;j += 1<\/pre>\n\n\n\n<p>\u6700\u5927\u6ed1\u7a97\u6a21\u677f\uff1a\u7ed9\u5b9a\u6570\u7ec4 nums\uff0c\u5b9a\u4e49\u6ed1\u7a97\u7684\u5de6\u53f3\u8fb9\u754c i, j\uff0c\u6c42\u6ee1\u8db3\u67d0\u4e2a\u6761\u4ef6\u7684\u6ed1\u7a97\u7684\u6700\u5927\u957f\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while j &lt; len(nums):<br> &nbsp; &nbsp;\u5224\u65ad[i, j]\u662f\u5426\u6ee1\u8db3\u6761\u4ef6<br> &nbsp; &nbsp;while \u4e0d\u6ee1\u8db3\u6761\u4ef6\uff1a<br> &nbsp; &nbsp; &nbsp; &nbsp;i += 1 \uff08\u6700\u4fdd\u5b88\u7684\u538b\u7f29i\uff0c\u4e00\u65e6\u6ee1\u8db3\u6761\u4ef6\u4e86\u5c31\u9000\u51fa\u538b\u7f29i\u7684\u8fc7\u7a0b\uff0c\u4f7f\u5f97\u6ed1\u7a97\u5c3d\u53ef\u80fd\u7684\u5927\uff09<br> &nbsp; &nbsp;\u4e0d\u65ad\u66f4\u65b0\u7ed3\u679c\uff08\u6ce8\u610f\u5728while\u5916\u66f4\u65b0\uff01\uff09<br> &nbsp; &nbsp;j += 1<\/pre>\n\n\n\n<p>\u5173\u952e\u7684\u533a\u522b\u5728\u4e8e\uff0c\u6700\u5927\u6ed1\u7a97\u662f\u5728\u8fed\u4ee3\u53f3\u79fb\u53f3\u8fb9\u754c\u7684\u8fc7\u7a0b\u4e2d\u66f4\u65b0\u7ed3\u679c\uff0c\u800c\u6700\u5c0f\u6ed1\u7a97\u662f\u5728\u8fed\u4ee3\u53f3\u79fb\u5de6\u8fb9\u754c\u7684\u8fc7\u7a0b\u4e2d\u66f4\u65b0\u7ed3\u679c\u3002\u56e0\u6b64\u867d\u7136\u90fd\u662f\u6ed1\u7a97\uff0c\u4f46\u662f\u4e24\u8005\u7684\u6a21\u677f\u548c\u5bf9\u5e94\u7684\u8d2a\u5fc3\u601d\u8def\u5e76\u4e0d\u4e00\u6837\uff0c\u800c\u771f\u6b63\u7406\u89e3\u540e\u5c31\u53ef\u4ee5\u5728lc.76\uff0clc.904\uff0clc.3, lc.1004\u5199\u51fa\u975e\u5e38\u65e0\u8111\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1aO(N) \u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(N)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u94fe\u8868<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">\u5b9a\u4e49<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u5355\u94fe\u8868<br>struct ListNode {<br> &nbsp; &nbsp;int val; &nbsp;\/\/ \u8282\u70b9\u4e0a\u5b58\u50a8\u7684\u5143\u7d20<br> &nbsp; &nbsp;ListNode *next; &nbsp;\/\/ \u6307\u5411\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488<br> &nbsp; &nbsp;ListNode(int x) : val(x), next(NULL) {} &nbsp;\/\/ \u8282\u70b9\u7684\u6784\u9020\u51fd\u6570<br>};<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5220\u9664\u94fe\u8868\u5143\u7d20<\/h5>\n\n\n\n<h6 class=\"wp-block-heading\">\u533a\u5206\u5934\u8282\u70b9\u548c\u975e\u5934\u8282\u70b9<\/h6>\n\n\n\n<pre class=\"wp-block-preformatted\">ListNode* removeElements(ListNode* head, int val) {<br> &nbsp; &nbsp;\/\/ \u5220\u9664\u5934\u7ed3\u70b9<br> &nbsp; &nbsp;while (head != NULL &amp;&amp; head-&gt;val == val) { \/\/ \u6ce8\u610f\u8fd9\u91cc\u4e0d\u662fif<br> &nbsp; &nbsp; &nbsp; &nbsp;ListNode* tmp = head;<br> &nbsp; &nbsp; &nbsp; &nbsp;head = head-&gt;next;<br> &nbsp; &nbsp; &nbsp; &nbsp;delete tmp;<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;\/\/ \u5220\u9664\u975e\u5934\u7ed3\u70b9<br> &nbsp; &nbsp;ListNode* cur = head;<br> &nbsp; &nbsp;while (cur != NULL &amp;&amp; cur-&gt;next!= NULL) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (cur-&gt;next-&gt;val == val) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListNode* tmp = cur-&gt;next;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur-&gt;next = cur-&gt;next-&gt;next;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete tmp;<br> &nbsp; &nbsp; &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur = cur-&gt;next;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;return head;<br>}<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">\u8bbe\u7f6e\u4e00\u4e2a\u865a\u62df\u5934\u8282\u70b9<\/h6>\n\n\n\n<pre class=\"wp-block-preformatted\">ListNode* removeElements(ListNode* head, int val) {<br> &nbsp; &nbsp;ListNode* dummyHead = new ListNode(0); \/\/ \u8bbe\u7f6e\u4e00\u4e2a\u865a\u62df\u5934\u7ed3\u70b9<br> &nbsp; &nbsp;dummyHead-&gt;next = head; \/\/ \u5c06\u865a\u62df\u5934\u7ed3\u70b9\u6307\u5411head\uff0c\u8fd9\u6837\u65b9\u9762\u540e\u9762\u505a\u5220\u9664\u64cd\u4f5c<br> &nbsp; &nbsp;ListNode* cur = dummyHead;<br> &nbsp; &nbsp;while (cur-&gt;next != NULL) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if(cur-&gt;next-&gt;val == val) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListNode* tmp = cur-&gt;next;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur-&gt;next = cur-&gt;next-&gt;next;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete tmp;<br> &nbsp; &nbsp; &nbsp;  } else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cur = cur-&gt;next;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br> &nbsp; &nbsp;head = dummyHead-&gt;next;<br> &nbsp; &nbsp;delete dummyHead;<br> &nbsp; &nbsp;return head;<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u53c9\u6811\u904d\u5386<\/h3>\n\n\n\n<h5 class=\"wp-block-heading\">\u524d\u4e2d\u540e\u5e8f\u904d\u5386\uff08\u9012\u5f52\u7b80\u5355\uff09<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u4ee5\u524d\u5e8f\u4e3a\u4f8b<br>class Solution {<br>public:<br> &nbsp; &nbsp;void traversal(TreeNode* cur, vector&lt;int&gt;&amp; vec) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (cur == NULL) return;<br> &nbsp; &nbsp; &nbsp; &nbsp;vec.push_back(cur-&gt;val); &nbsp; &nbsp;\/\/ \u4e2d<br> &nbsp; &nbsp; &nbsp; &nbsp;traversal(cur-&gt;left, vec); &nbsp;\/\/ \u5de6<br> &nbsp; &nbsp; &nbsp; &nbsp;traversal(cur-&gt;right, vec); \/\/ \u53f3<br> &nbsp;  }<br> &nbsp; &nbsp;vector&lt;int&gt; preorderTraversal(TreeNode* root) {<br> &nbsp; &nbsp; &nbsp; &nbsp;vector&lt;int&gt; result;<br> &nbsp; &nbsp; &nbsp; &nbsp;traversal(root, result);<br> &nbsp; &nbsp; &nbsp; &nbsp;return result;<br> &nbsp;  }<br>};<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u5c42\u5e8f\u904d\u5386\uff08\u7528\u961f\u5217\u7b80\u5355\uff09<\/h5>\n\n\n\n<pre class=\"wp-block-preformatted\">class Solution {<br>public:<br> &nbsp; &nbsp;vector&lt;vector&lt;int&gt;&gt; levelOrder(TreeNode* root) {<br> &nbsp; &nbsp; &nbsp; &nbsp;queue&lt;TreeNode*&gt; que;<br> &nbsp; &nbsp; &nbsp; &nbsp;if (root != NULL) que.push(root);<br> &nbsp; &nbsp; &nbsp; &nbsp;vector&lt;vector&lt;int&gt;&gt; result;<br> &nbsp; &nbsp; &nbsp; &nbsp;while (!que.empty()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int size = que.size();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vector&lt;int&gt; vec;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fd9\u91cc\u4e00\u5b9a\u8981\u4f7f\u7528\u56fa\u5b9a\u5927\u5c0fsize\uff0c\u4e0d\u8981\u4f7f\u7528que.size()\uff0c\u56e0\u4e3aque.size\u662f\u4e0d\u65ad\u53d8\u5316\u7684<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i &lt; size; i++) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TreeNode* node = que.front();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;que.pop();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vec.push_back(node-&gt;val);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (node-&gt;left) que.push(node-&gt;left);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (node-&gt;right) que.push(node-&gt;right);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result.push_back(vec);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;return result;<br> &nbsp;  }<br>};<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u6bd4\u8f83\u51fd\u6570<\/h3>\n\n\n\n<p>bool cmp(const std::pair&lt;int, int&gt;&amp; p1, const std::pair&lt;int, int&gt;&amp; p2) { return p1.second &gt; p2.second; }<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6280\u5de7<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6307\u5411\u8d4b\u503c\u770b\u61c2\u6280\u5de7\uff1acur-&gt;next = cur-&gt;next-&gt;next; \u628a\u5de6\u8fb9\u770b\u6210cur\u7684\u8fde\u7ebf \u53f3\u8fb9\u770b\u6210\u5177\u4f53\u8282\u70b9 \u5373cur\u6307\u5411\u5b83\u7684\u540e\u9762\u7b2c\u4e8c\u4e2a\u8282\u70b9<\/p>\n\n\n\n<p>\u5982\u679c\u662fcur-&gt;next-&gt;next = p-&gt;next; \u5c31\u662f\u628acur-&gt;next\u8fd9\u4e2a\u8282\u70b9\u6307\u5411p-&gt;next\u8fd9\u4e2a\u5177\u4f53\u8282\u70b9 \u6ce8\u610f\u5de6\u8fb9\u603b\u662f\u5c11\u770b\u4e00\u4e2anext \u56e0\u4e3a\u8981\u505a\u8fde\u7ebf<\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\">STL<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Vector<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u521d\u59cb\u5316\uff1a\u2460 vector&lt;int&gt; v(nums.size()); \/\/\u6307\u5b9a\u5927\u5c0f\u4e3anums.size \uff08\u8be6\u89c1C++\u63d0\u9ad8\u7f16\u7a0b\u7b14\u8bb0\uff09<\/p>\n\n\n\n<p>\u2461 int a[] = {2,3,1,2,4,3}; vector&lt;int&gt; v(a, a + sizeof(a)\/sizeof(a[0])); \/\/\u5229\u7528\u6570\u7ec4\u521b\u5efavector<\/p>\n\n\n\n<p>\u2462 vector&lt;int&gt; v(nums.size(), 0); \/\/\u5927\u5c0f\u4e3anums.size\uff0c\u5143\u7d20\u5747\u4e3a0<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u6392\u5e8f\uff1asort(v.begin(), v.end());<\/p>\n\n\n\n<p>\u5c3e\u63d2\uff1apush_back(x);<\/p>\n\n\n\n<p>\u5c3e\u5220\uff1apush_pop();<\/p>\n\n\n\n<p>\u63d2\u5165\uff1av.insert( v.begin, a\uff09;<\/p>\n\n\n\n<p>\u5220\u9664\uff1aerase(const_iterator pos); erase(const_iterator start, const_iterator end);<\/p>\n\n\n\n<p>\u6e05\u7a7a\uff1aclear();<\/p>\n\n\n\n<p>\u53d6\u5934\uff1afront(); \/\/\u8fd4\u56de\u5bb9\u5668\u4e2d\u7b2c\u4e00\u4e2a\u6570\u636e\u5143\u7d20<\/p>\n\n\n\n<p>\u53d6\u5c3e\uff1aback(); \/\/\u8fd4\u56de\u5bb9\u5668\u4e2d\u6700\u540e\u4e00\u4e2a\u6570\u636e\u5143\u7d20<\/p>\n\n\n\n<p>\u6539\u53d8\u5bb9\u5668\u957f\u5ea6\uff1aresize(int num); \/\/\u91cd\u65b0\u6307\u5b9a\u5bb9\u5668\u7684\u957f\u5ea6\u4e3anum\uff0c\u82e5\u5bb9\u5668\u53d8\u957f\uff0c\u5219\u4ee5\u9ed8\u8ba4\u503c\u586b\u5145\u65b0\u4f4d\u7f6e\uff1b\u82e5\u5bb9\u5668\u53d8\u77ed\uff0c\u5219\u672b\u5c3e\u8d85\u51fa\u5bb9\u5668\u957f\u5ea6\u7684\u5143\u7d20\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<p>resize(int num, elem); \/\/\u91cd\u65b0\u6307\u5b9a\u5bb9\u5668\u7684\u957f\u5ea6\u4e3anum\uff0c\u82e5\u5bb9\u5668\u53d8\u957f\uff0c\u5219\u4ee5elem\u503c\u586b\u5145\u65b0\u4f4d\u7f6e\uff1b\u82e5\u53d8\u77ed\uff0c\u5219\u672b\u5c3e\u8d85\u51fa\u5bb9\u5668\u957f\u5ea6\u7684\u5143\u7d20\u88ab\u5220\u9664\u3002<\/p>\n\n\n\n<p>\u4ee5vector\u521d\u59cb\u5316set\uff1aunordered_set&lt;int&gt; nums_set(nums1.begin(), nums1.end());<\/p>\n\n\n\n<p>\u4ee5set\u8fd4\u56devector\u7c7b\u578b\uff1areturn vector&lt;int&gt;(result_set.begin(), result_set.end()); \u7528set\u65b0\u5efavector\u4e5f\u53ef\u4ee5<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Set<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>set&lt;int,greater&lt;int&gt;&gt; p; <em>\/\/\u964d\u5e8f\u6392\u5217 \u9ed8\u8ba4\u5c31\u662f\u5347\u5e8f<\/em><\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>q.insert(x); q.erase(x); \/\/\u5220\u9664q\u4e2d\u7684x\u5143\u7d20,\u8fd4\u56de0\u62161,0\u8868\u793aset\u4e2d\u4e0d\u5b58\u5728x q.clear(); \/\/\u6e05\u7a7aq q.empty(); \/\/\u5224\u65adq\u662f\u5426\u4e3a\u7a7a\uff0c\u82e5\u662f\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56de0 q.size(); q.find(x);<\/p>\n\n\n\n<p><em>\u8303\u56f4-based for \u5faa\u73af\uff1a\u904d\u5386 num_set \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u6253\u5370\u51fa\u6765<\/em> for (int num : num_set) cout &lt;&lt; num &lt;&lt; &#8221; &#8220;; \/\/num\u8868\u793a\u5185\u90e8\u7684\u5143\u7d20<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Map<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e0eunordered_map\u533a\u522b\uff1amap\u6709\u5e8f\uff0cunordered_map\u67e5\u627e\u901f\u5ea6\u5feb<\/p>\n<\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5584\u7528auto\u8fed\u4ee3\u5668\uff1a\u8ba9\u7f16\u8bd1\u5668\u81ea\u52a8\u8bc6\u522b\u662f\u4ec0\u4e48\u5bb9\u5668(\u4f46\u53ef\u80fd\u4f1a\u589e\u52a0\u65f6\u95f4\u590d\u6742\u5ea6)<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-preformatted\">auto it = mp.begin();<br>while (it!= mp.end())<br>{ &nbsp;<br> &nbsp; &nbsp;cout &lt;&lt; it-&gt;first &lt;&lt; \" \" &lt;&lt; it-&gt;second &lt;&lt; endl; &nbsp;<br> &nbsp; &nbsp;it++; &nbsp;<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">String<\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u622a\u53d6\u5b50\u4e32\uff1astring t = s.substr(start, len);<\/p>\n\n\n\n<p>\u67e5\u627e\u51fd\u6570\uff1astr.find(&#8220;de&#8221;) str.rfind(&#8220;de&#8221;) \/\/find\u662f\u4ece\u5de6\u5411\u53f3\u627e\uff0crfind\u662f\u4ece\u53f3\u5411\u5de6\u627e<\/p>\n\n\n\n<p>\u66ff\u6362\u51fd\u6570\uff1astr.replace(1, 3, &#8220;1111&#8221;); \/\/\u4ece\u4e0b\u6807\u4e3a1\u7684\u4f4d\u7f6e\u5f00\u59cb\u66ff\u6362\uff0c\u5c06\u4e09\u4e2a\u5143\u7d20\u66ff\u6362\u4e3a\u201c1111\u201d<\/p>\n\n\n\n<p>\u6bd4\u8f83\u51fd\u6570\uff1astr.compare(s); \/\/ = \u8fd4\u56de 0; &gt; \u8fd4\u56de 1; &lt; \u8fd4\u56de -1<\/p>\n\n\n\n<p>\u63d2\u5165\u51fd\u6570\uff1astr.insert(1, &#8220;aaa&#8221;); \/\/\u4ece\u4e0b\u6807\u4f4d\u7f6e1\u5f00\u59cb\u63d2\u5165<\/p>\n\n\n\n<p>\u5220\u9664\u51fd\u6570\uff1astr.erase(1, 3); \/\/\u4ece\u4e0b\u6807\u4f4d\u7f6e1\u5f00\u59cb\u5220\u9664\u4e09\u4e2a\u5b57\u7b26<\/p>\n\n\n\n<p>\u4ea4\u6362\u51fd\u6570\uff1aswap(s[i], s[j]);<\/p>\n\n\n\n<p>\u53cd\u8f6c\u51fd\u6570\uff1areverse(s.begin() + i, s.begin() + i + k);<\/p>\n\n\n\n<p>\u6269\u5145\u957f\u5ea6\uff1as.resize(s.size() + count * 2);<\/p>\n\n\n\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3along long\u7c7b\u578b\u7684\u6574\u6570\uff1along long num = stoll(str);<\/p>\n\n\n\n<p>\u5c06char\u6570\u7ec4\u8f6c\u6362\u4e3astring\uff1aint startPos = 6; <em>\/\/ \u8d77\u59cb\u4f4d\u7f6e<\/em> int length = 5; <em>\/\/ \u9700\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u6570<\/em> string str(charArray + startPos, length); <em>\/\/ \u5c06char\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u8f6c\u6362\u6210std::string\u7c7b\u578b<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>C++ Array\/Vector \u4e8c\u5206 int search(vector&lt;int&gt;&amp; n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[27,42],"class_list":["post-881","post","type-post","status-publish","format-standard","hentry","category-suanfa","tag-c","tag-42"],"_links":{"self":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=881"}],"version-history":[{"count":1,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions\/882"}],"wp:attachment":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}