前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c++容器加迭代器和python装饰器的比较

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

作者头像
py3study
发布2020-01-20 14:21:23
7110
发布2020-01-20 14:21:23
举报
文章被收录于专栏:python3python3

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

代码语言:javascript
复制
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 利用装饰器进行实现

代码语言:javascript
复制
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实现的代码更加简洁。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/04/23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档