在 C++ 中命名和使用迭代器的正常约定是什么?

发布于 2024-09-25 23:11:30 字数 450 浏览 3 评论 0原文

我觉得我命名和使用迭代器的方式不专业。我的意思是,我“感觉”应该用别的名字来称呼它们,但我总是根据“it_”前缀来命名它们,过了一会儿,在一个很长的函数中,这些名称开始看起来很相似。

此外,我总是想知道我是否正在以“奇怪”的方式做事,而我学到的只是因为我不知道更好。例如,如果我正在迭代一个映射以显示其所有键/值对,我会这样做:

map<int, int>::const_iterator it = layout.begin();
for (; it != layout.end(); ++it)
{
   cout << it->first << ":\t" << it->second << "\n";
}

我看到有些人称他们的迭代器为“iter” - 我看到其他执行循环的方法。有没有一种超越风格的惯例并且只是良好的实践?

I feel like I'm unprofessional in the way I name and use iterators. What I mean by that is that I "feel" like I should be calling them something else, but I always name them based on the "it_" prefix, and after a while, in a long function, the names start to all look alike.

Additionally, I always wonder if I'm doing things a "strange" way that I learned just because I didn't know better. For instance, if I were iterating through a map to display all its key/value pairs, I would do this:

map<int, int>::const_iterator it = layout.begin();
for (; it != layout.end(); ++it)
{
   cout << it->first << ":\t" << it->second << "\n";
}

I see some people calling their iterators "iter" - I see other ways of doing loops. Is there any kind of convention that transcends style and is just good practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风追烟花雨 2024-10-02 23:11:31

我尝试尽可能在 for 循环内声明迭代器,以便最小化迭代器标识符的范围。

首先,我 typedef 将“长”类型名称改为“较短”且可重用的名称:

typedef std::map< int, int > IntMap;
typedef IntMap::const_iterator IntMapConstIter;

然后,我这样做

for( IntMapConstIter it = layout.begin(); it != layout.end(); ++it ) {
    ....
}

I try to declare the iterators inside the for loop as much as possible, so that the scope of the iterator identifier is minimized.

First, I typedef the "long" type names to "shorter" and re-usable ones:

typedef std::map< int, int > IntMap;
typedef IntMap::const_iterator IntMapConstIter;

Then, I do

for( IntMapConstIter it = layout.begin(); it != layout.end(); ++it ) {
    ....
}
时光是把杀猪刀 2024-10-02 23:11:31

我这样做如下,

for (map<int, int>::const_iterator it = layout.begin();
     it != layout.end(); ++it)
{
    cout << it->first << ":\t" << it->second << "\n";
}

感谢迭代器的作用域仅在块内,这使得给它一个更短的名称更安全。

I do that as follows

for (map<int, int>::const_iterator it = layout.begin();
     it != layout.end(); ++it)
{
    cout << it->first << ":\t" << it->second << "\n";
}

Thanks to that the iterator is scoped only inside the block, which makes it safer to give it a shorter name.

初见 2024-10-02 23:11:31

我很确定在这件事上没有任何惯例能够超越风格。我相信迭代器命名最重要的部分是第二部分(考虑到您决定添加前缀,应该是“it”或“iter_”或其他任何内容)。像选择任何其他变量一样仔细选择第二部分,就可以了。

I am pretty sure no convention transcends style for this matter. I believe the most important part of iterator naming is on the second part (considering you decide to add a prefix, should it be "it" or "iter_" or anything else). Choose the second part carefully as you would for any other variable and you'll be fine.

2024-10-02 23:11:31

我还没有看到一个规范循环。

创建迭代器的成本指定为O(1)(与容器的大小有关),但它可能会产生成本,特别是在激活特定调试选项时。

因此,在循环的每次迭代中调用 end 是一种浪费。

因此,编写 for 循环的规范方法是在第一个语句中计算 itend

typedef std::map<int,int> LayoutType;

for (LayoutType::const_iterator it = layout.begin(), end = layout.end();
     it != end; ++it)
{
  // ...
}

我通常typedef我的容器,但老实说typedef迭代器没有多大意义,它已经在容器中可用,引入太多同义词没有帮助。

另外,这可能是个人怪癖,但当我介绍我的typedef时,我更喜欢声明函数而不是类型。类型可能会在以后的重构中改变,但函数不会(至少,如果改变的话,它根本就不是同一件事,所以它会要求完全重写)。

例如,如果您突然喜欢: typedef std::unordered_map该怎么办?布局类型?它仍然符合您的需要,并且您可能可以将其放入而无需重写成本(前提是您使用了 typedef)。

I haven't seen a single canonical loop.

The cost of creating an iterator is specified to be O(1) (wrt the size of the container), but it may cost, especially when specific debugging options are activated.

Therefore, calling end at each iteration of the loop is wasteful.

The canonical way to write a for loop is therefore to compute both it and end in the first statement.

typedef std::map<int,int> LayoutType;

for (LayoutType::const_iterator it = layout.begin(), end = layout.end();
     it != end; ++it)
{
  // ...
}

I usually typedef my container, but honestly there isn't much point in typedefing the iterator, it's already available within the container and introducing too many synonyms does not help.

Also, it may be a personal quirk, but I prefer stating the function rather than the type when I introduce my typedef. The type may change in later refactoring, the function will not (at least, it won't be the same thing at all if it does, so it'll call for a complete rewrite).

For example what if you suddenly preferred: typedef std::unordered_map<int,int> LayoutType ? It still matches your need, and you can probably drop it in at no rewriting cost (providing you have used a typedef).

对岸观火 2024-10-02 23:11:31

Would it be totally unreasonable for me to suggest the use of BOOST_FOREACH and for you to (mostly) forget about iterators ?

遗弃M 2024-10-02 23:11:31

名称对我来说是最不重要的,只要它清晰且选择得当即可。

的无休止的争论......

  • 这陷入了变量命名m_ 为成员
  • 尾随下划线为成员

The name is the least important thing to me as long as it's clear and chosen appropriately.

This falls into the endless debate of variable naming

  • m_ for members
  • trailing underscore for members
  • etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文