为了账号安全,请及时绑定邮箱和手机立即绑定

C++11工程实践资料:从零开始的编程之旅

标签:
C++
C++11基础简介

C++11,全名为C++03的修订版,旨在提升语言的现代性、灵活性和安全性。以下是一些关键的改进与新特性:

标准库的更新与改进

C++11对标准库进行了大幅增强,引入了多项新的容器、算法和迭代器特性:

#include <optional>
#include <vector>

int main() {
    std::optional<int> maybeNumber;
    if (maybeNumber.has_value()) {
        std::cout << maybeNumber.value() << std::endl;
    }
    return 0;
}

C++11语法规则基础

C++11引入了一些新的语法特性,使代码更加简洁与可读:

#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {1, 2, 3};
    for (auto number : numbers) {
        std::cout << number << std::endl;
    }
    return 0;
}
模块化与封装

模块化与封装是面向对象编程的核心概念,C++11通过类的定义、构造函数、析构函数和拷贝构造函数进一步强化了这一特性。

使用命名空间管理代码

命名空间有助于避免命名冲突,尤其在大型项目中非常有用:

namespace math {
    int add(int a, int b) {
        return a + b;
    }
}

类和结构体

类与结构体用于封装数据与方法,通过访问控制符(public、protected、private)进行封装:

class Person {
public:
    std::string name;
    int age;

    Person(std::string name, int age) : name(name), age(age) {}
};

int main() {
    Person person("Alice", 30);
    std::cout << person.name << ", " << person.age << std::endl;
    return 0;
}
函数式编程

C++11引入了现代函数式编程概念,例如lambda表达式、闭包和函数适配器:

lambda表达式与闭包

lambda表达式允许在代码中定义匿名函数:

#include <iostream>
#include <functional>

int main() {
    std::function<void(int)> printDouble = [](int n) {
        std::cout << 2 * n << std::endl;
    };
    printDouble(10);
    return 0;
}

函数适配器与闭包

std::bindstd::function提供了一种执行任意函数的统一接口:

#include <iostream>
#include <functional>

int main() {
    std::function<void(int)> printDouble = std::bind([](int x) {
        std::cout << 2 * x << std::endl;
    }, std::placeholders::_1);
    printDouble(10);
    return 0;
}
智能指针与RAII原则

智能指针(如std::unique_ptrstd::shared_ptr)和RAII原则(资源获取即初始化)是C++11中用于管理资源的关键特性:

std::unique_ptrstd::shared_ptr

智能指针能够自动管理生命周期内的资源,确保资源在不再需要时被正确释放:

#include <memory>

class Resource {
public:
    Resource() {
        std::cout << "Resource created" << std::endl;
    }
    ~Resource() {
        std::cout << "Resource destroyed" << std::endl;
    }
};

int main() {
    std::unique_ptr<Resource> uptr(new Resource());
    std::shared_ptr<Resource> sptr(new Resource());
    return 0;
}

RAII原则的实践

RAII原则确保资源在对象构造时获取,在对象析构时释放:

#include <iostream>

class File {
public:
    File() {
        std::cout << "File opened" << std::endl;
    }
    ~File() {
        std::cout << "File closed" << std::endl;
    }
};

int main() {
    File file;
    return 0;
}
并发编程

C++11引入了线程支持和并发算法库,简化了多线程编程:

std::thread的使用

线程库允许创建、管理线程以及同步线程间的通信:

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void printThread(int threadID) {
    std::lock_guard<std::mutex> guard(mtx);
    std::cout << "Thread " << threadID << " started" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Thread " << threadID << " finished" << std::endl;
}

int main() {
    std::thread t1(printThread, 1);
    std::thread t2(printThread, 2);

    t1.join();
    t2.join();

    return 0;
}

std::condition_variable与互斥锁

并发编程中,std::condition_variable与互斥锁(如std::mutex)用于实现线程间的同步:

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool flag = false;

void printCondition() {
    std::unique_lock<std::mutex> lock(mtx);
    cv.wait(lock, [] { return flag; });
    std::cout << "Condition satisfied" << std::endl;
}

int main() {
    std::thread t1(printCondition);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    flag = true;
    cv.notify_all();
    t1.join();

    return 0;
}
案例实践

实施一个简单的任务调度器,使用std::thread和并发算法库:

#include <iostream>
#include <thread>
#include <vector>
#include <functional>
#include <algorithm>
#include <chrono>

void task(std::string name) {
    std::cout << "Task " << name << " started" << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Task " << name << " finished" << std::endl;
}

int main() {
    std::vector<std::thread> threads;

    // 创建多个任务线程
    for (int i = 0; i < 5; ++i) {
        std::string name = "Task " + std::to_string(i);
        threads.emplace_back(task, name);
    }

    // 等待所有线程完成
    for (auto& t : threads) {
        t.join();
    }

    return 0;
}

通过这些实践和示例,您将能够深入了解C++11中一些核心概念,并将其应用于实际的工程实践。随着经验的增长,您将能够解决更复杂的问题,并编写更高效、安全的代码。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

举报

0/150
提交
取消