在C++中实现foreach循环,比for_each更简洁!
原文:http://blogread.cn/it/article/2570?f=sr
python,c#,java里面都有类似于foreach的结构,stl里面虽然有for_each这个函数,但是感觉使用还是太繁琐了一些,所以就自己实现了一个。 先来看看stl里面的for_each函数,官方文档上的原型如下:
Function for_each (InputIterator first, InputIterator last, Function f);
示例代码如下:
// for_each example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void myfunction (int i) {
cout << " " << i;
}
struct myclass {
void operator() (int i) {cout << " " << i;}
} myobject;
int main () {
vector<int> myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
cout << "myvector contains:";
for_each (myvector.begin(), myvector.end(), myfunction);
// or:
cout << "\nmyvector contains:";
for_each (myvector.begin(), myvector.end(), myobject);
cout << endl;
return 0;
}
不只函数原型有点不习惯,而且还要再写一个函数,比起python的实现方式,的确有点繁琐了:
for d in l:
print d
我们来自己实现一个,方法肯定是用宏啦,我们来看一下第一个版本:
#define foreach(container,it,type) \
for(type::iterator it = (container).begin();it!=(container).end();++it)
示例代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define foreach(container,it,type) \
for(type::iterator it = (container).begin();it!=(container).end();++it)
int main(int argc, const char *argv[])
{
set<string> s;
s.insert("w");
s.insert("a");
s.insert("n");
foreach(s,it,set<string>)
{
cout<<*it<<endl;
}
/*map<unsigned,string> m;
m[0]="x";
m[1]="w";
foreach(m,it,map<unsigned,string>)
{
cout<<it->first<<","<<it->second<<endl;
}*/
return 0;
}
如果把注释掉的代码打开的话,就会报错,应该是宏无法处理逗号的原因。
而且调用起来还是有点繁琐对吧,python里面并没有要求传入容器类型,我们是不是也能把set这个参数省掉呢?
先来看一下这段代码:
typeof(10) a;
a = 100;
cout<<a<<endl
#define foreach(container,it) \
for(typeof(container)::iterator it = (container).begin();it!=(container).end();++it)
然而很不幸,这段代码是无法运行的,编译结果如下:
test4.cpp|21| error: expected initializer before "it" test4.cpp|21| error: `it' was not declared in this scope test4.cpp|34| error: expected initializer before "it" test4.cpp|34| error: `it' was not declared in this scope
有没有办法解决呢?
有的,我们用一个曲线救国的方法!typeof(container.begin()) ,哈哈!最终代码如下:
#define foreach(container,it) \
for(typeof((container).begin()) it = (container).begin();it!=(container).end();++it)
测试代码如下:
#include <vector>
#include <set>
#include <map>
using namespace std;
#define foreach(container,it) \
for(typeof((container).begin()) it = (container).begin();it!=(container).end();++it)
int main(int argc, const char *argv[])
{
set<string> s;
s.insert("w");
s.insert("a");
s.insert("n");
foreach(s,it)
{
cout<<*it<<endl;
}
map<unsigned,string> m;
m[0]="x";
m[1]="w";
foreach(m,it)
{
cout<<it->first<<","<<it->second<<endl;
}
return 0;
}
输入结果如下:
a n w 0,x 1,w
OK!一切正常!这应该是形式比较简单的版本啦,如果各位有什么更好的建议,欢迎留言交流~
ps:
当然,其实你连it这个参数都可以省掉,但是根据pythonic的原则(好吧,我知道自己是在写C++),要简单但不能让人迷惑,所以建议还是把it这个参数保留。
在C++中实现foreach循环,比for_each更简洁!相关推荐
- 在Java中的foreach循环中调用remove
本文翻译自:Calling remove in foreach loop in Java [duplicate] This question already has an answer here: 这 ...
- c foreach循环_C ++中的foreach循环
c foreach循环 介绍 (Introduction) The foreach loop in C++ or more specifically, range-based for loop was ...
- java foreach标签_Java中Velocity foreach循环标签详解
Java中Velocity foreach循环标签详解 Java Velocity中foreach循环可以很容易的遍历数组或者集合. 定义 #foreach( $elem in $allElems) ...
- javascript编写_如何在JavaScript中使用解构来编写更简洁,功能更强大的代码
javascript编写 by Ashay Mandwarya ?️?? 由Ashay Mandwarya提供吗? 如何在JavaScript中使用解构来编写更简洁,功能更强大的代码 (How to ...
- java for循环迭代_JAVA中的for-each循环与迭代
在学习java中的collection时注意到,collection层次的根接口Collection实现了Iterable接口(位于java.lang包中),实现这个接口允许对象成为 "fo ...
- js中终止forEach循环的方法
正常终止for循环我们可以使用break关键字来实现,而在forEach循环中是不能使用break和continue这两个关键字的,为什么呢? 因为这两个关键字要在循环中使用,而forEach中所执行 ...
- java中的for-each循环
增强 for 循环可以在不知道初始值和终止值的情况下,对数组和集合元素进行遍历,其语法如下. for(数据类型 变量名 : 数组或集合){循环代码块 } 例如 使用增强 for 循环遍历一个整型数组 ...
- PHP中的foreach循环 1
PHP中的foreach语句,常用于遍历数组. foreach一般有两种使用方式: (1)只取数组的值,不取数组的下标: (2)取数组值,同时也取数组的下标: 以下面的例1和例2来解释其用法: 例1: ...
- PHP中的foreach循环
注意:foreach中的循环只适用于遍历数组,用于循环输出其中的键值对. 语法 foreach ($array as $value) { code to be executed; } 每进行一次循环迭 ...
最新文章
- Java中的集合笔记
- Spring.Net官网翻译
- DFT实训教程笔记4(bibili版本)- ATPG
- 学习历史预测未来,国防科大新模型实现未来事实预测SOTA
- python numpy np.array_Python | numpy | np.split()与np.array_split()函数
- 使用 typescript ,提升 vue 项目的开发体验(1)
- python怎么编辑文件_如何使用python中的方法对文件进行修改文件名
- 海底捞、百果园、大娘水饺凭什么可以疯狂扩张门店?
- sql dbcc_SQL Server中的DBCC命令的概念和基础
- Rust 1.7.0 处理命令行參数
- Intellij IDEA 2016 使用
- 转载“用USBOOT制作DOS启动盘”
- Windows 7声卡驱动一键修复精灵3.0完美版(2009年11月9日发布)
- 非结构化数据分析技术是忽悠
- C# 从零开始写 SharpDx 应用 画三角
- 二进制逆向实验——寻找flag
- 业务逻辑漏洞之水平越权和垂直越权
- 2017,人们视算法为「洪水猛兽」;算法说:我不想背锅
- php中 下列哪些说法是正确的,下列PHP的判断语句中( )是正确的。
- 和Leo一起做爱线段树的好孩子之火车运输