模仿的循环幻想 [英] Templated for-loop fantasy
问题描述
我经常使用STL集合课程,并经常发现自己
写的内容如下:
vector< CDataClass> :: iterator iter = DataClassCollection .begin();
vector< CDataClass> :: iterator iter_end = DataClassCollection.end();
for(; iter!= iter_end; ++ iter)
{
//用它做东西......
}
我有这样的幻想它应该可以创建一个模板
用以下内容替换所有这些:
for< CDataClass>(DataClassCollection)dataiteration;
// for<>模板可以有各种循环类型和数据项
(迭代器,etceteras)
//已定义。迭代
DataClassCollection中每个元素的简单情况可能是
//看起来像这样:
dataiteration.iterate
{
//用dataiteration.iter做什么>
}
是的,我知道for_each方法。问题是
for_each使用起来有点尴尬,我想要一个模板构造
,它具有在后续语句中运行的能力或者
语句块。我不知道C ++中存在这样的能力。
任何想法/建议?
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
I use the STL collection classes a lot, and frequently find myself
writing something like:
vector<CDataClass>::iterator iter=DataClassCollection.begin();
vector<CDataClass>::iterator iter_end=DataClassCollection.end();
for(;iter!=iter_end;++iter)
{
//doing stuff with iter...
}
I have this fantasy that it ought to be possible to create a template
to replace all of that with something that looks like:
for<CDataClass>(DataClassCollection) dataiteration;
//the for<> template could have a variety of loop types and data items
(iterators, etceteras)
//defined. A simple case that iterates over each element in the
DataClassCollection might
//look like this:
dataiteration.iterate
{
//doing stuff with "dataiteration.iter"
}
And yes, I know about the for_each method. The problem is that
for_each is a bit awkward to use, and I''d like a template construct
which has the capability of operating on a subsequent statement or
block of statements. I''m not aware that such a capabity exists in C++.
Any ideas/suggestions?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
algorimancer写道:
algorimancer wrote:
我经常使用STL集合类,经常发现自己
编写类似的东西:
vector< CDataClass> :: iterator iter = DataClassCollection.begin();
vector< CDataClass> :: iterator iter_end = DataClassCollection。结束();
for(; iter!= iter_end; ++ iter)
{
用它做事......
}
I use the STL collection classes a lot, and frequently find myself
writing something like:
vector<CDataClass>::iterator iter=DataClassCollection.begin();
vector<CDataClass>::iterator iter_end=DataClassCollection.end();
for(;iter!=iter_end;++iter)
{
//doing stuff with iter...
}
>
您是否看过boost ForEach宏,计划在某个阶段成为
增强发行版的一部分?
http://boost-consulting.com /vault/in...ory=Algorithms
如果链接不起作用,请转到 http://www.boost.org 。找到金库的链接
并查看算法目录。
欢呼
Andy Little
Have you looked at boost ForEach macro, scheduled to be part of the
boost distro at some stage?
http://boost-consulting.com/vault/in...ory=Algorithms
If the link doesnt work go to http://www.boost.org. Find link to vault
and look in algorithms directory.
cheers
Andy Little
algorimancer写道:
algorimancer wrote:
我经常使用STL集合类,经常发现自己写了类似的东西:
vector< CDataClass> :: iterator iter = DataClassCollection.begin();
vector< CDataClass> :: iterator iter_end = DataClassCollection.end();
for(; iter != iter_end; ++ iter)
//
//用iter做什么......
}
I use the STL collection classes a lot, and frequently find myself
writing something like:
vector<CDataClass>::iterator iter=DataClassCollection.begin();
vector<CDataClass>::iterator iter_end=DataClassCollection.end();
for(;iter!=iter_end;++iter)
{
//doing stuff with iter...
}
#include< ; boost / foreach.hpp>
BOOST_FOREACH(CDataClass& v,DataClassCollection)
{
//用v做的东西
}
James
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
#include <boost/foreach.hpp>
BOOST_FOREACH(CDataClass& v, DataClassCollection)
{
// do stuff with v
}
James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
我没有意识到增强FOREACH宏,它看起来好像可以实现这个技巧。升级库似乎比我上次看到的时间更容易接近,我会在那里花更多时间。
谢谢:)
I had not been aware of the boost FOREACH macro, it looks like it could
do the trick. The boost library seems more accessible than the last
time I looked at it, I''ll spend some more time there I think.
Thanks :)
这篇关于模仿的循环幻想的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!