c++容器加迭代器和python装饰器的对比

c++利用对象实现简单数据的测试:

class TestDataEmptyArray {
public:
    static vector<int> get_array() {
        std::vector<int> vec {};
        return vec;
    }

};

class TestDataUniqueValues {
public:
    static vector<int> get_array() {
        // declare a vector with minimum 2 values that are unique
        std::vector<int> vec {1, 2, 3, 4, 5};
        return vec;
    }

    static int get_expected_result() {
        //declare the former vector
        std::vector<int> vec {get_array()};
        //search for the smallest element with algorithm
        std::vector<int>::iterator result = std::min_element(std::begin(vec), std::end(vec));
        //return the index of the smallest element as a type int
        return std::distance(std::begin(vec), result);
    }

};

class TestDataExactlyTwoDifferentMinimums {
public:
    static vector<int> get_array() {
        //declare a vector that has atleast 2 non unique number in it
        std::vector<int> vec {1, 2, 3, 4, 3, 2, 1};
        return vec;
    }

    static int get_expected_result() {
        //declare tha former vector
        std::vector<int> vec {get_array()};
        search for the smallest element with algorithm
        std::vector<int>::iterator result = std::min_element(std::begin(vec), std::end(vec));
        //return the index of the smallest element as a type int
        return std::distance(std::begin(vec), result);
    }

};

python 利用装饰器进行实现

class TestDataEmptyArray(object):
    
    @staticmethod
    def get_array():
        return []

class TestDataUniqueValues(object):

    @staticmethod
    def get_array():
        arr = [2,3,4,6,5]
        return arr

    @staticmethod
    def get_expected_result():
        return 0

class TestDataExactlyTwoDifferentMinimums(object):

    @staticmethod
    def get_array():
        arr = [2,3,4,2,4]
        return arr

    @staticmethod
    def get_expected_result():
        return 0

相比较来说,python实现的代码更加简洁。

转载于:https://www.cnblogs.com/kk328/p/10138505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值