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 main() { 
        std::cout << "For loop: ";
             for (int i = 0; i < 5; ++i) {
                 std::cout << i << " "; 
              } 
                std::cout << std::endl; 
                std::cout << "While loop: "; 
                int j = 0; 
                while (j < 5) { 
                    std::cout << j << " "; ++j;
                 } 
                std::cout << std::endl; 
                std::cout << "Do-while loop: "; 
                int k = 0; 
                do { std::cout << k << " "; ++k; } 
                while (k < 5); 
                std::cout << std::endl; 
    return 0; 
}

定义和使用函数

函数是可重复使用的代码块,可以帮助组织和管理代码。

#include <iostream> // 函数声明 
int add(int a, int b); 
int main() { 
    int num1, num2; 
    std::cout << "Enter two integers: "; 
    std::cin >> num1 >> num2; 
    int sum = add(num1, num2); 
    std::cout << "The sum is: " << sum << std::endl; 
    return 0; 
} 
// 函数定义
int add(int a, int b) {
     return a + b; 
}

数组和指针

数组

数组是一种数据结构,用于存储一组相同类型的数据。你可以使用数组存储和访问多个值。

#include <iostream>

int main() {
    // 声明一个包含5个整数的数组
    int numbers[5] = {1, 2, 3, 4, 5};

    // 访问和打印数组元素
    for (int i = 0; i < 5; ++i) {
        std::cout << "Element at index " << i << ": " << numbers[i] << std::endl;
    }

    return 0;
}
动态数组

可以使用动态数组来处理大小在编译时未知的数据集合。

#include <iostream>

int main() {
    int size;
    std::cout << "Enter the size of the array: ";
    std::cin >> size;

    // 动态分配数组
    int* numbers = new int[size];

    // 输入数组元素
    std::cout << "Enter " << size << " elements: ";
    for (int i = 0; i < size; ++i) {
        std::cin >> numbers[i];
    }

    // 打印数组元素
    for (int i = 0; i < size; ++i) {
        std::cout << "Element at index " << i << ": " << numbers[i] << std::endl;
    }

    // 释放动态数组
    delete[] numbers;

    return 0;
}

指针

指针是一个变量,用于存储内存地址。它们在C++中非常强大,可以用于动态内存分配、数组处理和函数参数传递。

#include <iostream>

int main() {
    int number = 10;
    int* ptr = &number;  // 指针ptr指向变量number的地址

    std::cout << "Value of number: " << number << std::endl;
    std::cout << "Address of number: " << &number << std::endl;
    std::cout << "Value of ptr (address of number): " << ptr << std::endl;
    std::cout << "Value pointed to by ptr: " << *ptr << std::endl;  // 解引用指针

    return 0;
}
动态内存分配

动态内存分配允许你在运行时分配和释放内存,通常使用new和delete操作符。

#include <iostream>

int main() {
    // 动态分配一个整数
    int* ptr = new int;
    *ptr = 42;

    std::cout << "Dynamically allocated integer: " << *ptr << std::endl;

    // 释放动态分配的内存
    delete ptr;

    return 0;
}
函数中的指针

指针可以作为函数参数传递,这在需要修改原始数据或传递大数据结构时非常有用。

#include <iostream>

void increment(int* ptr) {
    (*ptr)++;
}

int main() {
    int number = 10;
    std::cout << "Before increment: " << number << std::endl;

    increment(&number);  // 传递变量的地址
    std::cout << "After increment: " << number << std::endl;

    return 0;
}

综合示例,演示了数组和指针的使用。

#include <iostream>

void printArray(int* arr, int size) {
    for (int i = 0; i < size; ++i) {
        std::cout << "Element at index " << i << ": " << arr[i] << std::endl;
    }
}

int main() {
    int size = 5;
    int* numbers = new int[size] {1, 2, 3, 4, 5};

    std::cout << "Original array:" << std::endl;
    printArray(numbers, size);

    for (int i = 0; i < size; ++i) {
        numbers[i] *= 2;
    }

    std::cout << "Modified array:" << std::endl;
    printArray(numbers, size);

    delete[] numbers;
    return 0;
}

接下来我们详细讲解类和对象、继承、多态以及封装和访问控制等C++的面向对象编程(OOP)概念

1. 类和对象

    类是C++种用于定义对象的蓝图。类包含成员变量(数据成员)和成员函数(方法)。

#include <iostream>
//定义一个类
class Person{
public:
    //成员函数
    std::string name;
    int age;
    
//成员函数
void introduce(){
    std::cout << "Hello, my name is "<<name<<"and I am"<<age<<"years old."<<std::endl;

    }
};

int main(){
    //创建对象
    Person person;
    person.name = "Alice";
    person.age = 30;

    //调用成员函数
    person.introduce();

    return 0;
}

构造函数和析构函数

    构造函数用于初始化对象,析构函数用于清理资源。

#include <iostream>

class Person{
public:
    std::string name;
    int age;
    
    //构造函数
    Person(std::string n, int a) : name(n), age(a){
        std::cout<<"Person created: "<<name<<std::endl;
    }

    //析构函数
    ~Person() {
        std::cout << "Person destroyed: "<<name<<std::endl;
    }

     void introduce(){
        std::cout<<"Hello, my name is "<<name<<"and I am"<<age<<"years old."<<std::endl;
    }
};

int main(){
    Person person("Alice", 30);
    person.introduce();

    return 0;
}

2.继承

继承是面向对象编程中的一个重要概念,用于实现代码复用和建立类之间的层次结构。

基本继承

#include <iostream>

//基类(父亲)
class Animal{
public:
    void eat(){
        std::cout<<"Eating..."<<std::endl;
    }
};

//派生类(子类)
class Dog::public Animal{
public:
    void bark(){
        std::cout<<"Barking..."<<std::endl;
    }
};

int main(){
    Dog dog;
    dog.eat(); //继承自Animal类
    dog.bark();

    return 0;
}

3.多态

多态允许同一个接口调用不同的具体实现。C++中的多态主要通过虚函数和抽象类实现;

虚函数

#include <iostream>

class Animal {
public:
    virtual void makeSound(){
        std::cout<<"Some generic animal sound"<<std::endl;
    }
};

class Dog : public Animal{
public:
    void makeSound() override {
        std::cout<< "Woof!"<<std::endl;
    }
};

class Cat : public Animal {
public:
    void makeSound() override{
        std::cout<<"Meow!"<<std::endl;
    }
};

void makeAnimalSound(Animal* animal){
    animal->makeSound();
}

int main(){
    Animal* dog = new Dog();
    Animal* cat = new Cat();

    makeAnimalSound(dog); //输出“Woof!”
    makeAnimalSound(cat);  //输出“Meow!”

    delete dog;
    delete cat;

    return 0;
}

抽象类

抽象类是包含纯虚函数的类,不能实例化,只能作为基类。、

#include <iostream>
class Shape{
public:
    virtual void draw() = 0; //纯虚函数
};

class Circle : public Shape {
public:
     void draw() override{
        std::cout<<"Drawing a circle"<<std::endl;
    }
};

class Square : public Shape{
public:
     void draw() override{
        std::cout<<"Drawing a square"<<std::endl;
    }
};

int main(){
    Shape* circle = new Circle();
    Shape* square = new Square();

    circle->draw(); //输出“Drawing a circle”
    square->draw(); //输出“Drawing a square”

    delete circle;
    delete square;

    return 0;
}

4.封装和访问控制

封装是将数据和封装操作在类内部,并通过访问控制来限制对数据的直接访问。C++提供了'public'、'protected'和'private'三个访问控制符。

#include <iostream>

class Person{
private:
    std::string name; //私有成员,外部不能直接访问
    int age;
public:
    //公共成员函数,可以访问私有成员
    void setName(std::string n){
        name = n;
    }

    void setAge(int a){
        age = a;
    }
    void introduce(){
    std::cout<<"Hello, my name is "<<name<<"and I am"<<age<<"years old." << std::endl;
    }
};

int main(){
    Person person;
    person.setName("Alice");
    person.setAge(30);
    person.introduce();

    return 0;
}
访问控制符解释
  • public:成员可以被任何地方访问。

  • protected:成员可以被类本身和派生类访问。

  • private:成员只能被类本身访问。

接下来我们详细讲解C++标准库(Standard Library)和标准模板库(STL)的核心部分,包括容器、算法和智能指针的使用。

1. STL容器

STL容器是用于存储和管理数据的类模板。以下是一些常用的STL容器及其用法。

vector

        vector是一种动态数组,可以自动调整大小。

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    vec.push_back(6);  // 添加元素
    vec.pop_back();    // 移除最后一个元素

    for (int val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

list

list是一个双向链表,适合频繁插入和删除操作。

#include <iostream>
#include <list>

int main() {
    std::list<int> lst = {1, 2, 3, 4, 5};

    lst.push_back(6);  // 添加元素
    lst.pop_front();   // 移除第一个元素

    for (int val : lst) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

deque

deque是双端队列,可以在两端快速插入和删除元素。

#include <iostream>
#include <deque>

int main() {
    std::deque<int> deq = {1, 2, 3, 4, 5};

    deq.push_front(0);  // 在前端添加元素
    deq.push_back(6);   // 在后端添加元素

    for (int val : deq) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

set

set是一个有序集合,自动去重并排序。

#include <iostream>
#include <set>

int main() {
    std::set<int> s = {5, 1, 4, 2, 3, 2};

    s.insert(6);  // 添加元素

    for (int val : s) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

map

map是一个键值对容器,自动根据键排序。

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> m;
    m["apple"] = 5;
    m["banana"] = 3;

    for (const auto& [key, value] : m) {
        std::cout << key << ": " << value << std::endl;
    }

    return 0;
}

2. 常用算法

STL提供了一些常用的算法函数,可以与容器一起使用。

sort

对容器中的元素进行排序。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {5, 2, 4, 3, 1};

    std::sort(vec.begin(), vec.end());  // 排序

    for (int val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

find

在容器中查找元素。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    auto it = std::find(vec.begin(), vec.end(), 3);  // 查找元素3

    if (it != vec.end()) {
        std::cout << "Found: " << *it << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    return 0;
}

accumulate

计算容器中元素的累积和。

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};

    int sum = std::accumulate(vec.begin(), vec.end(), 0);  // 累积和

    std::cout << "Sum: " << sum << std::endl;

    return 0;
}

3. 智能指针

智能指针是C++11引入的用于管理动态内存的工具,防止内存泄漏。

shared_ptr

shared_ptr是一个共享所有权的智能指针,多个shared_ptr可以指向同一块内存,内存会在最后一个指针被销毁时自动释放。

#include <iostream>
#include <memory>

int main() {
    std::shared_ptr<int> ptr1 = std::make_shared<int>(10);
    std::shared_ptr<int> ptr2 = ptr1;  // 共享所有权

    std::cout << "Value: " << *ptr1 << std::endl;
    std::cout << "Use count: " << ptr1.use_count() << std::endl;

    return 0;
}

unique_ptr

unique_ptr是独占所有权的智能指针,内存只能由一个unique_ptr管理。

#include <iostream>
#include <memory>

int main() {
    std::unique_ptr<int> ptr = std::make_unique<int>(10);

    std::cout << "Value: " << *ptr << std::endl;

    // std::unique_ptr<int> ptr2 = ptr;  // 错误:不能复制unique_ptr

    return 0;
}

weak_ptr

weak_ptr是一个不参与引用计数的智能指针,通常用于解决shared_ptr之间循环引用的问题。

#include <iostream>
#include <memory>

class Node {
public:
    std::shared_ptr<Node> next;
    std::weak_ptr<Node> prev;  // 使用weak_ptr打破循环引用

    Node() {
        std::cout << "Node created" << std::endl;
    }
    ~Node() {
        std::cout << "Node destroyed" << std::endl;
    }
};

int main() {
    std::shared_ptr<Node> node1 = std::make_shared<Node>();
    std::shared_ptr<Node> node2 = std::make_shared<Node>();

    node1->next = node2;
    node2->prev = node1;

    return 0;
}

  • 30
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值