列表迭代器 +操作员

发布于 2024-10-01 04:30:21 字数 560 浏览 0 评论 0原文

// ((++currentEntry)--) is equivalent to (currentEntry + 1). Kind of.
menuEntries.insert((++currentEntry)--, newEntries.begin(), newEntries.end());

所以我这里有世界上最糟糕的代码。有更好的方法吗?

当使用“+ 1”时,我得到这个:

source/menu.cpp:146:37: error: invalid operands to binary expression
      ('list<menuEntry *>::iterator' (aka '_List_iterator<menuEntry *>') and
      'int')
                            menuEntries.insert(currentEntry + 1, ...
                                               ~~~~~~~~~~~~ ^ ~
// ((++currentEntry)--) is equivalent to (currentEntry + 1). Kind of.
menuEntries.insert((++currentEntry)--, newEntries.begin(), newEntries.end());

So I have the world's worst piece of code here. Is there a better way to do this?

When using '+ 1' I get this:

source/menu.cpp:146:37: error: invalid operands to binary expression
      ('list<menuEntry *>::iterator' (aka '_List_iterator<menuEntry *>') and
      'int')
                            menuEntries.insert(currentEntry + 1, ...
                                               ~~~~~~~~~~~~ ^ ~

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

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

发布评论

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

评论(5

伤感在游骋 2024-10-08 04:30:22

为什么不分成多行:

iterator nextEntry = currentEntry;
menuEntries.insert( ++nextEntry, newEntries.begin(), newEntries.end());

其中 iterator 是列表的迭代器类型。就我个人而言,为了进一步清晰起见,我可能会将 ++nextEntry 拉到自己的行上 - 但这可能是一个主观决定。

Why not split into multiple lines:

iterator nextEntry = currentEntry;
menuEntries.insert( ++nextEntry, newEntries.begin(), newEntries.end());

where iterator is iterator type for the list. Personally, I would probably pull the ++nextEntry onto its own line for further clarity - but that is probably a subjective decision.

机场等船 2024-10-08 04:30:22

辅助函数怎么样:

template<class IterT>
IterT Next(IterT i)
{
  return ++i;
}

然后您可以将 (++currentEntry)-- 替换为 Next(currentEntry)

编辑:
甚至更好:如果您使用 Boost,请参阅 Rob 的建议 下一步和实用程序库中的优先级

How about a helper function:

template<class IterT>
IterT Next(IterT i)
{
  return ++i;
}

You could then replace (++currentEntry)-- by Next(currentEntry)

Edit:
Or even better: If you use Boost see Rob's suggestion of next and prior in the Utility library

水溶 2024-10-08 04:30:22
++currentEntry;
menuEntries.insert(currentEntry, newEntries.begin(), newEntries.end());
--currentEntry;

或者

menuEntries.insert(++currentEntry, newEntries.begin(), newEntries.end());
--currentEntry;
++currentEntry;
menuEntries.insert(currentEntry, newEntries.begin(), newEntries.end());
--currentEntry;

or

menuEntries.insert(++currentEntry, newEntries.begin(), newEntries.end());
--currentEntry;
无边思念无边月 2024-10-08 04:30:22

您可以使用反向迭代器,例如:

myList.insert(myList.rbegin(), newEntries.begin(), newEntries.end())

将新项目添加到末尾。

当然,这是否适合您取决于您​​如何使用迭代器。

You can use reverse iterators, for example:

myList.insert(myList.rbegin(), newEntries.begin(), newEntries.end())

would add the new items to the end.

Of course, whether this works for you depends on how you are using your iterator.

梅窗月明清似水 2024-10-08 04:30:22

由于您不想影响 currentEntry 迭代器,并且希望在 currentEntry 之后插入成员,因此 currentEntry + 1 是最佳选择。

Since you don't want to affect your currentEntry iterator, and you want to insert the member after currentEntry, currentEntry + 1 is the best bet.

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