C++核心技术精要:从基础到实战,LeetCode 148.排序链表。

张开发
2026/5/31 14:28:57 15 分钟阅读
C++核心技术精要:从基础到实战,LeetCode 148.排序链表。
C基础语法核心技术详解变量与数据类型C提供丰富的数据类型支持包括基本类型和复合类型。基本类型分为整型int、short、long、浮点型float、double、字符型char和布尔型bool。复合类型包含数组、结构体、联合体和枚举。变量声明需要指定类型和标识符int count 0; double price 9.99; char grade A;运算符与表达式C运算符分为算术运算符、关系运算符、逻辑运算符、位运算符等。特别注意自增/自减运算符的前置与后置区别int a 5; int b a; // b5, a6 int c a; // c7, a7控制结构条件控制包含if-else和switch语句。循环结构包括while、do-while和for循环。现代C推荐使用范围for循环遍历容器std::vectorint vec {1,2,3}; for(auto num : vec) { num * 2; }函数与参数传递函数定义包含返回类型、函数名和参数列表。参数传递方式影响程序行为值传递创建参数副本引用传递直接操作原变量指针传递通过地址访问变量void swap(int a, int b) { int temp a; a b; b temp; }指针与内存管理指针存储内存地址使用解引用运算符*访问数据。动态内存分配通过new和delete实现int* ptr new int(10); delete ptr;智能指针unique_ptr、shared_ptr可自动管理内存生命周期std::unique_ptrint smartPtr std::make_uniqueint(20);面向对象编程类定义包含数据成员和成员函数。构造函数初始化对象析构函数清理资源。访问控制符public、private、protected决定成员可见性。class Rectangle { private: double width, height; public: Rectangle(double w, double h) : width(w), height(h) {} double area() const { return width * height; } };标准模板库(STL)STL提供常用数据结构和算法容器vector、list、map等算法sort、find、transform等迭代器连接容器与算法的桥梁std::vectorstd::string names {Alice, Bob}; std::sort(names.begin(), names.end());异常处理try-catch块处理运行时错误。throw抛出异常catch捕获并处理异常try { if(b 0) throw Division by zero; return a/b; } catch(const char* msg) { std::cerr msg std::endl; }现代C特性C11及后续版本引入重要特性auto类型推导基于范围的for循环移动语义和右值引用lambda表达式constexpr编译期计算auto sum [](auto a, auto b) { return a b; }; std::cout sum(3.5, 4) std::endl; // 输出7.5掌握这些核心技术可构建高效可靠的C应用程序。建议通过实际项目练习巩固理解并持续关注C标准演进。https://github.com/bass-cropper5f/965_adqh/blob/main/README.mdhttps://raw.githubusercontent.com/bass-cropper5f/965_adqh/main/README.mdhttps://github.com/rambles-loams-8e/i6m_wdqthttps://github.com/rambles-loams-8e/i6m_wdqt/blob/main/README.mdhttps://raw.githubusercontent.com/rambles-loams-8e/i6m_wdqt/main/README.md

更多文章