语义迭代器声明?
我很抱歉这个非常模糊的标题,我真的不知道如何命名这个问题。
假设我有这个:
std::list<std::string> msgs;
for (std::list<std::string>::iterator it = msgs.begin(); it < msgs.end(); it++) {
// ...
}
对我来说,这很难阅读。 std::list
几乎看起来像一个神奇的数字,特别是如果 msgs
的声明很远,比如在头文件中。在我看来,如果它是这样的,它会更容易阅读并且更具语义:
std::list<std::string> msgs;
for (msgs.iterator it = msgs.begin(); it < msgs.end(); it++) {
// ...
}
现在,这显然是非法的 C++。但我的问题是,有没有一种方法可以实现支持像这样编写迭代器声明的东西?
I'm sorry for the very vague title, I really didn't know how to title this question.
Let's say I have this:
std::list<std::string> msgs;
for (std::list<std::string>::iterator it = msgs.begin(); it < msgs.end(); it++) {
// ...
}
For me, this is hard to to read. The std::list<std::string>::iterator
almost seems like a magic number, especially if the declaration of msgs
is far away, like in an header file. IMO it would be easier to read and much more semantic if it were something like this:
std::list<std::string> msgs;
for (msgs.iterator it = msgs.begin(); it < msgs.end(); it++) {
// ...
}
Now, this is obviously illegal C++. But my question is, is there a way of implementing something that supports writing iterator declarations like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您提供 typedef,它将使生活变得更加轻松:
除此之外,c++0x 通过提供 auto 关键字来为您做到这一点:
edit 就像日期后 5 年一样,但我更新了
<
tp!=
并使用预增量,因为这实际上是我多年来一直在做的事情,并且对我来说更有意义,以至于它让我想知道我是如何写的这个答案不使用那个If you provide a typedef it will make life a lot easier:
Other than that, c++0x does it for you by providing the auto keyword:
edit like 5 years after date, but I updated
<
tp!=
and using preincrement because that's actually what I've been doing for ages, and makes more sense to me, to the point it makes me wonder how I ever wrote this answer not using that有两种实用的规范方法:
以及仅限 C++0x
auto
:此外,C++0x 允许:
我认为这是与您具体情况最接近的匹配询问(即使这不一定是最好的解决方案)。
缺点是能够将作用域运算符 (
::
) 应用于decltype
link 最近才被投票纳入工作文件,我不知道任何支持它的编译器(GCC 4.5.1 不)。There are two practical, canonical approaches:
And the C++0x-only
auto
:In addition, C++0x allows:
I think that this is the closest match to what you were specifically asking (even if it's not necessarily the best solution).
The downside is that the ability to apply the scope operator (
::
) todecltype
link was only voted into the working paper relatively recently, and I'm not aware of any compilers that support it yet (GCC 4.5.1 does not).您可以使用 typedef 来清理它:
You can use typedef to clean that up:
所有容器都具有这些
typedef
来支持编写非常通用的代码。考虑以下函数模板:这使您能够编写可与定义类型
const_iterator
和方法begin()
和end 的任何对象一起使用的代码()
。在 C++0x 中,通过引入
auto
关键字解决了该问题:All containers have these
typedef
s to enable the writing of very generic code. Consider the following function-template:This enables you to write code that can work with any object that has defines a type
const_iterator
and a methodbegin()
andend()
.In C++0x the problem is solved with the introduction of the
auto
keyword:规范的解决方案是
在 C++0x 之前,编写
...
位曾经有点困难(如果它很复杂的话),但我们现在有了 lambda。The canonical solution is
Before C++0x, writing the
...
bit used to be a bit hard (if it was anything complex), but we now have lambda's.