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实现的代码更加简洁。

c++容器加迭代器和python装饰器的对比的更多相关文章

  1. Python装饰器、迭代器&生成器、re正则表达式、字符串格式化

    Python装饰器.迭代器&生成器.re正则表达式.字符串格式化 本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用 ...

  2. python装饰器,迭代器,生成器,协程

    python装饰器[1] 首先先明白以下两点 #嵌套函数 def out1(): def inner1(): print(1234) inner1()#当没有加入inner时out()不会打印输出12 ...

  3. python装饰器(docorator)详解

    引言: 装饰器是python面向对象编程三大器之一,另外两个迭代器.生成器只是我现在还没有遇到必须使用的场景,等确实需要用到的时候,在补充资料:装饰器在某些场景真的是必要的,比如定义了一个类或者一个函 ...

  4. Python装饰器详解

    python中的装饰器是一个用得非常多的东西,我们可以把一些特定的方法.通用的方法写成一个个装饰器,这就为调用这些方法提供一个非常大的便利,如此提高我们代码的可读性以及简洁性,以及可扩展性. 在学习p ...

  5. Python装饰器由浅入深

    装饰器的功能在很多语言中都有,名字也不尽相同,其实它体现的是一种设计模式,强调的是开放封闭原则,更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也能装饰其他的对象,比如类,但通常,我们 ...

  6. python 装饰器 一篇就能讲清楚

    装饰器一直是我们学习python难以理解并且纠结的问题,想要弄明白装饰器,必须理解一下函数式编程概念,并且对python中函数调用语法中的特性有所了解,使用装饰器非常简单,但是写装饰器却很复杂.为了讲 ...

  7. 转发对python装饰器的理解

    [Python] 对 Python 装饰器的理解的一些心得分享出来给大家参考   原文  http://blog.csdn.net/sxw3718401/article/details/3951958 ...

  8. Python装饰器探险

    关于python装饰器的理解和用法,推荐廖雪峰老师和这一篇博客以及知乎 以下代码均已手动敲过,看完本篇内容,包你装饰器小成! 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就 ...

  9. python 装饰器、递归原理、模块导入方式

    1.装饰器原理 def f1(arg): print '验证' arg() def func(): print ' #.将被调用函数封装到另外一个函数 func = f1(func) #.对原函数重新 ...

随机推荐

  1. mouseover、mouseout与mouseenter、mouseleave

    待定 附加链接: http://www.aijquery.cn/Html/jqueryrumen/129.html

  2. babel-cli 的使用

    1.安装babel-cli npm i babel-cli -D 2.实现npm的初始化 npm init -y 3.配置package.json { "name": " ...

  3. Decimal integer conversion

    问题 : Decimal integer conversion 时间限制: 1 Sec  内存限制: 128 MB 题目描述 XiaoMing likes mathematics, and he is ...

  4. Pymysql-总结

    背景:工作需要大量链接数据库进行一些操作查询,但是也会有出现异常情况 1.添加字段 1 ALTER TABLE app01_student ADD COLUMN Relation VARCHAR(25 ...

  5. Mac Mojave(10.14.1)执行Matlab的mex报错

    先装了matlab2018b,发现很频繁的crash,同时考虑到要跑的代码在>=2017a时就计算错误,于是转战matlab2016b matlab2016b安装后,执行mex -setup报错 ...

  6. 编写UEditor插件

    UE.registerUI('beijing', function (editor, uiName) { // 注册按钮执行时的command命令 editor.registerCommand(uiN ...

  7. 在WPF中获取DATAGRIDTEMPLATECOLUMN模板定义的内容控件(转载)

    原文:http://www.cnblogs.com/eric_ibm/p/3772516.html xaml格式描述: <DataGrid Name="dataGrid" G ...

  8. CentOS6.9安装Filebeat监控Nginx的访问日志发送到Kafka

    一.下载地址: 官方:https://www.elastic.co/cn/downloads/beats/filebeat 百度云盘:https://pan.baidu.com/s/1dvhqb0 二 ...

  9. 去除ArrayList集合中的重复自定义对象元素

    要求去除ArrayList集合中重复的Student的对象(什么叫重复,所有属性值都相同叫做重复). 思路: 1.创建一个新集合 2.遍历旧集合中的每一个元素,去新集合中找这个元素,如果这个元素不存在 ...

  10. error C1128: 节数超过对象文件格式限制: 请使用 /bigobj 进行编译

    VS2015出现如上错误. 默认情况下,对象文件最多可存放 65,536 (2^16) 个可寻址的节. 这种情况不管指定哪个目标平台. /bigobj 可将该地址容量增加至 4,294,967,296 ...