{"id":1361,"date":"2025-02-04T00:59:02","date_gmt":"2025-02-03T16:59:02","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1361"},"modified":"2025-02-04T00:59:03","modified_gmt":"2025-02-03T16:59:03","slug":"c%e6%95%99%e7%a8%8b%e4%b9%8b%ef%bc%9a7-c%e5%ba%94%e7%94%a8%e4%b8%8e%e9%a1%b9%e7%9b%ae%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/04\/c%e6%95%99%e7%a8%8b%e4%b9%8b%ef%bc%9a7-c%e5%ba%94%e7%94%a8%e4%b8%8e%e9%a1%b9%e7%9b%ae%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"C++\u9879\u76ee\u5b9e\u8df5\u4e0e\u5e94\u7528\uff1a\u4ece\u7cfb\u7edf\u7f16\u7a0b\u5230\u6570\u636e\u5e93\u8fde\u63a5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">C++\u4e0d\u4ec5\u662f\u9ad8\u6027\u80fd\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u7406\u60f3\u9009\u62e9\uff0c\u800c\u4e14\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u591a\u7ebf\u7a0b\u5e76\u53d1\u4ee5\u53ca\u6027\u80fd\u4f18\u5316\u7b49\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u672c\u90e8\u5206\u5c06\u6df1\u5165\u63a2\u8ba8C++\u7684\u5b9e\u9645\u5e94\u7528\u548c\u9879\u76ee\u5b9e\u8df5\uff0c\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u638c\u63e1C++\u7684\u6838\u5fc3\u6280\u672f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C++\u5728\u7cfb\u7edf\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">C++\u5e38\u7528\u4e8e\u5f00\u53d1\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5982\u7cfb\u7edf\u5de5\u5177\u3001\u9a71\u52a8\u7a0b\u5e8f\u3001\u7f51\u7edc\u670d\u52a1\u7b49\u3002\u5b83\u5141\u8bb8\u76f4\u63a5\u8bbf\u95ee\u5e95\u5c42\u786c\u4ef6\u8d44\u6e90\uff0c\u5e76\u4e14\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u591a\u4efb\u52a1\u548c\u5e76\u53d1\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u64cd\u4f5c\u7cfb\u7edfAPI\uff08Windows\u3001Linux\uff09<\/strong><br>\u5728Windows\u548cLinux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0cC++\u80fd\u591f\u76f4\u63a5\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684API\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u3001\u7ebf\u7a0b\u63a7\u5236\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u901a\u8fc7C++\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edfAPI\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/li>\n\n\n\n<li><strong>Windows\uff1a<\/strong> <code>#include &lt;windows.h> int main() { MessageBox(NULL, \"Hello, World!\", \"Windows API\", MB_OK); return 0; }<\/code><\/li>\n\n\n\n<li><strong>Linux\uff1a<\/strong> <code>#include &lt;iostream> #include &lt;unistd.h> int main() { std::cout &lt;&lt; \"Hello from Linux!\" &lt;&lt; std::endl; sleep(2); \/\/ \u4f11\u77202\u79d2 return 0; }<\/code><\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u64cd\u4f5c\u3001\u8fdb\u7a0b\u7ba1\u7406\u3001\u7ebf\u7a0b\u5904\u7406<\/strong><br>C++\u901a\u8fc7\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u64cd\u4f5c\u652f\u6301\u3002\u53ef\u4ee5\u4f7f\u7528<code>&lt;fstream><\/code>\u8fdb\u884c\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u4f7f\u7528<code>&lt;thread><\/code>\u8fdb\u884c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002\u5bf9\u4e8e\u8fdb\u7a0b\u7ba1\u7406\uff0cWindows\u63d0\u4f9b\u4e86API\u5982<code>CreateProcess<\/code>\uff0cLinux\u5219\u53ef\u4ee5\u4f7f\u7528<code>fork()<\/code>\u3001<code>exec()<\/code>\u7b49\u7cfb\u7edf\u8c03\u7528\u3002<\/li>\n\n\n\n<li><strong>\u6587\u4ef6\u64cd\u4f5c\u793a\u4f8b\uff1a<\/strong> <code>#include &lt;fstream> #include &lt;iostream> int main() { std::ofstream outFile(\"example.txt\"); outFile &lt;&lt; \"Hello, C++ file operations!\" &lt;&lt; std::endl; outFile.close(); }<\/code><\/li>\n\n\n\n<li><strong>\u591a\u7ebf\u7a0b\u793a\u4f8b\uff1a<\/strong> <code>#include &lt;iostream> #include &lt;thread> void printHello() { std::cout &lt;&lt; \"Hello from a thread!\" &lt;&lt; std::endl; } int main() { std::thread t(printHello); t.join(); \/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f return 0; }<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C++\u4e0e\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">C++\u53ef\u4ee5\u4e0e\u591a\u4e2a\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\uff0c\u901a\u8fc7ODBC\u3001MySQL\u3001SQLite\u7b49\u6570\u636e\u5e93\u63a5\u53e3\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5b58\u50a8\u3001\u67e5\u8be2\u548c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528ODBC<\/strong><br>ODBC\uff08\u5f00\u653e\u6570\u636e\u5e93\u8fde\u63a5\uff09\u662f\u4e00\u79cd\u901a\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\u6807\u51c6\uff0cC++\u901a\u8fc7ODBC\u63a5\u53e3\u4e0e\u5404\u79cd\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/li>\n\n\n\n<li>ODBC\u8fde\u63a5\u7684\u57fa\u7840\u6b65\u9aa4\uff1a\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>SQLDriverConnect<\/code>\u6216<code>SQLConnect<\/code>\u5efa\u7acb\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u3002<\/li>\n\n\n\n<li>\u6267\u884cSQL\u67e5\u8be2\u3002<\/li>\n\n\n\n<li>\u5904\u7406\u67e5\u8be2\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;sql.h&gt;\n  #include &lt;sqlext.h&gt;\n\n  SQLHENV henv;\n  SQLHDBC hdbc;\n\n  \/\/ \u521d\u59cb\u5316ODBC\u73af\u5883\u548c\u6570\u636e\u5e93\u8fde\u63a5...<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528MySQL<\/strong><br>C++\u53ef\u4ee5\u901a\u8fc7MySQL Connector\u8fdb\u884c\u6570\u636e\u5e93\u8fde\u63a5\u548c\u64cd\u4f5c\u3002\u4f7f\u7528MySQL C API\u53ef\u4ee5\u6267\u884cSQL\u67e5\u8be2\u548c\u66f4\u65b0\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;mysql\/mysql.h&gt;\n\n  int main() {\n      MYSQL *conn;\n      conn = mysql_init(NULL);\n\n      if (mysql_real_connect(conn, \"localhost\", \"root\", \"password\", \"testdb\", 0, NULL, 0)) {\n          mysql_query(conn, \"SELECT * FROM users\");\n          \/\/ \u5904\u7406\u67e5\u8be2\u7ed3\u679c\n      }\n      mysql_close(conn);\n      return 0;\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528SQLite<\/strong><br>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u5e93\uff0c\u5b83\u4e0d\u9700\u8981\u4e00\u4e2a\u72ec\u7acb\u7684\u670d\u52a1\u5668\u3002\u901a\u8fc7SQLite C API\uff0cC++\u7a0b\u5e8f\u53ef\u4ee5\u8f7b\u677e\u6267\u884cSQL\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;sqlite3.h&gt;\n\n  int main() {\n      sqlite3* db;\n      sqlite3_open(\"test.db\", &amp;db);\n      sqlite3_exec(db, \"CREATE TABLE users (id INT, name TEXT);\", nullptr, nullptr, nullptr);\n      sqlite3_close(db);\n      return 0;\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>C++\u6027\u80fd\u4f18\u5316<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6027\u80fd\u4f18\u5316\u662fC++\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u8bfe\u9898\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u7a0b\u5e8f\u3002C++\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u6280\u672f\u6765\u5206\u6790\u548c\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u5206\u6790\u4e0e\u4f18\u5316\u6280\u672f<\/strong><br>\u6027\u80fd\u5206\u6790\u5de5\u5177\uff08\u5982<code>gprof<\/code>\u3001<code>valgrind<\/code>\uff09\u5e2e\u52a9\u5f00\u53d1\u8005\u8bc6\u522b\u7a0b\u5e8f\u4e2d\u7684\u74f6\u9888\u3002\u5e38\u89c1\u7684\u4f18\u5316\u6280\u672f\u5305\u62ec\uff1a<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5185\u5b58\u7ba1\u7406\u4f18\u5316<\/strong>\uff1a\u4f7f\u7528\u667a\u80fd\u6307\u9488\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff0c\u907f\u514d\u8fc7\u591a\u7684\u52a8\u6001\u5185\u5b58\u5206\u914d\u3002<\/li>\n\n\n\n<li><strong>\u7b97\u6cd5\u4f18\u5316<\/strong>\uff1a\u901a\u8fc7\u9009\u62e9\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\uff08\u4f8b\u5982\u9009\u62e9\u6392\u5e8f\u66ff\u6362\u4e3a\u5feb\u901f\u6392\u5e8f\uff09\u6765\u51cf\u5c11\u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u5e76\u53d1\u4f18\u5316<\/strong>\uff1a\u901a\u8fc7\u591a\u7ebf\u7a0b\u3001\u5e76\u884c\u8ba1\u7b97\u7b49\u65b9\u5f0f\u63d0\u9ad8\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u7684\u6027\u80fd\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;vector&gt;\n  #include &lt;algorithm&gt;\n\n  int main() {\n      std::vector&lt;int&gt; data = {5, 3, 8, 1};\n      std::sort(data.begin(), data.end());  \/\/ \u4f18\u5316\u6392\u5e8f\n      return 0;\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u591a\u7ebf\u7a0b\u4e0e\u5e76\u53d1\u7f16\u7a0b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u7ebf\u7a0b\u548c\u5e76\u53d1\u7f16\u7a0b\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u7684\u5173\u952e\uff0cC++\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ebf\u7a0b\u5e93<code>&lt;thread&gt;<\/code>\u3001<code>&lt;mutex&gt;<\/code>\u3001<code>&lt;future&gt;<\/code>\u7b49\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u7ebf\u7a0b\u5e76\u53d1\u7f16\u7a0b\u793a\u4f8b<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;iostream&gt;\n  #include &lt;thread&gt;\n\n  void task1() {\n      std::cout &lt;&lt; \"Task 1 is running\" &lt;&lt; std::endl;\n  }\n\n  void task2() {\n      std::cout &lt;&lt; \"Task 2 is running\" &lt;&lt; std::endl;\n  }\n\n  int main() {\n      std::thread t1(task1);\n      std::thread t2(task2);\n\n      t1.join();  \/\/ \u7b49\u5f85\u7ebf\u7a0b1\u7ed3\u675f\n      t2.join();  \/\/ \u7b49\u5f85\u7ebf\u7a0b2\u7ed3\u675f\n      return 0;\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5e76\u53d1\u540c\u6b65<\/strong><br>\u4f7f\u7528<code>std::mutex<\/code>\u53ef\u4ee5\u907f\u514d\u591a\u7ebf\u7a0b\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u65f6\u4ea7\u751f\u7684\u6570\u636e\u7ade\u4e89\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  #include &lt;iostream&gt;\n  #include &lt;thread&gt;\n  #include &lt;mutex&gt;\n\n  std::mutex mtx;\n\n  void printHello() {\n      mtx.lock();\n      std::cout &lt;&lt; \"Hello from a thread!\" &lt;&lt; std::endl;\n      mtx.unlock();\n  }\n\n  int main() {\n      std::thread t(printHello);\n      t.join();\n      return 0;\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u9879\u76ee\u5b9e\u6218<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9e\u8df5\u4e2d\uff0c\u7f16\u5199\u4e00\u4e2a\u5c0f\u578b\u7684C++\u9879\u76ee\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5de9\u56fa\u6240\u5b66\u77e5\u8bc6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9879\u76ee\u5b9e\u4f8b\uff0c\u4f9b\u4f60\u53c2\u8003\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u7ed3\u6784\u5b9e\u73b0<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u5b9a\u4e49\u94fe\u8868\u3001\u6808\u3001\u961f\u5217\u3001\u4e8c\u53c9\u6811\u7b49\u6570\u636e\u7ed3\u6784\uff0c\u6df1\u5165\u7406\u89e3\u5185\u5b58\u7ba1\u7406\u548c\u7b97\u6cd5\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7b80\u5355\u7684\u6e38\u620f<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528C++\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u63a7\u5236\u53f0\u6e38\u620f\uff0c\u5982\u8d2a\u5403\u86c7\u3001\u4e95\u5b57\u68cb\u7b49\uff0c\u5b66\u4e60\u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u56fe\u5f62\u548c\u4e8b\u4ef6\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7f51\u7edc\u5e94\u7528<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u7f51\u7edc\u804a\u5929\u7a0b\u5e8f\u6216\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u5e94\u7528\uff0c\u4f7f\u7528C++\u7684\u7f51\u7edc\u5e93\uff08\u5982<code>boost.asio<\/code>\uff09\u6765\u5904\u7406\u7f51\u7edc\u8fde\u63a5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C++\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6027\u80fd\u4f18\u5316\u3001\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7b49\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u5b9e\u8df5\u9879\u76ee\u548c\u4f18\u5316\u6280\u5de7\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u81ea\u5df1\u7684C++\u7f16\u7a0b\u80fd\u529b\u3002\u672c\u90e8\u5206\u7684\u76ee\u6807\u662f\u5e2e\u52a9\u4f60\u7406\u89e3C++\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f18\u52bf\uff0c\u5e76\u638c\u63e1\u5982\u4f55\u5c06C++\u6280\u672f\u5e94\u7528\u4e8e\u590d\u6742\u7684\u5de5\u7a0b\u9879\u76ee\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++\u4e0d\u4ec5\u662f\u9ad8\u6027\u80fd\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u7406\u60f3\u9009\u62e9\uff0c\u800c\u4e14\u5728\u7cfb\u7edf\u7f16\u7a0b\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u591a\u7ebf\u7a0b\u5e76\u53d1\u4ee5\u53ca\u6027\u80fd\u4f18\u5316\u7b49\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1361","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1361"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1361\/revisions"}],"predecessor-version":[{"id":1362,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1361\/revisions\/1362"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}