02、自动驾驶编程语言与工具C++篇

首先需要一个编写和运行C++代码的环境。

这里推荐使用Visual Studio Code配合GCC编译器。

  1. 下载并安装Visual Studio Code:可以从Visual Studio Code官方网站下载并安装。

  2. 安装GCC编译器:如果你使用的是Windows系统,可以安装MinGW。详细的安装步骤可以参考MinGW安装指南。如果你使用的是Linux或macOS,通常可以直接通过包管理器安装GCC。例如,在Linux上可以使用sudo apt-get install gcc。

  3. 配置Visual Studio Code:在Visual Studio Code中安装C++扩展(C/C++ by Microsoft)。然后,在终端中确认GCC编译器已经正确安装。

编写第一个C++程序

我们从一个简单的“Hello, World!”程序开始。

  1. 打开Visual Studio Code,新建一个文件,命名为hello.cpp。

  2. 在文件中输入以下代码:

#include <iostream> 
int main() { 
    std::cout << "Hello, World!" << std::endl;
    return 0; 
}
  1. 保存文件,然后在终端中使用以下命令编译和运行程序:

g++ hello.cpp -o hello ./hello

你应该会看到输出:

Hello, World!

学习基础概念

接下来学习一些C++的基础概念,包括变量、数据类型和基本输入输出。

1. 变量和数据类型

在C++中,你可以使用各种类型的变量来存储数据。常见的数据类型有int(整数)、float(浮点数)、double(双精度浮点数)、char(字符)和bool(布尔型)。

#include <iostream> 
int main() { 
    int a = 10; 
    float b = 3.14f; 
    double c = 2.71828; 
    char d = 'A'; 
    bool e = true; 
    std::cout << "int: " << a << std::endl; 
    std::cout << "float: " << b << std::endl; 
    std::cout << "double: " << c << std::endl; 
    std::cout << "char: " << d << std::endl; 
    std::cout << "bool: " << e << std::endl; 
    return 0; 
}
2. 基本输入输出

C++使用cin进行输入,使用cout进行输出。

#include <iostream> 
    int main() {
    int number; 
    std::cout << "Enter an integer: "; 
    std::cin >> number; 
    std::cout << "You entered: " << number << std::endl; 
    return 0; 
}

控制结构

控制结构包括条件语句和循环语句。

1. 条件语句

使用if语句和switch语句可以根据条件执行不同的代码。

#include <iostream> 
    int main() { 
    int number; 
    std::cout << "Enter a number: "; 
    std::cin >> number; 
    if (number > 0) { 
        std::cout << "The number is positive." << std::endl; 
    } else if (number < 0) {
         std::cout << "The number is negative." << std::endl; 
    } else { 
         std::cout << "The number is zero." << std::endl; 
    } 

    switch (number) 
        { case 0: 
                std::cout << "The number is zero." << std::endl; 
                break; 
          case 1: 
                std::cout << "The number is one." << std::endl; 
                break; 
        default: 
                std::cout << "The number is not zero or one." << std::endl; 
        break; } 
    return 0; 
}
2. 循环语句

使用for循环、while循环和do-while循环可以重复执行代码。

#include <iostream> 
    int
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值