C++函数知识点大全(二)

简介: 教程来源 https://xcfsr.cn/category/open-source.html 本文详解C++核心函数特性:内联函数(`inline`)提升性能、`constexpr`实现编译期计算;函数模板支持泛型编程与特化;可变参数模板与折叠表达式简化多参处理;Lambda表达式提供灵活匿名函数,涵盖捕获机制、泛型参数(`auto`)及`constexpr`支持。内容覆盖C++11至C++17关键特性。

四、内联函数

4.1 inline关键字

cpp
#include <iostream>
using namespace std;

// inline建议编译器将函数代码插入调用处
// 适用于短小、频繁调用的函数

// 内联函数
inline int max(int a, int b) {
    return (a > b) ? a : b;
}

// 类内部定义的函数默认为内联
class Point {
private:
    int x, y;
public:
    // 类内定义,默认为内联
    Point(int x, int y) : x(x), y(y) {}

    // 显式内联
    inline int getX() const { return x; }

    // 类外定义的内联函数
    inline int getY() const;
};

int Point::getY() const {
    return y;
}

// 内联函数通常放在头文件中
// inline.h
inline int square(int x) {
    return x * x;
}

int main() {
    cout << "max(10, 20) = " << max(10, 20) << endl;
    cout << "square(5) = " << square(5) << endl;

    Point p(3, 4);
    cout << "Point: (" << p.getX() << ", " << p.getY() << ")" << endl;

    return 0;
}

4.2 内联与宏

cpp
#include <iostream>
using namespace std;

// 宏定义(预处理阶段)
#define MAX_MACRO(a, b) ((a) > (b) ? (a) : (b))
#define SQUARE_MACRO(x) ((x) * (x))

// 内联函数(编译阶段)
inline int maxInline(int a, int b) {
    return (a > b) ? a : b;
}

inline int squareInline(int x) {
    return x * x;
}

int main() {
    int a = 5, b = 10;

    // 宏的问题:参数多次求值
    int x = 5;
    cout << "SQUARE_MACRO(x++): " << SQUARE_MACRO(x++) << endl;  // x++执行两次
    cout << "x = " << x << endl;

    x = 5;
    cout << "squareInline(x++): " << squareInline(x++) << endl;  // x++执行一次
    cout << "x = " << x << endl;

    // 类型安全
    cout << "maxInline(10, 20): " << maxInline(10, 20) << endl;
    cout << "maxInline(3.14, 2.71): " << maxInline(3.14, 2.71) << endl;

    return 0;
}

4.3 constexpr函数(C++11)

cpp
#include <iostream>
using namespace std;

// constexpr函数:可在编译期求值
constexpr int factorial(int n) {
    return (n <= 1) ? 1 : n * factorial(n - 1);
}

constexpr int square(int x) {
    return x * x;
}

// constexpr可用于模板
template<typename T>
constexpr T sum(T a, T b) {
    return a + b;
}

// C++14中constexpr函数可以包含循环
constexpr int fibonacci(int n) {
    if (n <= 1) return n;
    int a = 0, b = 1;
    for (int i = 2; i <= n; i++) {
        int c = a + b;
        a = b;
        b = c;
    }
    return b;
}

int main() {
    // 编译期计算
    constexpr int fact5 = factorial(5);
    constexpr int arrSize = square(10);

    int arr[arrSize];  // 编译期常量数组大小
    cout << "fact5 = " << fact5 << endl;
    cout << "arrSize = " << arrSize << endl;

    // 运行时计算
    int n = 6;
    int factN = factorial(n);  // 运行时计算

    cout << "fib(10) = " << fibonacci(10) << endl;
    cout << "sum(3.14, 2.71) = " << sum(3.14, 2.71) << endl;

    return 0;
}

五、函数模板

5.1 模板函数基础

cpp
#include <iostream>
#include <string>
using namespace std;

// 函数模板:泛型编程的基础
template<typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

// 多个模板参数
template<typename T, typename U>
auto add(T a, U b) -> decltype(a + b) {
    return a + b;
}

// C++14简化版
template<typename T, typename U>
auto multiply(T a, U b) {
    return a * b;
}

// 模板特化
template<>
const char* max<const char*>(const char* a, const char* b) {
    return (strcmp(a, b) > 0) ? a : b;
}

int main() {
    // 隐式实例化
    cout << "max(10, 20) = " << max(10, 20) << endl;
    cout << "max(3.14, 2.71) = " << max(3.14, 2.71) << endl;
    cout << "max('A', 'Z') = " << max('A', 'Z') << endl;

    // 显式实例化
    cout << "max<int>(10, 20) = " << max<int>(10, 20) << endl;

    // 不同类型参数
    cout << "add(10, 3.14) = " << add(10, 3.14) << endl;
    cout << "multiply(5, 2.5) = " << multiply(5, 2.5) << endl;

    // 字符串特化
    cout << "max(\"apple\", \"banana\") = " << max("apple", "banana") << endl;

    return 0;
}

5.2 模板参数推导

cpp
#include <iostream>
#include <vector>
using namespace std;

// 模板参数推导规则
template<typename T>
void func(T param) {
    cout << "T: " << typeid(T).name() << endl;
}

template<typename T>
void funcRef(T& param) {
    cout << "T&: " << typeid(T).name() << endl;
}

template<typename T>
void funcPtr(T* param) {
    cout << "T*: " << typeid(T).name() << endl;
}

// 通用引用(转发引用)
template<typename T>
void funcForward(T&& param) {
    cout << "T&&: " << typeid(T).name() << endl;
}

int main() {
    int x = 10;
    const int cx = 20;
    int& rx = x;

    func(x);      // T = int
    func(cx);     // T = int (const被忽略)
    func(rx);     // T = int

    funcRef(x);   // T = int
    funcRef(cx);  // T = const int (const保留)
    // funcRef(rx); // T = int

    funcPtr(&x);  // T = int

    funcForward(x);   // T = int&
    funcForward(10);  // T = int

    return 0;
}

5.3 可变参数模板(C++11)

cpp
#include <iostream>
using namespace std;

// 递归终止函数
void print() {
    cout << endl;
}

// 可变参数模板
template<typename T, typename... Args>
void print(T first, Args... args) {
    cout << first << " ";
    print(args...);  // 递归调用
}

// 折叠表达式(C++17)
template<typename... Args>
auto sum(Args... args) {
    return (args + ...);  // 右折叠
}

template<typename... Args>
auto sum2(Args... args) {
    return (... + args);  // 左折叠
}

// 带初始值的折叠
template<typename... Args>
auto sumWithInit(Args... args) {
    return (0 + ... + args);  // 带初始值的右折叠
}

// 使用折叠表达式打印
template<typename... Args>
void printAll(Args... args) {
    (cout << ... << args) << endl;  // 折叠表达式
}

int main() {
    print(1, 2, 3, 4, 5);
    print("Hello", "World", 2024);

    cout << "sum(1,2,3,4,5) = " << sum(1, 2, 3, 4, 5) << endl;
    cout << "sumWithInit(1,2,3) = " << sumWithInit(1, 2, 3) << endl;

    printAll("A", "B", "C", 123);

    return 0;
}

六、Lambda表达式

6.1 Lambda基础

cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Lambda表达式:匿名函数对象

int main() {
    // 基本语法:[捕获列表](参数列表) -> 返回类型 { 函数体 }

    // 1. 最简单的Lambda
    [] { cout << "Hello Lambda!" << endl; }();

    // 2. 带参数的Lambda
    auto add = [](int a, int b) { return a + b; };
    cout << "add(10, 20) = " << add(10, 20) << endl;

    // 3. 指定返回类型
    auto divide = [](double a, double b) -> double {
        if (b == 0) return 0;
        return a / b;
    };
    cout << "divide(10, 3) = " << divide(10, 3) << endl;

    // 4. 捕获外部变量
    int x = 10, y = 20;

    // 值捕获
    auto byValue = [=] { return x + y; };
    cout << "byValue = " << byValue() << endl;

    // 引用捕获
    auto byRef = [&] { x = 100; y = 200; };
    byRef();
    cout << "x=" << x << ", y=" << y << endl;

    // 混合捕获
    int a = 1, b = 2, c = 3;
    auto mixed = [=, &b] { 
        // a, c是值捕获,b是引用捕获
        b = a + c;
        return b;
    };
    cout << "mixed = " << mixed() << endl;

    return 0;
}

6.2 捕获方式详解

cpp
#include <iostream>
#include <memory>
using namespace std;

class Widget {
public:
    int value = 100;
    void method() {
        // 捕获this(C++11方式)
        auto lambda1 = [this] { 
            cout << "value = " << value << endl; 
        };
        lambda1();

        // 捕获*this(C++17拷贝)
        auto lambda2 = [*this] { 
            cout << "value = " << value << endl; 
        };
        lambda2();
    }
};

int main() {
    // 捕获方式总结
    int x = 10, y = 20, z = 30;

    // [=] 值捕获所有
    auto lambda1 = [=] { return x + y; };

    // [&] 引用捕获所有
    auto lambda2 = [&] { x = y = 100; };

    // [=, &y] 值捕获除y外,y引用捕获
    auto lambda3 = [=, &y] { 
        // x, z是值捕获,y是引用捕获
        y = x + z;
    };

    // [&, y] 引用捕获除y外,y值捕获
    auto lambda4 = [&, y] { 
        // x, z是引用捕获,y是值捕获
        x = y + z;
    };

    // [this] 捕获this指针
    Widget w;
    w.method();

    // 移动捕获(C++14)
    unique_ptr<int> ptr = make_unique<int>(42);
    auto lambda = [ptr = move(ptr)] { 
        cout << "value = " << *ptr << endl; 
    };
    lambda();

    return 0;
}

6.3 泛型Lambda(C++14)

cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    // 泛型Lambda:参数使用auto
    auto add = [](auto a, auto b) { return a + b; };

    cout << "add(10, 20) = " << add(10, 20) << endl;
    cout << "add(3.14, 2.71) = " << add(3.14, 2.71) << endl;
    cout << "add(string, string) = " << add(string("Hello "), string("World")) << endl;

    // 使用Lambda与STL
    vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // 过滤偶数
    vector<int> evens;
    copy_if(numbers.begin(), numbers.end(), back_inserter(evens),
            [](auto n) { return n % 2 == 0; });

    // 映射到平方
    vector<int> squares;
    transform(numbers.begin(), numbers.end(), back_inserter(squares),
              [](auto n) { return n * n; });

    // 累加
    int sum = 0;
    for_each(numbers.begin(), numbers.end(), 
             [&sum](auto n) { sum += n; });

    // 使用constexpr Lambda(C++17)
    constexpr auto square = [](int n) constexpr { return n * n; };
    constexpr int result = square(5);

    cout << "result = " << result << endl;

    return 0;
}

来源:
https://xcfsr.cn/category/tech-news.html

相关文章
|
13天前
|
前端开发 JavaScript
JavaScript学习知识点大全(二)
教程来源 https://app-a87ujc988w01.appmiaoda.com/ 本节系统讲解JavaScript核心数据结构与异步编程:涵盖数组(创建、遍历map/filter/reduce、增删改查及高级方法)、Set/Map集合操作与WeakSet/WeakMap弱引用特性;字符串常用方法与正则表达式(匹配、提取、替换及高级语法);以及回调、Promise、async/await异步处理与事件循环机制。
|
13天前
|
存储 JavaScript 前端开发
JavaScript学习知识点大全(三)
教程来源 https://app-aakcgtuh1ywx.appmiaoda.com 本文系统讲解前端核心 DOM/BOM 操作与错误处理:涵盖元素选择、增删改查、属性/样式控制、事件绑定与委托;BOM 中 window、navigator、location、history 及本地存储;以及 try-catch、错误类型识别、全局异常捕获等健壮性实践,助力高效开发。
|
13天前
|
JavaScript 前端开发 开发者
JavaScript学习知识点大全(四)
教程来源 https://app-aaqhxe29haf5.appmiaoda.com 讲解JavaScript模块化(ES6/ CommonJS)、ES6+核心特性(解构、展开符、模板字符串、可选链、BigInt等)及性能优化技巧(内存管理、代码与网络优化),涵盖最佳实践与实用示例,助力开发者构建高效、可维护的现代Web应用。
|
13天前
|
存储 JavaScript 前端开发
JavaScript学习知识点大全(一)
教程来源 https://app-a7illrp9pngh.appmiaoda.com/ 本文系统梳理JavaScript核心知识:从基础语法(变量、数据类型、运算符、流程控制)到函数进阶(作用域、闭包、高阶函数)、面向对象(原型链、Class、继承)等,兼顾ES6+新特性。适合初学者构建完整知识体系,也便于开发者查漏补缺。
|
12天前
|
缓存 前端开发 网络协议
使用 CDN 给网站加速的保姆级教程:从卡顿到飞一般的体验
CDN 内容分发网络配置全攻略,从开通到生效不到 1 小时,就能让用户体验从 “卡顿” 变 “飞一般流畅”。
558 3
|
4天前
|
C++
C++函数知识点大全(三)
教程来源 http://htnus.cn/category/software-apps.html 本节详解C++函数对象(仿函数)、std::function、成员函数及友元机制:涵盖自定义函数对象(带状态/谓词/比较)、标准库预定义函数对象(plus、greater等)、std::function统一包装函数/lambda/对象;深入成员函数const属性、静态成员、指针调用;以及友元函数与友元类突破封装限制的用法。
|
4天前
|
C语言 C++ 开发者
C++函数知识点大全(一)
教程来源 https://xcfsr.cn/category/cloud.html 本文系统梳理C++函数核心知识:从基础定义、返回值(含引用/指针/移动语义)、重载规则、默认参数,到现代特性如Lambda、模板函数等,兼顾初学者体系构建与开发者深度参考。
|
24天前
|
SQL 安全 关系型数据库
Mysql指南大全(新手也能轻松掌握的Mysql教程)第四卷
教程来源:https://app-a6nw7st4g741.appmiaoda.com/ 本文系统讲解MySQL核心知识:第八章详解事务ACID特性与操作(START TRANSACTION/COMMIT/ROLLBACK);第九章涵盖用户管理、权限控制及备份恢复;第十章通过订单系统实战,演示建库建表、关联设计与复杂查询。附SQL速查表,助力快速上手。
|
6天前
|
JSON 前端开发 API
用Pydantic实现Python数据校验的最佳实践
本文以小张调试用户注册报错为引,生动揭示Python后端数据校验混乱的痛点:规则散落、类型错误频发、业务逻辑被校验淹没。随即引出Pydantic解法——通过声明式模型(如`class User(BaseModel): username: str; age: int = Field(ge=18)`),实现自动类型转换、嵌套校验、字段约束与清晰错误提示,大幅提升代码可读性、健壮性与可维护性。(239字)
72 0
|
5天前
|
安全 C语言
C语言函数知识点大全(二)
教程来源 https://www.xcfsr.cn/category/open-source.html 本文系统讲解C语言中递归函数、函数指针与内联函数三大核心概念:递归涵盖阶乘、斐波那契、汉诺塔等经典案例及递归/迭代效率对比;函数指针详解声明、传参、返回值及在qsort中的实际应用;内联函数则对比宏定义,突出其类型安全与副作用更少的优势。

热门文章

最新文章