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. Swift Write to file 到电脑桌面

    (arr as NSArray).write(toFile: "Users/你的用户名/Desktop/mian.plist", atomically: true)

  2. input,select默认颜色修改

    input::-webkit-input-placeholder{color: #7f7f7f;} select{color: #7f7f7f} option{color: #7f7f7f;}

  3. SS-QT5

    https://blog.csdn.net/sos218909/article/details/78781017

  4. python 与mongodb 交互

    创建管理员 1 > use admin 2 switched to db admin 3 > db 4 admin 5 > db.createUser({user:'admin',p ...

  5. rpm命令用法小结

    rpm 是用来管理 Redhat系列的包管理工具: 通过将打包编译好的程序包文件放置在各自的位置上,就完成了安装: rpm   [OPTIONS]   PACHAGE_FILE 1 安装:: -i : ...

  6. js----常用功能

    切割 1. a="ddd" a.substr(0,1) 通过js找子代 document.getElementByClass("ddd").getElement ...

  7. time与datetime模块

    在python中,通常用下面几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 格式化的时间字符串(format s ...

  8. mysql 去除重复 Select中DISTINCT关键字的用法 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是 distinct只能返回它的目标字段,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,

      在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记 ...

  9. C++ 内链接 外链接

    编译的时候(假如编译器是VS),是以源文件cpp文件为单位,编译成一个个的obj文件,然后再通过链接器把不同的obj文件链接起来.如果一些变量或函数的定义是内连接的话,链接器链接的时候就不会拿它们去与 ...

  10. 插件使用一进度条---nprogress

    nprogress 是像youtube一样在顶部出现进度条,用在一些加载比较缓慢的场景中. 官方网站是 http://ricostacruz.com/nprogress/ 源码在 https://gi ...