python中的range函数表示一个连续的有序序列,range使用起来很方便,因为在定义时就隐含了初始化过程,因为只需要给begin()和end()或者仅仅一个end(),就能表示一个连续的序列。还可以指定序列产生的步长,如range(0,10,8)产生的序列为[0, 8], 默认的步长为1,range(3)表示的序列是[0,1,2]。range的遍历也很方便:

for i in range():
print i

  

  c++11中增加了一项新特性range-based for循环,其实这也不是什么新东西,在c#、java和python等语言中已经有了。这种循环方式非常简洁,它的内部其实是对传统的begin()/end()方式的遍历做了包装,算是一个循环的语法糖。用法很简单:

//遍历vector
std::vector<int> v;
for(auto i : v)
{
cout<<i<<endl;
} //以只读方式遍历map
std::map<string, int> map;
for(const auto& item : map)
{
cout << item->first<<item->second<<endl;
}

  c++11的range-based for循环有意思的地方是他可以支持自定义类型的遍历,但是要求自定义类型满足三个条件:

  1. 要实现begin()和end(),他们分别用来返回第一个和最后一个元素的迭代器;
  2. 提供迭代终止的方法;
  3. 提供遍历range的方法;

满足这三个条件之后,我们自定义的类型就能支持range-based for循环了。

  再回到刚才提到的python的range(),它很好用,但是c++中目前还没有类似的东西,虽然标准库中有很多容器如vector、list、queue、map、初始化列表和array等等都已经支持了range-based for循环,但是他们使用起来还是不够方便,比如要生成一个有序序列时,需要专门去初始化,如果有一个类似于python range的东西就比较完美了。虽然c++11现在没有,但我们可以自己用c++11去实现一个类似的range,而且我还想让这个range比python的range更强大,让它不仅仅能支持整数还能支持浮点数,同时还能双向迭代,实现这个range还是比较简单的,看看具体实现吧:

namespace Cosmos
{
template<typename value_t>
class RangeImpl
{
class Iterator;
public:
RangeImpl(value_t begin, value_t end, value_t step = ) :m_begin(begin), m_end(end), m_step(step)
{
if (step>&&m_begin >= m_end)
throw std::logic_error("end must greater than begin.");
else if (step< && m_begin <= m_end)
throw std::logic_error("end must less than begin."); m_step_end = (m_end - m_begin) / m_step;
if (m_begin + m_step_end*m_step != m_end)
{
m_step_end++;
}
} Iterator begin()
{
return Iterator(, *this);
} Iterator end()
{
return Iterator(m_step_end, *this);
} value_t operator[](int s)
{
return m_begin + s*m_step;
} int size()
{
return m_step_end;
} private:
value_t m_begin;
value_t m_end;
value_t m_step;
int m_step_end; class Iterator
{
public:
Iterator(int start, RangeImpl& range) : m_current_step(start), m_range(range)
{
m_current_value = m_range.m_begin + m_current_step*m_range.m_step;
} value_t operator*() { return m_current_value; } const Iterator* operator++()
{
m_current_value += m_range.m_step;
m_current_step++;
return this;
} bool operator==(const Iterator& other)
{
return m_current_step == other.m_current_step;
} bool operator!=(const Iterator& other)
{
return m_current_step != other.m_current_step;
} const Iterator* operator--()
{
m_current_value -= m_range.m_step;
m_current_step--;
return this;
} private:
value_t m_current_value;
int m_current_step;
RangeImpl& m_range;
};
}; template<typename T, typename V>
auto Range(T begin, T end, V stepsize)->RangeImpl<decltype(begin + end + stepsize)>
{
return RangeImpl<decltype(begin + end + stepsize)>(begin, end, stepsize);
} template<typename T>
RangeImpl<T> Range(T begin, T end)
{
return RangeImpl<T>(begin, end, );
} template<typename T>
RangeImpl<T> Range(T end)
{
return RangeImpl<T>(T(), end, );
}
}

再看看测试代码:

void TestRange()
{
cout << "Range(15):";
for (int i : Range()){
cout << " " << i;
} cout << endl;
cout << "Range(2,6):";
for (int i : Range(, )){
cout << " " << i;
}
cout << endl;
cout << "Range(10.5, 15.5):";
for (float i : Range(10.5, 15.5)){
cout << " " << i;
}
cout << endl;
cout << "Range(35,27,-1):";
for (int i : Range(, , -)){
cout << " " << i;
}
cout << endl;
cout << "Range(2,8,0.5):";
for (float i : Range(, , 0.5)){
cout << " " << i;
}
cout << endl;
cout << "Range(8,7,-0.1):";
for (auto i : Range(, , -0.1)){
cout << " " << i;
}
cout << endl; cout << "Range('a', 'z'):";
for (auto i : Range('a', 'z'))
{
cout << " " << i;
}
cout << endl;
}

  测试结果:

  可以看到这个range不仅仅会根据步长生成有序序列,还能支持浮点类型和char类型以及双向迭代,比python的range更强大。

  如果你觉得这篇文章对你有用,可以点一下推荐,谢谢。

  c++11 boost技术交流群:296561497,欢迎大家来交流技术。

(原创)用c++11打造类似于python的range的更多相关文章

  1. 用c++11打造类似于python的range

    python中的range函数表示一个连续的有序序列,range使用起来很方便,因为在定义时就隐含了初始化过程,因为只需要给begin()和end()或者仅仅一个end(),就能表示一个连续的序列.还 ...

  2. A Neural Network in 11 lines of Python

    A Neural Network in 11 lines of Python A bare bones neural network implementation to describe the in ...

  3. 老司机带你用vagrant打造一站式python开发测试环境

      前言 作为一个学习和使用Python的老司机,好像应该经常总结一点东西的,让新司机尽快上路,少走弯路,然后大家一起愉快的玩耍. 今天,咱们就使用vagrant配合xshell打造一站式Python ...

  4. cplusplus 库 在线管理; 类似于 python的 pip install 、nodejs 的npm模块

    cplusplus 库 在线管理: 类似于 python的 pip install .nodejs 的npm模块 还有 apache 经常使用的 Apache Ivy 项目依赖管理工具/Maven 这 ...

  5. # Pycharm打造高效Python IDE

    Pycharm打造高效Python IDE 建议以scientific mode运行,在科学计算时,可以方便追踪变量变化,并且会提示函数的用法,比普通模式下的提示更加智能,一般在文件中引入了numpy ...

  6. 第11.6节 Python正则表达式的字符串开头匹配模式及元字符“^”(插入符、脱字符)功能介绍

    符号"^"为插入符,也称为脱字符,在Python中脱字符表示匹配字符串的开头,即字符串的开头满足匹配模式的要求.这个功能有点类似搜索函数match,只是这是通过搜索模式来指定,而m ...

  7. 第11.5节 Python正则表达式搜索任意字符匹配及元字符“.”(点)功能介绍

    在re模块中,任意字符匹配使用"."(点)来表示, 在默认模式下,点匹配除了换行的任意字符.如果指定了搜索标记re.DOTALL ,它将匹配包括换行符的任意字符.关于搜索标记的含义 ...

  8. 第11.13节 Python正则表达式的转义符”\”功能介绍

    为了支持特殊元字符在特定场景下能表示自身而不会被当成元字符进行匹配出来,可以通过字符集或转义符表示方法来表示,字符集表示方法前面在<第11.4节 Python正则表达式搜索字符集匹配功能及元字符 ...

  9. 第11.7节 Python正则表达式的字符串结尾匹配模式及元字符“$”功能介绍

    符号"$"表示匹配字符串的结尾,即字符串的结尾满足匹配模式的要求. 在 MULTILINE 模式(搜索标记中包含re.MULTILINE,关于搜索标记的含义请见<第11.2节 ...

随机推荐

  1. Vista/Win7以上系统查看和清除本地DNS缓存新方法

    你是否因修改网站DNS解析后,却因本机DNS缓存而需要等待... 你是否遇到修改了本机的hosts文件后,必须重起firefox和ie才起作用... 其实只要清空DNS缓存这些问题都可以解决. 查看D ...

  2. java读取txt字符串挨个写入int数组

    int []num=new int[1001]; FileReader fr = new FileReader("1.txt"); BufferedReader br = new ...

  3. Swift3 JSON字符串和字典互转(JSON字符串转字典和字典转JSON字符串)

    直接上代码吧 1.JSONString转换为字典 /// JSONString转换为字典 /// /// - Parameter jsonString: <#jsonString descrip ...

  4. 【虚拟化系列】VMware vSphere 5.1 简介与安装

    一. VMware vSphere 5.1简介           vSphere是VMware推出的基于云计算的新一代数据中心虚拟化套件,提供了虚拟化基础架构.高可用性.集中管理.监控等一整套解决方 ...

  5. Socket编程:之TCP案例

    转载请加上博文引用:http://i.cnblogs.com/EditPosts.aspx?postid=5733248&update=1 服务端: #include <stdio.h& ...

  6. 一些实用的php库

    Idiorm – 轻量级ORM库 Snappy – 快照/PDF库 Buzz – 简单的HTTP请求库 Omnipay – 支付处理库 ShellWrap – 优美的命令行包装器

  7. Android适配底部返回键等虚拟键盘的完美解决方案

    这个问题来来回回困扰了我很久,一直没能妥善解决. 场景1:华为手机遮挡了屏幕底部. 场景2:进入应用时,虚拟键自动缩回,留下空白区域. 需求: 需要安卓能自适应底部虚拟按键,用户隐藏虚拟按键时应用要占 ...

  8. winform中键盘和鼠标事件的捕捉和重写

    在编写winform应用程序时,有时需要无论在哪个控件获取焦点时,对某一个键盘输入或者鼠标事件都进行同样的操作.比如编写一个处理图片的应用程序时,希望无论当前哪个控件获得焦点,当用户按上.下.左.右键 ...

  9. Android后台处理最佳实践(Best Practices for Background Jobs)

    本课将告诉你如何通过后台加载来加速应用启动和降低应用耗电. 后台跑服务 除非你做了特殊指定,否则在应用中的大部分前台操作都是在一个特殊的UI线程里面进行的.这有可能会导致一些问题,因为长时间运行的操作 ...

  10. 温故而知新 phpstudy 设置 nginx 代理

    nginx.conif 找到 server 关键字配置 server { listen ; server_name localhost; #charset koi8-r; #access_log lo ...