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实现的代码更加简洁。
c++容器加迭代器和python装饰器的对比的更多相关文章
- Python装饰器、迭代器&生成器、re正则表达式、字符串格式化
Python装饰器.迭代器&生成器.re正则表达式.字符串格式化 本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用 ...
- python装饰器,迭代器,生成器,协程
python装饰器[1] 首先先明白以下两点 #嵌套函数 def out1(): def inner1(): print(1234) inner1()#当没有加入inner时out()不会打印输出12 ...
- python装饰器(docorator)详解
引言: 装饰器是python面向对象编程三大器之一,另外两个迭代器.生成器只是我现在还没有遇到必须使用的场景,等确实需要用到的时候,在补充资料:装饰器在某些场景真的是必要的,比如定义了一个类或者一个函 ...
- Python装饰器详解
python中的装饰器是一个用得非常多的东西,我们可以把一些特定的方法.通用的方法写成一个个装饰器,这就为调用这些方法提供一个非常大的便利,如此提高我们代码的可读性以及简洁性,以及可扩展性. 在学习p ...
- Python装饰器由浅入深
装饰器的功能在很多语言中都有,名字也不尽相同,其实它体现的是一种设计模式,强调的是开放封闭原则,更多的用于后期功能升级而不是编写新的代码.装饰器不光能装饰函数,也能装饰其他的对象,比如类,但通常,我们 ...
- python 装饰器 一篇就能讲清楚
装饰器一直是我们学习python难以理解并且纠结的问题,想要弄明白装饰器,必须理解一下函数式编程概念,并且对python中函数调用语法中的特性有所了解,使用装饰器非常简单,但是写装饰器却很复杂.为了讲 ...
- 转发对python装饰器的理解
[Python] 对 Python 装饰器的理解的一些心得分享出来给大家参考 原文 http://blog.csdn.net/sxw3718401/article/details/3951958 ...
- Python装饰器探险
关于python装饰器的理解和用法,推荐廖雪峰老师和这一篇博客以及知乎 以下代码均已手动敲过,看完本篇内容,包你装饰器小成! 装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就 ...
- python 装饰器、递归原理、模块导入方式
1.装饰器原理 def f1(arg): print '验证' arg() def func(): print ' #.将被调用函数封装到另外一个函数 func = f1(func) #.对原函数重新 ...
随机推荐
- flask 面试题
1,什么是Flask,有什么优点?概念解释Flask是一个Web框架,就是提供一个工具,库和技术来允许你构建一个Web应用程序.这个Web应用程序可以是一些Web页面,博客,wiki,基于Web的日里 ...
- requireJs require.config公共配置
//场景:让require.config配置文件成一个公共文件,每个页面引用这个公共配置 //方式一样例: require.config({ baseUrl: (function () { var p ...
- 【python】声明编码的格式
来自:http://www.xuebuyuan.com/975181.html 编码声明必须在第一行或者第二行,且要符合正则表达式 "coding[:=]\s*([-\w.]+)" ...
- LeetCode(2): 两数相加
本内容为LeetCode第二道题目:两数相加 # -*- coding: utf-8 -*- """ Created on Sun Mar 10 10:47:12 201 ...
- javaSocket笔记
1.查看电脑当前开放链接的端口号 使用netstat命令 netstat -nao 可以查看PID进程号 netstat -nab 获取进程的详细信息 2.类创建的不同对象,引用是不同的.也就是说同一 ...
- 小学生都看得懂的C语言入门(5): 指针
现在已经学到C语言的后面了, 快学完咯.... (一)取地址运算 先来看一下sizeof 计算所占字节 #include<stdio.h> int main() { int a; a=; ...
- Imperial roads 非严格次小生成树
cf测评姬比uva快了五倍... /* 不管这条边是不是在mst上,直接跑lca求出路径上的最大边w即可 ans=mst-w+dist(u,v) */ #include<bits/stdc++. ...
- ES6 Promise 全面总结
转载:点击查看原文 ES6 Promise对象 ES6中,新增了Promise对象,它主要用于处理异步回调代码,让代码不至于陷入回调嵌套的死路中. @-v-@ 1. Promise本质 Promise ...
- MySQL:日期函数、时间函数总结
MySQL 获得当前日期时间 函数 查询昨天,时间拼接 select concat(DATE_FORMAT(date_add(now(), interval -1 day),'%Y-%d-%d'),& ...
- nodejs 环境安装
参考网站 http://www.runoob.com/nodejs/nodejs-http-server.html https://github.com/nodesource/distribution ...