Bootstrap

稀奇古怪的 C++ 代码

在c++中有各种各样的稀奇代码,今天我就带大家看十五个例子~

1. 使用宏定义创建一个简单的调试工具

#include <iostream>  
#define DEBUG(x) std::cout << "Debug: " << x << std::endl;  

int main() {  
    int a = 5;  
    DEBUG(a); // 输出: Debug: 5  
    return 0;  
}

2. 使用模板元编程计算阶乘

#include <iostream>  

template<int N>  
struct Factorial {  
    static const int value = N * Factorial<N - 1>::value;  
};  

template<>  
struct Factorial<0> {  
    static const int value = 1;  
};  

int main() {  
    std::cout << "Factorial of 5: " << Factorial<5>::value << std::endl; // 输出: 120  
    return 0;  
}

3. 使用 decltype 和 auto 进行类型推导

#include <iostream>  

int main() {  
    auto x = 5; // x 的类型是 int  
    decltype(x) y = 10; // y 的类型也是 int  
    std::cout << "x: " << x << ", y: " << y << std::endl; // 输出: x: 5, y: 10  
    return 0;  
}

4. 使用运算符重载创建一个简单的复数类

#include <iostream>  

class Complex {  
public:  
    double real, imag;  

    Complex(double r, double i) : real(r), imag(i) {}  

    Complex operator+(const Complex& other) {  
        return Complex(real + other.real, imag + other.imag);  
    }  

    void display() {  
        std::cout << real << " + " << imag << "i" << std::endl;  
    }  
};  

int main() {  
    Complex c1(1.0, 2.0);  
    Complex c2(3.0, 4.0);  
    Complex c3 = c1 + c2;  
    c3.display(); // 输出: 4 + 6i  
    return 0;  
}

5. 使用 lambda 表达式和 std::function

#include <iostream>  
#include <functional>  

int main() {  
    std::function<int(int, int)> add = [](int a, int b) { return a + b; };  
    std::cout << "Sum: " << add(3, 4) << std::endl; // 输出: Sum: 7  
    return 0;  
}

6. 通过递归实现斐波那契数列

#include <iostream>  

int fibonacci(int n) {  
    if (n <= 1) return n;  
    return fibonacci(n - 1) + fibonacci(n - 2);  
}  

int main() {  
    std::cout << "Fibonacci of 5: " << fibonacci(5) << std::endl; // 输出: 5  
    return 0;  
}

7. 使用 std::initializer_list 实现一个简单的集合类

#include <iostream>  
#include <initializer_list>  
#include <vector>  

class SimpleSet {  
public:  
    SimpleSet(std::initializer_list<int> elements) {  
        for (int element : elements) {  
            if (std::find(data.begin(), data.end(), element) == data.end()) {  
                data.push_back(element);  
            }  
        }  
    }  

    void display() const {  
        for (int element : data) {  
            std::cout << element << " ";  
        }  
        std::cout << std::endl;  
    }  

private:  
    std::vector<int> data;  
};  

int main() {  
    SimpleSet s = {1, 2, 3, 2, 1}; // 重复元素会被忽略  
    s.display(); // 输出: 1 2 3  
    return 0;  
}

8. 使用 std::tuple 和 std::get 访问不同类型的元素

#include <iostream>  
#include <tuple>  

int main() {  
    std::tuple<int, double, std::string> myTuple(1, 3.14, "Hello");  
    
    std::cout << "Integer: " << std::get<0>(myTuple) << std::endl; // 输出: Integer: 1  
    std::cout << "Double: " << std::get<1>(myTuple) << std::endl; // 输出: Double: 3.14  
    std::cout << "String: " << std::get<2>(myTuple) << std::endl; // 输出: String: Hello  

    return 0;  
}

9. 使用 std::variant 实现一个简单的联合体

#include <iostream>  
#include <variant>  

int main() {  
    std::variant<int, double, std::string> var;  

    var = 42; // 存储 int  
    std::cout << "Integer: " << std::get<int>(var) << std::endl; // 输出: Integer: 42  

    var = 3.14; // 存储 double  
    std::cout << "Double: " << std::get<double>(var) << std::endl; // 输出: Double: 3.14  

    var = "Hello, Variant!"; // 存储 string  
    std::cout << "String: " << std::get<std::string>(var) << std::endl; // 输出: String: Hello, Variant!  

    return 0;  
}

10. 使用 constexpr 进行编译时计算

#include <iostream>  

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

int main() {  
    constexpr int result = square(5);  
    std::cout << "Square of 5: " << result << std::endl; // 输出: Square of 5: 25  
    return 0;  
}

11. 使用 std::unique_ptr 实现智能指针

#include <iostream>  
#include <memory>  

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

int main() {  
    std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>();  
    // MyClass 对象会在 ptr 超出作用域时自动销毁  
    return 0;  
}

12. 使用 std::thread 创建多线程程序

#include <iostream>  
#include <thread>  

void printHello() {  
    std::cout << "Hello from thread!" << std::endl;  
}  

int main() {  
    std::thread t(printHello);  
    t.join(); // 等待线程完成  
    return 0;  
}

13. 使用 std::map 实现简单的字典

#include <iostream>  
#include <map>  

int main() {  
    std::map<std::string, int> dictionary;  
    dictionary["apple"] = 1;  
    dictionary["banana"] = 2;  

    for (const auto& pair : dictionary) {  
        std::cout << pair.first << ": " << pair.second << std::endl;  
    }  
    return 0;  
}

14. 使用 std::function 和 std::bind 绑定参数

#include <iostream>  
#include <functional>  

void multiply(int a, int b) {  
    std::cout << "Result: " << a * b << std::endl;  
}  

int main() {  
    auto boundFunc = std::bind(multiply, 5, std::placeholders::_1);  
    boundFunc(10); // 输出: Result: 50  
    return 0;  
}

15. 使用 std::array 实现固定大小的数组

#include <iostream>  
#include <array>  

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

    for (const auto& element : arr) {  
        std::cout << element << " ";  
    }  
    std::cout << std::endl; // 输出: 1 2 3 4 5  
    return 0;  
}

;