【C++11】Lazy类的实现

介绍

惰性求值一般用于函数式编程语言中。在使用延迟求值的时候,表达式不在它绑定到变量时就求值,而是等到真正需要的时候再求值。
惰性求值类,目前C++中还没有相应的实现。但是可以借助lamda表达式,将函数封装到lamda中即可。

代码实现

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

template<typename T>
class Lazy
{
public:
    Lazy() {}

    //保存需要延迟执行的函数
    template<typename Func, typename ...Args>
    Lazy(Func& f, Args&& ...args)
    {
        m_function = [&f, &args...]() { return f(std::forward<Args>(args)...); };
        m_function = std::bind(f, std::forward<Args>(args)...);
    }

    //延迟执行,将结果保存起来
    T& value()
    {
        if (!m_isCreate)
        {
            m_result = m_function();
            m_isCreate = true;
        }
        return m_result;
    }

private:
    std::function<T()> m_function;
    T                  m_result;
    bool               m_isCreate = false;
};

//帮助函数 -> 将要执行的函数以及函数的入参保存成Lazy对象
template<class Func, typename ...Args>
Lazy<typename std::result_of<Func(Args...)>::type>
lazy(Func&& f, Args&& ...args)
{
    return Lazy<typename std::result_of<Func(Args...)>::type>(
        std::forward<Func>(f), std::forward<Args>(args)...);
}

struct Object       //big object
{
public:
    Object()
    {
        cout << "big object create" << endl;
    }
};

struct MyStruct     //operation struct
{
    MyStruct()
    {
        m_obj = lazy([] {return std::make_shared<Object>(); });
    }

    void load()
    {
        m_obj.value();
    }

    Lazy<std::shared_ptr<Object>> m_obj;
};

int foo(int x)
{
    return x * 20;
}

int main()
{
    //测试大对象延迟加载
    MyStruct t;
    t.load();

    //测试普通函数加载延迟加载
    auto lazyer1 = lazy(foo, 4);
    cout << lazyer1.value() << endl;

    //测试无参数lamda    
    Lazy<int> lazyer2 = lazy([]() {return 12; });
    cout << lazyer2.value() << endl;

    //测试有参数lamda    
    Lazy<int> lazyer3 = lazy([](int x) {return 10 + x; }, 20);
    cout << lazyer3.value() << endl;

    //测试有参数的function
    std::function<int(int)> f = [](int x) {return x + 3; };
    auto lazyer4 = lazy(f, 4);
    cout << lazyer4.value() << endl;

    system("pause");
    return 0;
}

以上测试结果如下:
这里写图片描述

这个Lazy类可以接受lamda表达式function,实现按需加载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vigenere密码是一种多表密码,它使用多个不同的凯撒密码表。具体来说,对于明文中的每个字母,使用一个不同的凯撒密码表来将其加密。这样,相同的明文在不同位置上会被加密成不同的密文,增强了密码的安全性。 以下是使用C++实现Vigenere密码的示例代码: ```cpp #include <iostream> #include <string> using namespace std; string encrypt(string plaintext, string key) { string ciphertext = ""; int keyLen = key.length(); int i = 0; for (char& c : plaintext) { if (isalpha(c)) { int shift = key[i % keyLen] - 'a'; int base = isupper(c) ? 'A' : 'a'; c = (c - base + shift) % 26 + base; i++; } ciphertext += c; } return ciphertext; } string decrypt(string ciphertext, string key) { string plaintext = ""; int keyLen = key.length(); int i = 0; for (char& c : ciphertext) { if (isalpha(c)) { int shift = key[i % keyLen] - 'a'; int base = isupper(c) ? 'A' : 'a'; c = (c - base - shift + 26) % 26 + base; i++; } plaintext += c; } return plaintext; } int main() { string plaintext = "The quick brown fox jumps over the lazy dog"; string key = "lemon"; string ciphertext = encrypt(plaintext, key); string decrypted = decrypt(ciphertext, key); cout << "Plaintext: " << plaintext << endl; cout << "Key: " << key << endl; cout << "Ciphertext: " << ciphertext << endl; cout << "Decrypted: " << decrypted << endl; return 0; } ``` 在这个示例中,我们定义了两个函数:encrypt和decrypt。encrypt函数将明文和密钥作为输入,并返回加密后的密文。decrypt函数将密文和密钥作为输入,并返回解密后的明文。 在encrypt函数中,我们迭代明文中的每个字符。如果字符是字母,则我们计算出密钥中对应位置的偏移量,并使用凯撒密码的加密公式来加密该字符。注意,我们要将字符转换为大写或小写形式,以便在计算时使用ASCII编码中的正确偏移量。最后,我们将加密后的字符添加到密文中,然后返回整个密文字符串。 在decrypt函数中,我们执行与encrypt函数相反的操作。对于每个密文中的字符,我们计算出密钥中对应位置的偏移量,并使用凯撒密码的解密公式来解密该字符。最后,我们将解密后的字符添加到明文中,然后返回整个明文字符串。 在main函数中,我们定义了一个样例明文和密钥,并使用encrypt函数加密该明文。然后,我们使用decrypt函数解密该密文,并将结果打印到控制台上。 以上是一种简单的实现方法,Vigenere密码还有其他更高级的实现方式,例如使用矩阵代替凯撒密码表。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值