{"id":374,"date":"2023-02-08T00:49:30","date_gmt":"2023-02-07T16:49:30","guid":{"rendered":"https:\/\/9iwd.top\/?p=374"},"modified":"2023-02-08T00:49:46","modified_gmt":"2023-02-07T16:49:46","slug":"09-7-c%e7%bb%93%e6%9e%84%e4%bd%93%e6%a1%88%e4%be%8b%e4%ba%8c","status":"publish","type":"post","link":"http:\/\/9iwd.top\/?p=374","title":{"rendered":"09.8 C++\u7ed3\u6784\u4f53\u6848\u4f8b\u4e8c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\/\/ \u8f93\u51fa\u6d41\u5e93\r\n#include &lt;string>\/\/ \u5b57\u7b26\u4e32\u5934\u6587\u4ef6\r\r\nusing namespace std;\/\/\u4f7f\u7528\u6807\u51c6\u547d\u540d\u7a7a\u95f4\r\n\r\n\/\/\u8bfe\u7a0b\uff1a9.8 \u7ed3\u6784\u4f53\u6848\u4f8b\u4e8c\r\n\r\n\/\/\u521b\u5efa\u82f1\u96c4\u7ed3\u6784\u4f53\r\nstruct Hero\r\n{\r\n    string name;\/\/\u59d3\u540d\r\n    int age;\/\/\u5e74\u9f84\r\n    string sex;\/\/\u6027\u522b\r\n};\r\n\r\n\/\/\u521b\u5efa\u6392\u5e8f\u51fd\u6570\r\nvoid bubb1eSort(struct Hero heroArray&#91;], int len)\r\n{\r\n    \/\/\u5916\u90e8\u5faa\u73af\u6b21\u6570 = \u5143\u7d20\u4e2a\u6570 - 1\r\n    for (int i = 0; i &lt; len - 1; i++)\r\n    {\r\n        \/\/\u5d4c\u5957\u5faa\u73af\uff0c\u6267\u884c\u6570\u636e\u4e92\u6362\uff0c\u6b21\u6570 = \u5143\u7d20\u4e2a\u6570 - \u5f53\u524d\u8f6e\u6570 - 1\r\n        for (int j = 0; j &lt; len - i - 1; j++)\r\n        {\r\n            \/\/\u5224\u65ad\uff1a\u5982\u679cheroArray&#91;j] > heroArray&#91;j + 1]\u5c06\u4e24\u4e2a\u5143\u7d20\u6570\u636e\u4ea4\u6362\r\n            if (heroArray&#91;j].age  > heroArray&#91;j + 1].age)\r\n            {\r\n                \/\/\u521b\u5efa\u82f1\u96c4\u4e34\u65f6\u7ed3\u6784\u8fdb\u884c\u8c03\u6362\r\n                struct Hero temp = heroArray&#91;j];\r\n                heroArray&#91;j] = heroArray&#91;j + 1];\r\n                heroArray&#91;j + 1] = temp;\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n\/\/\u521b\u5efa\u6253\u5370\u51fd\u6570\r\nvoid printHero(struct Hero heroArray&#91;], int len)\r\n{\r\n    for (int i = 0; i &lt; len; i++)\r\n    {\r\n        cout &lt;&lt; \"\u59d3\u540d\uff1a\" &lt;&lt; heroArray&#91;i].name\r\n            &lt;&lt; \" \u5e74\u9f84\uff1a\" &lt;&lt; heroArray&#91;i].age\r\n            &lt;&lt; \" \u6027\u522b\uff1a\" &lt;&lt; heroArray&#91;i].sex &lt;&lt; endl;\r\n    }\r\n}\r\n\r\nint main() {\r\n    \/\/1\u3001\u8bbe\u8ba1\u82f1\u96c4\u7ed3\u6784\u4f53\r\n    \/\/2\u3001\u521b\u5efa\u6570\u7ec4\u5b58\u653e5\u540d\u82f1\u96c4\r\n    struct Hero heroArray&#91;] =\r\n    {\r\n        {\"\u5218\u5907\",23,\"\u7537\"},\r\n        {\"\u5173\u7fbd\",22,\"\u7537\"},\r\n        {\"\u5f20\u98de\",20,\"\u7537\"},\r\n        {\"\u8d75\u4e91\",21,\"\u7537\"},\r\n        {\"\u8c82\u8749\",19,\"\u5973\"}\r\n    };\r\n\r\n    \/\/\u53d6\u6570\u7ec4\u957f\u5ea6\r\n    int len = sizeof(heroArray) \/ sizeof(heroArray&#91;0]);\r\n    cout &lt;&lt; \"\u6392\u5e8f\u524d\u7684\uff1a\" &lt;&lt; endl;\r\n    printHero(heroArray, len);\r\n\r\n    \/\/3\u3001\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u6309\u7167\u5e74\u9f84\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\r\n    bubb1eSort(heroArray,len);\r\n\r\n    \/\/4\u3001\u5c06\u6392\u5e8f\u540e\u7ed3\u679c\u6253\u5370\u8f93\u51fa\r\n    cout &lt;&lt; \"\u6392\u5e8f\u540e\u7684\uff1a\" &lt;&lt; endl;\r\n    printHero(heroArray, len);\r\n\r\n    system(\"pause\");\/\/ \u63a7\u5236\u53f0\u6682\u505c\uff0c\u7b49\u5f85\u4e0b\u4e00\u6b65\u64cd\u4f5c\r\n    return 0;\/\/ \u7ed3\u675f\u8fd4\u56de\u503c\uff1a0\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"_links":{"self":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/posts\/374"}],"collection":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=374"}],"version-history":[{"count":0,"href":"http:\/\/9iwd.top\/index.php?rest_route=\/wp\/v2\/posts\/374\/revisions"}],"wp:attachment":[{"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/9iwd.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}