<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ C++ - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ freeCodeCamp 是一个免费学习编程的开发者社区，涵盖 Python、HTML、CSS、React、Vue、BootStrap、JSON 教程等，还有活跃的技术论坛和丰富的社区活动，在你学习编程和找工作时为你提供建议和帮助。 ]]>
        </description>
        <link>https://www.freecodecamp.org/chinese/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ C++ - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/chinese/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sun, 10 May 2026 13:50:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/chinese/news/tag/c-2/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ 如何在 Visual Studio Code 中编写和运行 C 及 C++ 代码 ]]>
                </title>
                <description>
                    <![CDATA[ Visual Studio Code（或简称 VS Code）是一个非常常见且广泛使用的文本编辑器和 IDE（集成开发环境）。通过使用大量扩展，你可以将 VS Code 变得非常强大。 在开始介绍如何在 Visual Studio Code 上运行你的第一行 C 或 C++ 代码之前，让我根据你计算机上使用的操作系统，带你了解设置的整个过程。 C 和 C++ 编译器 要运行 C 或 C++ 代码，你只需要在你的计算机上安装一个有效的 C/C++ 编译器。如果你使用的是 Linux 操作系统，那么很有可能它已经安装在你的系统上了。但我们需要确保它已正确安装。 要检查你的系统上是否安装了编译器（GCC/G++/MinGW），你需要先检查编译器版本。 只需打开终端并使用 gcc --version 和 g++ --version。如果你得到版本号，那么说明编译器已经安装在你的系统上。 你可以在任何操作系统上使用相同的命令来检查版本，无论是 Windows、Linux 还是 macOS 系统。 如果你的终端反馈说它不知道 GCC 或 G++，那么你需要正确安装编译器。 如果你使用 ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/</link>
                <guid isPermaLink="false">66ac9b9a79db950405c051bb</guid>
                
                    <category>
                        <![CDATA[ VSCode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C 语言 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Miya Liu ]]>
                </dc:creator>
                <pubDate>Fri, 02 Aug 2024 01:19:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2024/08/asd.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">How to Write And Run C and C++ Code in Visual Studio Code</a>
      </p><!--kg-card-begin: markdown--><p>Visual Studio Code（或简称 VS Code）是一个非常常见且广泛使用的文本编辑器和 IDE（集成开发环境）。通过使用大量扩展，你可以将 VS Code 变得非常强大。</p>
<!-- more -->
<p>在开始介绍如何在 Visual Studio Code 上运行你的第一行 C 或 C++ 代码之前，让我根据你计算机上使用的操作系统，带你了解设置的整个过程。</p>
<h2 id="cc">C 和 C++ 编译器</h2>
<p>要运行 C 或 C++ 代码，你只需要在你的计算机上安装一个有效的 C/C++ 编译器。如果你使用的是 Linux 操作系统，那么很有可能它已经安装在你的系统上了。但我们需要确保它已正确安装。</p>
<p>要检查你的系统上是否安装了编译器（GCC/G++/MinGW），你需要先检查编译器版本。</p>
<p>只需打开终端并使用 <code>gcc --version</code> 和 <code>g++ --version</code>。如果你得到版本号，那么说明编译器已经安装在你的系统上。</p>
<p>你可以在任何操作系统上使用相同的命令来检查版本，无论是 Windows、Linux 还是 macOS 系统。</p>
<p>如果你的终端反馈说它不知道 GCC 或 G++，那么你需要正确安装编译器。</p>
<p>如果你使用的是最常用的 Windows 操作系统，那么我已经在 freeCodeCamp 上写了一篇详细的文章<a href="https://chinese.freecodecamp.org/news/how-to-write-and-run-c-cpp-code-on-visual-studio-code/">《如何在 Windows 上安装 C 和 C++ 编译器》</a>，一步步展示所有过程。确保先阅读整篇文章，因为它还包含一个完整的视频，为你提供全面的支持。</p>
<p>如果你使用的是其他操作系统，并且没有安装编译器，请确保在继续之前安装它们。</p>
<h2 id="vscodevscodeinsiders">如何安装 VS Code 或 VS Code Insiders</h2>
<p>你需要直接从官方网站下载 Visual Studio Code：<a href="https://code.visualstudio.com/">https://code.visualstudio.com/</a>。</p>
<p>如果你愿意，你也可以安装 VS Code Insiders，过程也是同样的。</p>
<p>Visual Studio Code Insiders 实际上是 Visual Studio Code 的 “Insiders” 版本，它包含每天发布的所有最新功能。你可以将 VS Code 视为稳定版，将 VS Code Insiders 视为预览版。</p>
<p>如果你想立即体验最新的更新，那么你也可以尝试 Visual Studio Code Insiders（我自己也在使用）。要下载 VS Code Insiders，可以访问其官方网站：<a href="https://code.visualstudio.com/insiders/">https://code.visualstudio.com/insiders/</a></p>
<p>确保下载与你的操作系统匹配的文件。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-163.png" alt="下载页面：VS Code" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>下载页面：VS Code</figcaption>
</figure>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-164.png" alt="下载页面：VS Code Insiders" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>下载页面：VS Code Insiders</figcaption>
</figure>
<p>安装过程非常简单。但我会按顺序向你展示所有步骤。现在，我将展示使用 VS Code Insiders 的安装过程，但你在这里看到的一切，对于 VS Code 也完全相同。</p>
<p>确保勾选“我接受协议”框，然后点击 <strong>Next</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-165.png" alt="接受协议并点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>接受协议并点击 Next</figcaption>
</figure>
<p>保持所有设置不变。不用更改任何内容。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-168.png" alt="点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 Next</figcaption>
</figure>
<p>点击 <strong>Next</strong>。再次点击 <strong>Next</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-170.png" alt="点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 Next</figcaption>
</figure>
<p>确保勾选所有框（✔），然后点击 <strong>Next</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-171.png" alt="勾选所有框，并点击 Next" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>勾选所有框，并点击 Next</figcaption>
</figure>
<p>点击 <strong>Install</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-172.png" alt="点击 Install" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 Install</figcaption>
</figure>
<p>完成安装可能需要一些时间。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-173.png" alt="等待安装完成" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>等待安装完成</figcaption>
</figure>
<p>点击 <strong>Finish</strong>。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-175.png" alt="点击 finish" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>点击 finish</figcaption>
</figure>
<p>恭喜 - 你已经成功在系统上安装了 VS Code/VS Code Insiders。现在，干杯！🥂</p>
<h2 id="ccvscodevscodeinsiders">如何为 C 和 C++ 代码准备 VS Code/VS Code Insiders</h2>
<p>首先，打开 VS Code 或 VS Code Insiders。</p>
<p>进入扩展选项卡。搜索 “C” 或 “C++” 并安装第一个已被微软验证的扩展。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-178.png" alt="安装 C/C++ 扩展" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>安装 C/C++ 扩展</figcaption>
</figure>
<p>还需要安装 <strong>C/C++ Extension Pack</strong>。这个扩展包同样应当由微软验证。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-179.png" alt="安装 C/C++ Extension Pack" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>安装 C/C++ Extension Pack</figcaption>
</figure>
<p>接下来，搜索 <strong>Code Runner</strong> 并安装该扩展。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-180.png" alt="安装 Code Runner 扩展" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>安装 Code Runner 扩展</figcaption>
</figure>
<p>现在，我们需要更改一些设置。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-177.png" alt="更改一些设置" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>更改一些设置</figcaption>
</figure>
<p>点击<strong>齿轮</strong>箱（它被称为管理部分），然后点击<strong>设置</strong>。或者，你也可以使用快捷键 <code>Ctrl</code> + <code>,</code>。对于 Mac 系统，需要将 <code>Ctrl</code> 键替换为 <code>Command</code> 键。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-182.png" alt="输入 “Run code in terminal” 并按下 Enter 键" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>输入 “Run code in terminal” 并按下 Enter 键</figcaption>
</figure>
<p>在搜索栏里输入 “Run code in terminal” 并按下 Enter 键。</p>
<p>向下滚动直到找到 <code>Code-runner: Run In Terminal</code>。确保该选项被勾选（✔）。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-184.png" alt="确保勾选该选项" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>确保勾选该选项</figcaption>
</figure>
<p>现在你需要重新启动你的 VS Code/VS Code Insiders。简单地关闭并重新打开程序即可。</p>
<h2 id="">如何测试你的代码</h2>
<p>只需打开 VS Code/VS Code Insiders，打开任何文件夹，并创建扩展名为 <code>.c</code> 的 C 文件和扩展名为 <code>.cpp</code> 的 C++ 文件。</p>
<p>写好代码后，你可以使用右上角的播放按钮直接运行代码。</p>
<figure class="kg-card kg-card-image kg-card-hascaption">
    <img src="https://www.freecodecamp.org/news/content/images/2023/01/image-185.png" alt="这就是从 VS Code/Insiders 运行任何 C/C++ 程序的方法" class="kg-image" width="600" height="400" loading="lazy">
    <figcaption>这就是从 VS Code/Insiders 运行任何 C/C++ 程序的方法</figcaption>
</figure>
<p>它将直接编译并运行代码。运行代码后，代码运行按钮会默认直接运行。你的电脑已经 100% 准备好编译和运行任何 C/C++ 编程代码了。😀</p>
<h2 id="">小结</h2>
<p>感谢你阅读整篇文章。如果这篇文章对你有帮助，你也可以在 <a href="https://www.freecodecamp.org/news/author/fahimbinamin/">freeCodeCamp</a> 查看我的其他文章。</p>
<p>如果你想与我联系，你可以通过 <a href="https://twitter.com/Fahim_FBA">Twitter</a>、<a href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a> 和 <a href="https://github.com/FahimFBA">GitHub</a> 与我联系。</p>
<p>如果你想定期学习各种编程语言和大量实际案例，你也可以<a href="https://www.youtube.com/@FahimAmin?sub_confirmation=1">订阅我的 YouTube 频道</a>（Code With FahimFBA）。</p>
<p>如果你想查看我的进展，你可以在我的 <a href="https://www.polywork.com/fahimbinamin">Polywork 时间线</a>上查看。</p>
<p>你也可以<a href="https://fahimbinamin.com/">访问我的网站</a>来了解更多关于我的信息和我在做的事情。</p>
<p>非常感谢！</p>
<!--kg-card-end: markdown--> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ 如何在 Windows 上安装 C 和 C++ 编译器 ]]>
                </title>
                <description>
                    <![CDATA[ 如果你想在 Windows 操作系统上运行 C 或 C++ 程序，那么你需要有合适的编译器。 MinGW 编译器是一个知名且广泛使用的软件，用于安装 C 和 C++ 编程语言的 GCC 和 G++ 编译器。 但是许多开发人员在安装编译器时遇到了困难，所以我将在本文中向大家展示所有步骤，并附上截图来帮助你完成安装。 我将使用 Windows 11，相同的过程适用于所有其他 Windows 操作系统。但是如果你使用的是 Windows XP，你需要更改一些步骤。 如果你也想观看我关于此主题的视频，请点击这里： 安装 MSYS2 首先，我们需要从 MSYS2 下载一个可执行文件。访问 MSYS2 的官方网站：https://www.msys2.org/。截至今天，网站看起来如下。 向下滚动，直到找到可执行文件的下载按钮。 简单地点击安装按钮，并将安装文件保存在你希望的任何位置。 完成可执行文件的下载。根据你的网络速度，下载时间不应该太长。 下载文件后，我们将获得这个可执行文件。 双击可执行文件，然后点击 Next。 保持名称不变，然后点击 Nex ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/how-to-install-c-and-cpp-compiler-on-windows/</link>
                <guid isPermaLink="false">66aca85179db950405c0523d</guid>
                
                    <category>
                        <![CDATA[ Windows ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C 语言 ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Miya Liu ]]>
                </dc:creator>
                <pubDate>Tue, 30 Jul 2024 07:19:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2024/08/banner_freeCodeCamp.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/how-to-install-c-and-cpp-compiler-on-windows/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">How to Install C and C++ Compilers on Windows</a>
      </p><!--kg-card-begin: markdown--><p>如果你想在 Windows 操作系统上运行 C 或 C++ 程序，那么你需要有合适的编译器。</p>
<p>MinGW 编译器是一个知名且广泛使用的软件，用于安装 C 和 C++ 编程语言的 GCC 和 G++ 编译器。</p>
<p>但是许多开发人员在安装编译器时遇到了困难，所以我将在本文中向大家展示所有步骤，并附上截图来帮助你完成安装。</p>
<p>我将使用 Windows 11，相同的过程适用于所有其他 Windows 操作系统。但是如果你使用的是 Windows XP，你需要更改一些步骤。</p>
<p>如果你也想观看我关于此主题的视频，请点击这里：</p>
<figure class="kg-card kg-embed-card" data-test-label="fitted">
        <div class="fluid-width-video-container">
          <div style="padding-top: 56.17977528089888%;" class="fluid-width-video-wrapper">
            <iframe width="356" height="200" src="https://www.youtube.com/embed/c7FjV8Gwk_M?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" title="How to Install C and C++ Compilers on Windows 10 / 11" name="fitvid0"></iframe>
          </div>
        </div>
      </figure>
<h2 id="msys2">安装 MSYS2</h2>
<p>首先，我们需要从 MSYS2 下载一个可执行文件。访问 MSYS2 的官方网站：<a href="https://www.msys2.org/">https://www.msys2.org/</a>。截至今天，网站看起来如下。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--8-.png" alt="Screenshot--8-" width="600" height="400" loading="lazy"></p>
<p>向下滚动，直到找到可执行文件的下载按钮。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--9-.png" alt="Screenshot--9-" width="600" height="400" loading="lazy"></p>
<p>简单地点击安装按钮，并将安装文件保存在你希望的任何位置。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--10--1.png" alt="Screenshot--10--1" width="600" height="400" loading="lazy"></p>
<p>完成可执行文件的下载。根据你的网络速度，下载时间不应该太长。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--11-.png" alt="Screenshot--11-" width="600" height="400" loading="lazy"></p>
<p>下载文件后，我们将获得这个可执行文件。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--12-.png" alt="Screenshot--12-" width="600" height="400" loading="lazy"></p>
<p>双击可执行文件，然后点击 <code>Next</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--13-.png" alt="Screenshot--13-" width="600" height="400" loading="lazy"></p>
<p>保持名称不变，然后点击 <code>Next</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--14--1.png" alt="Screenshot--14--1" width="600" height="400" loading="lazy"></p>
<p>保持所有内容不变，并点击 <code>Next</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--15-.png" alt="Screenshot--15-" width="600" height="400" loading="lazy"></p>
<p>给它一些时间来完成安装过程。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--16-.png" alt="Screenshot--16-" width="600" height="400" loading="lazy"></p>
<p>如果保持选中，则一旦你点击 <code>Finish</code>，MSYS2 终端将会打开。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--17-.png" alt="Screenshot--17-" width="600" height="400" loading="lazy"></p>
<p>我更喜欢这样做，但如果你想稍后再执行剩余任务，则需要从开始菜单自行打开终端。</p>
<p>在这种情况下，你需要点击开始按钮 &gt; 搜索 <code>MSYS2</code>，然后像下面的图片一样点击终端：</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--26-.png" alt="Screenshot--26-" width="600" height="400" loading="lazy"></p>
<p>让我假设我们已经成功打开了 <strong>MSYS2 MSYS</strong> 终端。</p>
<p>应用命令 <code>pacman -Syu</code> 来更新软件包数据库和基础软件包。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--19-.png" alt="Screenshot--19-" width="600" height="400" loading="lazy"></p>
<p>如果你看到这种安装提示，请输入 <code>Y</code> 并按回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--20-.png" alt="Screenshot--20-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--21-.png" alt="Screenshot--21-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--22-.png" alt="Screenshot--22-" width="600" height="400" loading="lazy"></p>
<p>输入 <code>Y</code> 并按回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--23-.png" alt="Screenshot--23-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--24-.png" alt="Screenshot--24-" width="600" height="400" loading="lazy"></p>
<p>终端将关闭。我们必须手动打开终端并更新其余的软件包。</p>
<p>点击开始按钮。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--25-.png" alt="Screenshot--25-" width="600" height="400" loading="lazy"></p>
<p>搜索名为 <strong>MSYS2 64bit</strong> 的文件夹。点击该文件夹展开并获得终端。通过点击 <strong>MSYS2 MSYS</strong> 打开终端。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--26--1.png" alt="Screenshot--26--1" width="600" height="400" loading="lazy"></p>
<p>通过应用命令 <code>pacman -Su</code> 来更新其余的软件包。如果终端告诉你需要执行命令 <code>pacman -Sy</code>，则可能需要执行此命令。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--27-.png" alt="Screenshot--27-" width="600" height="400" loading="lazy"></p>
<p>如果收到任何安装提示，则需要输入 <code>Y</code> 或 <code>y</code> 并按回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--29-.png" alt="Screenshot--29-" width="600" height="400" loading="lazy"></p>
<p>稍等一会儿完成安装。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--30-.png" alt="Screenshot--30-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--31-.png" alt="Screenshot--31-" width="600" height="400" loading="lazy"></p>
<p>安装完成后关闭窗口。</p>
<h2 id="gccg">安装 GCC 和 G++ 编译器</h2>
<p>点击开始按钮，找到 <strong>MSYS2 64bit</strong> 文件夹，点击该文件夹展开它。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--32-.png" alt="Screenshot--32-" width="600" height="400" loading="lazy"></p>
<p>如果你和我一样使用的是 <strong>64 位</strong>操作系统，那么我们需要使用 <strong>MSYS2 MinGW x64</strong> 终端。点击终端以打开它。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--33-.png" alt="Screenshot--33-" width="600" height="400" loading="lazy"></p>
<p>⚠️但是，如果你使用的是 <strong>32 位</strong>操作系统，那么你需要使用 <strong>MSYS2 MinGW x86</strong> 终端。然后，你需要打开该终端。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--34-.png" alt="Screenshot--34-" width="600" height="400" loading="lazy"></p>
<p>因为我使用的是 <strong>64 位</strong>操作系统，所以我打开了 64 位终端。使用命令 <code>pacman -S mingw-w64-x86_64-gcc</code> 以安装编译器。</p>
<p>⚠️如果你使用的是 <strong>32 位</strong>操作系统，那么你需要在你的 32 位终端中使用命令 <code>pacman -S mingw-w64-i686-gcc</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--35-.png" alt="Screenshot--35-" width="600" height="400" loading="lazy"></p>
<p>稍等一会儿。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--36-.png" alt="Screenshot--36-" width="600" height="400" loading="lazy"></p>
<p>如果收到安装提示，输入 <code>Y</code> 或 <code>y</code> 并按下回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--37-.png" alt="Screenshot--37-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--38-.png" alt="Screenshot--38-" width="600" height="400" loading="lazy"></p>
<p>等待安装过程完成。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--39-.png" alt="Screenshot--39-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--39--1.png" alt="Screenshot--39--1" width="600" height="400" loading="lazy"></p>
<p>编译器安装完成。</p>
<h2 id="">如何安装调试器</h2>
<p>如果你和我一样使用的是 <strong>64 位</strong>操作系统，那么你需要使用命令 <code>pacman -S mingw-w64-x86_64-gdb</code>。</p>
<p>⚠️如果你使用的是 <strong>32 位</strong>操作系统，那么你需要在你的 32 位终端中使用命令 <code>pacman -S mingw-w64-i686-gdb</code>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--41-.png" alt="Screenshot--41-" width="600" height="400" loading="lazy"></p>
<p>如果收到安装提示，输入 <code>Y</code> 或 <code>y</code> 并按下回车键。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--42-.png" alt="Screenshot--42-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--38--1.png" alt="Screenshot--38--1" width="600" height="400" loading="lazy"></p>
<p>等待安装过程完成。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--44-.png" alt="Screenshot--44-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--45-.png" alt="Screenshot--45-" width="600" height="400" loading="lazy"></p>
<p>你可以关闭终端。</p>
<h2 id="">如何将目录添加到环境变量的路径</h2>
<p>打开文件资源管理器。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--46-.png" alt="Screenshot--46-" width="600" height="400" loading="lazy"></p>
<p>假设你和我一样将 MSYS 安装到默认目录。如果你使用了自定义目录，则需要前往你安装的目录。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--47-.png" alt="Screenshot--47-" width="600" height="400" loading="lazy"></p>
<p>如果你和我一样使用的是 64 位操作系统，则前往 <strong>mingw64</strong> 文件夹。</p>
<p>⚠️如果你使用的是 32 位操作系统，请前往 <strong>mingw32</strong> 文件夹。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--48-.png" alt="Screenshot--48-" width="600" height="400" loading="lazy"></p>
<p>现在我们需要转到二进制文件夹，前往 <strong>bin</strong> 文件夹。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--49-.png" alt="Screenshot--49-" width="600" height="400" loading="lazy"></p>
<p>⚠️如果你使用的是 32 位操作系统，则需要进入 <strong>mingw32</strong> 文件夹 &gt; <strong>bin</strong> 文件夹。</p>
<p>复制该目录。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--51-.png" alt="Screenshot--51-" width="600" height="400" loading="lazy"></p>
<p>⚠️如果你使用的是 32 位操作系统，并且也将 MSYS2 安装到了默认目录，那么你的目录应该如下所示：</p>
<pre><code>C:\msys64\mingw32\bin
</code></pre>
<p>打开<strong>高级系统设置</strong>。你可以通过多种方式做到这一点，一种简单的方式是点击开始按钮并搜索它，如下图所示。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--52-.png" alt="Screenshot--52-" width="600" height="400" loading="lazy"></p>
<p>从高级选项卡中点击<strong>环境变量</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--54-.png" alt="Screenshot--54-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>Path</strong> 并选择它。然后点击<strong>编辑</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--57-.png" alt="Screenshot--57-" width="600" height="400" loading="lazy"></p>
<p>将出现如下窗口：</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--58-.png" alt="Screenshot--58-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--59-.png" alt="Screenshot--59-" width="600" height="400" loading="lazy"></p>
<p>会出现一个空白框。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--60-.png" alt="Screenshot--60-" width="600" height="400" loading="lazy"></p>
<p>在这里粘贴目录。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--61-.png" alt="Screenshot--61-" width="600" height="400" loading="lazy"></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--62-.png" alt="Screenshot--62-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>OK</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--63-.png" alt="Screenshot--63-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>OK</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--65-.png" alt="Screenshot--65-" width="600" height="400" loading="lazy"></p>
<p>点击 <strong>OK</strong>。</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--66-.png" alt="Screenshot--66-" width="600" height="400" loading="lazy"></p>
<p>如果你想获取所有步骤的视频，你也可以观看<a href="https://www.youtube.com/watch?v=0HD0pqVtsmw">这个视频</a>。</p>
<h2 id="">检查安装</h2>
<p>现在是时候检查我们是否成功安装了上述所有内容。</p>
<p>打开终端 / PowerShell / CMD，并逐一应用命令：</p>
<p>检查 <strong>GCC</strong> 版本：</p>
<pre><code class="language-powershell">gcc --version
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--68-.png" alt="Screenshot--68-" width="600" height="400" loading="lazy"></p>
<p>检查 <strong>G++</strong> 版本：</p>
<pre><code class="language-powershell">g++ --version
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--69-.png" alt="Screenshot--69-" width="600" height="400" loading="lazy"></p>
<p>检查 <strong>GDB</strong> 版本：</p>
<pre><code class="language-powershell">gdb --version
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/02/Screenshot--70-.png" alt="Screenshot--70-" width="600" height="400" loading="lazy"></p>
<h2 id="">结论</h2>
<p>希望本文能帮助你在 Windows 操作系统上为 C 和 C++ 程序安装编译器。</p>
<p>感谢你阅读整篇文章。如果它对你有帮助，你还可以查看我在 <a href="https://www.freecodecamp.org/news/author/fahimbinamin/">freeCodeCamp</a> 上的其他文章。</p>
<p>如果你想与我联系，可以通过 <a href="https://twitter.com/Fahim_FBA">Twitter</a>、<a href="https://www.linkedin.com/in/fahimfba/">LinkedIn</a> 和 <a href="https://github.com/FahimFBA">GitHub</a> 联系我。</p>
<p>如果你希望定期学习各种编程语言的实用例子，可以<a href="https://www.youtube.com/@FahimAmin?sub_confirmation=1">订阅我的 YouTube 频道</a>（Code With FahimFBA）。</p>
<p>如果你想查看我的进展，可以访问我的 <a href="https://www.polywork.com/fahimbinamin">Polywork时间线</a>。</p>
<p>你也可以<a href="https://fahimbinamin.com/">访问我的网站</a>以了解更多关于我的信息和我目前正在做的工作。</p>
<p>非常感谢！</p>
<!--kg-card-end: markdown--> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ 在 C++ 中使用 to_string 将 Int 转换为 String ]]>
                </title>
                <description>
                    <![CDATA[ 当你在代码中处理字符串时，你可能想执行某些操作，比如将两个字符串连接起来（或连在一起）。 但在有些情况下，你希望把数值当作字符串来处理，因为把字符串和整数连接起来会出错。 在这篇文章中，我们将看到如何使用 C++ 中的 to_string() 方法将一个整数转换为字符串。 如何用 to_string() 转换整数 为了使用 to_string() 方法，我们必须将整数作为参数传入。下面是语法的内容，以帮助你理解： to_string(INTEGER) 让我们看一个例子。 #include <iostream> using namespace std; int main() {     string first_name = "John";          int age = 80;       ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/int-to-string-in-cpp-how-to-convert-an-integer-with-tostring/</link>
                <guid isPermaLink="false">63b6d41e81727e0763145a5b</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Chengjun.L ]]>
                </dc:creator>
                <pubDate>Thu, 05 Jan 2023 10:12:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2023/01/oskar-yildiz-cOkpTiJMGzA-unsplash.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/int-to-string-in-cpp-how-to-convert-an-integer-with-tostring/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">Int to String in C++ – How to Convert an Integer with to_string</a>
      </p><p>当你在代码中处理字符串时，你可能想执行某些操作，比如将两个字符串连接起来（或连在一起）。</p><p>但在有些情况下，你希望把数值当作字符串来处理，因为把字符串和整数连接起来会出错。</p><p>在这篇文章中，我们将看到如何使用 C++ 中的 <code>to_string()</code> 方法将一个整数转换为字符串。</p><h2 id="-to_string-">如何用 to_string() 转换整数</h2><p>为了使用 <code>to_string()</code> 方法，我们必须将整数作为参数传入。下面是语法的内容，以帮助你理解：</p><pre><code class="language-txt">to_string(INTEGER)</code></pre><p>让我们看一个例子。</p><pre><code class="language-c">#include &lt;iostream&gt;
using namespace std;

int main() {

    string first_name = "John";
    
    int age = 80;
    
    cout &lt;&lt; first_name + " is " + age + " years old";
    
}</code></pre><p>从上面的代码中，你会期望在控制台中看到 “John is 80 years old”。但这实际上会返回一个错误，因为我们正试图用一个整数来连接字符串。</p><p>让我们使用 <code>to_string()</code> 方法来解决这个问题。</p><pre><code class="language-c">#include &lt;iostream&gt;
using namespace std;

int main() {

    string first_name = "John";
    
    int age = 80;
    
    string AGE_TO_STRING = to_string(age);
    
    cout &lt;&lt; first_name + " is " + AGE_TO_STRING + " years old";
    
}</code></pre><p>我们创建了一个名为 <code>AGE_TO_STRING</code> 的新变量，在 <code>to_string()</code> 方法的帮助下存储了 <code>age</code> 变量的字符串值。</p><p>正如你在例子中看到的，整数的 <code>age</code> 被作为参数传入到 <code>to_string()</code> 方法中，转换为字符串。</p><p>现在，当我们运行这段代码时，我们将 “John is 80 years old” 打印到控制台。</p><p><code>to_string()</code> 方法在我们想将 <code>float</code> 和 <code>double</code> 数据类型的值——用于存储带小数的数字——转换为字符串时也能发挥作用。</p><p>下面是一些例子：</p><pre><code class="language-c">#include &lt;iostream&gt;
using namespace std;

int main() {

    string first_name = "John";
    
    float age = 10.5;
    
    string AGE_TO_STRING = to_string(age);
    
    cout &lt;&lt; first_name + " is " + AGE_TO_STRING + " years old";
    // John is 10.500000 years old
    
}</code></pre><p>上面的例子显示了一个 <code>float</code> 值被转换为一个字符串。在输出中（上面代码中的注释），你可以看到字符串中的小数值。</p><p>对于 <code>double</code> 数据类型：</p><pre><code class="language-c">#include &lt;iostream&gt;
using namespace std;

int main() {

    string first_name = "John";
    
    double age = 10.5;
    
    string AGE_TO_STRING = to_string(age);
    
    cout &lt;&lt; first_name + " is " + AGE_TO_STRING + " years old";
    // John is 10.500000 years old
    
}</code></pre><p>这与上一个例子的结果相同。唯一不同的是，我们使用的是一个 <code>double</code> 值。</p><h2 id="-">总结</h2><p>在这篇文章中，我们谈到了在 C++ 中使用 <code>to_string()</code> 方法将一个整数转换成一个字符串。</p><p>在例子中，我们试图将字符串和一个整数串联成一个较大的字符串，但这给了我们报错了。</p><p>使用 <code>to_string()</code> 方法，我们能够转换 <code>int</code>、<code>float</code> 和 <code>double</code> 数据类型的变量，并把它们当作字符串来使用。</p><p>Happy coding!</p> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ 如何在 C++ 中把整数转换为字符串 ]]>
                </title>
                <description>
                    <![CDATA[ 类型转换是将一个变量从一个数据类型转换到另一个数据类型的过程。 类型转换可以以隐式或显式方式进行。 隐式类型转换通过编译器自动完成，而显式类型转换则由开发者完成。 在这篇文章中，你将学习如何在 C++ 中使用 stringstream 类和 to_string() 方法将整数转换成字符串。 如何在 C++ 中使用 stringstream 类将整数转换成字符串 使用 stringstream 类，我们可以将整数转换为字符串。 这里有一个例子可以帮助你理解为什么你需要把一个整数转换成一个字符串： #include <iostream> using namespace std; int main() {     int age = 20;     cout << "The user is " + age + " years ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/how-to-convert-an-int-to-a-string-in-cpp/</link>
                <guid isPermaLink="false">6389cb47832e3f0781763a9b</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Chengjun.L ]]>
                </dc:creator>
                <pubDate>Fri, 02 Dec 2022 05:29:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2022/12/uday-awal-UjJWhMerJx0-unsplash.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p data-test-label="translation-intro">
        <strong>原文：</strong> <a href="https://www.freecodecamp.org/news/how-to-convert-an-int-to-a-string-in-cpp/" target="_blank" rel="noopener noreferrer" data-test-label="original-article-link">How to Convert an Int to a String in C++ – Integer Cast Tutorial</a>
      </p><p>类型转换是将一个变量从一个数据类型转换到另一个数据类型的过程。</p><p>类型转换可以以隐式或显式方式进行。</p><p>隐式类型转换通过编译器自动完成，而显式类型转换则由开发者完成。</p><p>在这篇文章中，你将学习如何在 C++ 中使用 <code>stringstream</code> 类和 <code>to_string()</code> 方法将整数转换成字符串。</p><h2 id="-c-stringstream-">如何在 C++ 中使用 stringstream 类将整数转换成字符串</h2><p>使用 <code>stringstream</code> 类，我们可以将整数转换为字符串。</p><p>这里有一个例子可以帮助你理解为什么你需要把一个整数转换成一个字符串：</p><pre><code class="language-c">#include &lt;iostream&gt;
using namespace std;

int main() {

    int age = 20;

    cout &lt;&lt; "The user is " + age + " years old";
    // error: invalid operands of types 'const char*' and 'const char [11]' to binary 'operator+'

    return 0; 

}
</code></pre><p>在上面的例子中，我们创建了一个 <code>int</code> 变量，其值为 20。</p><p>当我们尝试在一个字符串中连接这个值时，得到了一个错误：“invalid operands of types...（无效的操作数类型）”。</p><p>出现这个错误是因为我们试图使用两个不兼容的变量类型进行操作。解决办法是转换一个变量，使其与另一个兼容。</p><p><code>stringstream</code> 类有插入（<code>&lt;&lt;</code>）和提取（<code>&gt;&gt;</code>）操作符。</p><p>插入操作符用于将一个变量传递给流。在我们的例子中，将一个整数传递给流。</p><p>提取操作符用于给出修改后的变量。</p><p>换句话说，一个 <code>stringstream</code> 对象会接收一个数据类型，将其转换为另一个数据类型，并将新的数据类型分配给一个变量。</p><p>这里有一个例子：</p><pre><code class="language-c">#include &lt;iostream&gt;
#include &lt;sstream&gt;  
using namespace std;

int main() {

    int age = 20;
    
    // stringstream 对象
    stringstream stream;
    
    // 插入整数变量
    stream &lt;&lt; age;
    
    // 变量来保存来自流的新变量
    string age_as_string;
    
    // 提取整数变量的字符串类型
    stream &gt;&gt; age_as_string;
    
    cout &lt;&lt; "The user is " + age_as_string + " years old";
    // The user is 20 years old

    return 0; 

}
</code></pre><p>在上面的代码中，我们创建了一个名为 <code>stream</code> 的 <code>stringstream</code> 对象。注意，你必须在使用 <code>stringstream</code> 类之前包括它：<code>include &lt;sstream&gt;</code>。</p><p>然后我们将整数插入到 stream 中：<code>stream &lt;&lt; age;</code>。</p><p>之后，我们创建了一个名为 <code>age_as_string</code> 的新变量。这个变量将存储将从流中提取的字符串变量。</p><p>最后，我们提取了整数的字符串类型，并将其存储在上面创建的变量中：<code>stream &gt;&gt; age_as_string;</code>。</p><p>现在我们可以将这些字符串连接起来，得到所需的结果：</p><pre><code class="language-c">    cout &lt;&lt; "The user is " + age_as_string + " years old";
    // The user is 20 years old</code></pre><h2 id="-c-to_string-">如何在 C++ 中使用 to_string() 方法将整数转换为字符串</h2><p>你可以使用 <code><a href="https://www.freecodecamp.org/chinese/news/int-to-string-in-cpp-how-to-convert-an-integer-with-tostring/">to_string()</a></code> 方法将 int、float 和 double 数据类型转换为字符串。</p><p>下面是一个例子：</p><pre><code class="language-c">#include &lt;iostream&gt;
using namespace std;

int main() {

    int age = 20;
    
    string age_as_string = to_string(age);
    
    cout &lt;&lt; "The user is " + age_as_string + " years old";
    // The user is 20 years old

    return 0; 

}
</code></pre><p>在上面的代码中，我们把 <code>age</code> 变量传给了 <code>to_string()</code>：<code>to_string()</code>: <code>string age_as_string = to_string(age);</code>。</p><p>这就把 <code>age</code> 变量转换成了一个字符串。就像上一节的例子一样，我们现在可以把这个变量作为一个字符串使用：</p><pre><code class="language-c">cout &lt;&lt; "The user is " + age_as_string + " years old";
// The user is 20 years old</code></pre><h2 id="-"><strong>小结</strong></h2><p>在这篇文章中，我们谈到了在 C++ 中把整数转换为字符串的不同方法。</p><p>这些例子展示了如何使用 <code>stringstream</code> 类和 <code>to_string()</code> 方法将整数转换成字符串。</p><p>Happy coding!</p> ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ C++ 中如何将字符串转换为整数 ]]>
                </title>
                <description>
                    <![CDATA[ 当你使用 C++ 进行编码时，经常会需要将一种数据类型转换为另一种数据类型。 在本文中，你将通过查看两种最常用的方法来学习如何在 C++ 中将字符串转换为整数。 让我们开始吧！ C++ 中的数据类型 C++ 编程语言有一些内置的数据类型：  * int，用于整数（例如 10、150）  * double，用于浮点数（例如 5.0、4.5）  * char，用于单个字符（例如 “D”、“!”）  * string，用于字符序列（例如 “Hello”）  * bool，用于布尔值（真或假） C++ 是一种强类型编程语言，这意味着当你创建一个变量时，你必须明确声明将在其中存储什么类型的值。 如何在 C++ 中声明和初始化 int 要在 C++ 中声明一个 int 变量，你需要首先编写变量的数据类型——在本例中为 int。这将让编译器知道变量可以存储什么样的值，从而知道它可以采取什么行动。 接下来，你需要为变量命名。 最后，不要忘记用分号结束语句！ #include <iostream> int main() {  ]]>
                </description>
                <link>https://www.freecodecamp.org/chinese/news/string-to-int-in-c-how-to-convert-a-string-to-an-integer-example/</link>
                <guid isPermaLink="false">6176ac7dd05b5a0660d4f700</guid>
                
                    <category>
                        <![CDATA[ C++ ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Miya Liu ]]>
                </dc:creator>
                <pubDate>Mon, 25 Oct 2021 10:08:00 +0000</pubDate>
                <media:content url="https://chinese.freecodecamp.org/news/content/images/2021/10/nick-hillier-yD5rv8_WzxA-unsplash.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>当你使用 C++ 进行编码时，经常会需要将一种数据类型转换为另一种数据类型。</p><p>在本文中，你将通过查看两种最常用的方法来学习如何在 C++ 中将字符串转换为整数。</p><p>让我们开始吧！</p><h2 id="c-">C++ 中的数据类型</h2><p>C++ 编程语言有一些内置的数据类型：</p><ul><li><code>int</code>，用于整数（例如 10、150）</li><li><code>double</code>，用于浮点数（例如 5.0、4.5）</li><li><code>char</code>，用于单个字符（例如 “D”、“!”）</li><li><code>string</code>，用于字符序列（例如 “Hello”）</li><li><code>bool</code>，用于布尔值（真或假）</li></ul><p>C++ 是一种<strong>强类型</strong>编程语言，这意味着当你创建一个变量时，你必须明确声明将在其中存储什么类型的值。</p><h2 id="-c-int">如何在 C++ 中声明和初始化 <strong><code>int</code></strong></h2><p>要在 C++ 中声明一个 <code>int</code> 变量，你需要首先编写变量的数据类型——在本例中为 <code>int</code>。这将让编译器知道变量可以存储什么样的值，从而知道它可以采取什么行动。</p><p>接下来，你需要为变量命名。</p><p>最后，不要忘记用分号结束语句！</p><pre><code class="language-cpp">#include &lt;iostream&gt;

int main() {
    int age;
}
</code></pre><p>然后，你可以为你创建的变量赋值，如下所示：</p><pre><code class="language-cpp">#include &lt;iostream&gt;

int main() {
    int age;
    age = 28;
}
</code></pre><p>你可以通过初始化变量并最终打印结果来组合它们，而不是将这些操作作为单独的步骤执行：</p><pre><code class="language-cpp">// a header file that enables the use of functions for outputing information
//e.g. cout or inputing information e.g. cin
#include &lt;iostream&gt; 

// a namespace statement; you won't have to use the std:: prefix
using namespace std;


int main() { // start of main function of the program
    int age = 28; 
    // initialize a variable. 
    //Initializing  is providing the type,name and value of the varibale in one go.

    // output to the console: "My age is 28",using chaining, &lt;&lt;
    cout &lt;&lt; "My age is: " &lt;&lt; age &lt;&lt; endl;
}// end the main function
</code></pre><h2 id="-c-string">如何在 C++ 中声明和初始化 <strong><code>string</code></strong></h2><p>字符串是单个字符的集合。</p><p>在 C++ 中声明字符串的工作方式与声明和初始化整数非常相似，你在上一节中看到了这一点。</p><p>C++ 标准库提供了一个字符串类。为了使用字符串数据类型，你必须在文件顶部的 <code>#include &lt;iostream&gt;</code> 之后包含 <code>&lt;string&gt;</code> 库。</p><p>包含该头文件后，你还可以添加你之前看到的 <code>using namespace std;</code>。</p><p>除此之外，在添加这一行之后，你在创建字符串变量时将不必使用 <code>std::string</code> ——只需单独使用 <code>string</code> 即可。</p><pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
    //declare a string variable

    string greeting;
    greeting = "Hello";
    //the `=` is the assignment operator,assigning the value to the variable

}
</code></pre><p>或者你可以初始化一个字符串变量，并将其打印到控制台：</p><pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
    //initialize a string variable

    string greeting = "Hello";
   
   //output "Hello" to the console
   cout &lt;&lt; greeting &lt;&lt; endl;
}
</code></pre><h2 id="-">如何将字符串转换为整数</h2><p>如前所述，C++ 是一种<em>强类型</em>语言。</p><p>如果你尝试提供与数据类型不一致的值，你将收到错误消息。</p><p>此外，将字符串转换为整数并不像使用类型转换那么简单，你可以在将 <code>double</code> 转换为 <code>int</code> 时使用它。</p><p>例如，你不能这样做：</p><pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
   string str = "7";
   int num;

   num = (int) str;
}
</code></pre><p>编译后的错误将是：</p><pre><code>hellp.cpp:9:10: error: no matching conversion for C-style cast from 'std::__1::string' (aka
      'basic_string&lt;char, char_traits&lt;char&gt;, allocator&lt;char&gt; &gt;') to 'int'
   num = (int) str;
         ^~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:875:5: note: candidate function
    operator __self_view() const _NOEXCEPT { return __self_view(data(), size()); }
    ^
1 error generated.
</code></pre><p>有几种方法可以将字符串转换为 int，您将在接下来的部分中看到其中提到的两种方法。</p><h3 id="-stoi-int">如何使用 <code>stoi()</code> 函数将字符串转换为 <code>int</code></h3><p>将字符串对象转换为数字 <code>int</code> 的一种有效方法是使用 <code>stoi()</code> 函数。</p><p>此方法通常用于较新版本的 C++，在 C++11 中引入。</p><p>它接受一个字符串值作为输入，并返回它的整数版本作为输出。</p><pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
   // a string variable named str
   string str = "7";
   //print to the console
   cout &lt;&lt; "I am a string " &lt;&lt; str &lt;&lt; endl;

   //convert the string str variable to have an int value
   //place the new value in a new variable that holds int values, named num
   int num = stoi(str);
   
   //print to the console
   cout &lt;&lt; "I am an int " &lt;&lt; num &lt;&lt; endl;
}
</code></pre><p>输出：</p><pre><code>I am a string 7
I am an int 7
</code></pre><h3 id="-stringstream-int">如何使用 <code>stringstream</code> 类将字符串转换为 int</h3><p><code>stringstream</code> 类主要用于早期版本的 C++。它通过对字符串执行输入和输出来工作。</p><p>要使用它，你首先必须通过添加 <code>#include &lt;sstream&gt;</code> 行在程序顶部包含 <code>sstream</code> 库。</p><p>然后添加 <code>stringstream</code> 并创建一个 <code>stringstream</code> 对象，该对象将保存要转换为 int 的字符串的值，并将在将其转换为 int 的过程中使用。</p><p>你可以使用 <code>&lt;&lt;</code> 运算符从字符串变量中<em>提取</em>字符串。</p><p>最后，你使用 <code>&gt;&gt;</code> 运算符将新转换的 int 值输入到 int 变量中。</p><pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt; // this will allow you to use stringstream in your program

using namespace std;

int main() {
    //create a stringstream object, to input/output strings
   stringstream ss; 
   
   // a variable named str, that is of string data type
   string str = "7";
   
   // a variable named num, that is of int data type
   int num;
   
   
   //extract the string from the str variable (input the string in the stream)
   ss &lt;&lt; str;
   
   // place the converted value to the int variable
   ss &gt;&gt; num;
   
   //print to the consloe
   cout &lt;&lt; num &lt;&lt; endl; // prints the intiger value 7
}
</code></pre><h2 id="--1"><strong>总结</strong></h2><p>你已经看到了在 C++ 中将字符串转换为整数的两种简单方法。</p><p>如果你想了解有关 C++ 编程语言的更多信息，请查看 freeCodeCamp 的 4 小时的<a href="https://www.bilibili.com/video/BV1LE41187yt?spm_id_from=333.999.0.0">视频教程</a>。</p><p>感谢阅读，祝你学习愉快😊</p><p>原文：<a href="https://www.freecodecamp.org/news/string-to-int-in-c-how-to-convert-a-string-to-an-integer-example/">String to Int in C++ – How to Convert a String to an Integer Example</a>，作者：<a href="https://www.freecodecamp.org/news/author/dionysia/">Dionysia Lemonaki</a></p> ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
