文字编码与文字编码 std::pair,解决方案?
与大多数程序员一样,我钦佩并尝试遵循文学编程的原则,但在 C++ 中,我经常发现自己使用 std::pair
来完成无数的常见任务。 但是,恕我直言,std::pair 是文学编程的邪恶敌人……
我的观点是,当我回到一两天前编写的代码时,我看到了对std::pair
(通常作为迭代器)我想知道“iter->first 和 iter->second 是什么意思???”。
我猜其他人在查看他们的 std::pair 代码时也有同样的疑问,所以我想知道是否有人想出一些好的解决方案来在使用 std:: 时恢复读写能力配对?
As most programmers I admire and try to follow the principles of Literate programming, but in C++ I routinely find myself using std::pair
, for a gazillion common tasks. But std::pair
is, IMHO, a vile enemy of literate programming...
My point is when I come back to code I've written a day or two ago, and I see manipulations of a std::pair
(typically as an iterator) I wonder to myself "what did iter->first and iter->second mean???".
I'm guessing others have the same doubts when looking at their std::pair
code, so I was wondering, has anyone come up with some good solutions to recover literacy when using std::pair
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
std::pair
是使用本质上匿名列创建“本地”且本质上匿名类型的好方法; 如果您在如此大的词汇空间中使用某个对,以至于需要命名类型和列,那么我会使用普通的 struct 来代替。std::pair
is a good way to make a "local" and essentially anonymous type with essentially anonymous columns; if you're using a certain pair over so large a lexical space that you need to name the type and columns, I'd use a plainstruct
instead.怎么样:
这有点冗长,但是在代码中使用它可能会使事情更容易阅读,例如:
除此之外,我看不到任何能让事情变得更清晰的灵丹妙药。
How about this:
It's a bit verbose, however using this in your code might make things a little easier to read, eg:
Other than this, I can't see any silver bullet that's going to make things much clearer.
...你明白了。
...you get the point.
您可以创建两对 getter(常量和非),它们仅返回对第一个和第二个的引用,但更具可读性。 例如:
将让您从给定对中获取字段和值成员,而不必记住哪个成员包含什么。
如果您希望经常使用它,您还可以创建一个宏,根据名称和类型为您生成这些 getter:MAKE_PAIR_GETTERS(Field, string, Value, int) 等。 让 getter 变得简单可能会允许编译器优化它们,因此它们不会在运行时增加任何开销; 使用宏可以轻松地为您对成对的任何用途创建这些吸气剂。
You can create two pairs of getters (const and non) that will merely return a reference to first and second, but will be much more readable. For instance:
Will let you get the field and value members from a given pair without having to remember which member holds what.
If you expect to use this a lot, you could also create a macro that will generate those getters for you, given the names and types: MAKE_PAIR_GETTERS(Field, string, Value, int) or so. Making the getters straightforward will probably allow the compiler to optimize them away, so they'll add no overhead at runtime; and using the macro will make it a snap to create those getters for whatever use you make of pairs.
您可以使用 boost 元组,但它们并不能真正改变根本问题:您是否真的想要使用小型整数类型访问对/元组的每个部分,或者您想要更多的“文化” ' 代码。 请参阅我不久前发布的这个问题。
但是,boost::可选是一个有用的工具,我发现它取代了很多将成对/元组吹捧为答案的情况。
You could use boost tuples, but they don't really alter the underlying issue: Do your really want to access each part of the pair/tuple with a small integral type, or do you want more 'literate' code. See this question I posted a while back.
However, boost::optional is a useful tool which I've found replaces quite a few of the cases where pairs/tuples are touted as ther answer.
最近我发现自己使用
boost::tuple
作为std::pair
的替代品。 您可以为每个成员定义枚举器,因此每个成员的含义很明显:顺便说一句,如果使用此方法存在隐藏成本,欢迎发表评论。
编辑:从全局范围中删除名称。
只是关于全局命名空间的快速评论。 一般来说,我会使用:
看起来确实是这样,
boost::fusion
确实将身份和价值更紧密地联系在一起。Recently I've found myself using
boost::tuple
as a replacement forstd::pair
. You can define enumerators for each member and so it's obvious what each member is:BTW, comments welcome on if there is a hidden cost to using this approach.
EDIT: Remove names from global scope.
Just a quick comment regarding global namespace. In general I would use:
It does look to be the case that
boost::fusion
does tie the identity and value closer together.正如 Alex 提到的,std::pair 非常方便,但是当创建一个结构并以相同的方式使用它变得令人困惑时,请查看 std::pair 代码,没那么复杂。
As Alex mentioned,
std::pair
is very convenient but when it gets confusing create a structure and use it in the same way, have a look atstd::pair
code, it's not that complex.我也不喜欢 std::map 中使用的 std::pair,映射条目应该有成员键和值。
我什至使用 boost::MIC 来避免这种情况。 然而,boost::MIC 也是有代价的。
另外,返回 std::pair 会导致代码可读性较差:
???
我还发现 std::pair 常被懒惰的程序员使用,他们需要 2 个值,但没有想到为什么需要一起使用这些值。
I don't like std::pair as used in std::map either, map entries should have had members key and value.
I even used boost::MIC to avoid this. However, boost::MIC also comes with a cost.
Also, returning a std::pair results in less than readable code:
???
I also found that std::pair is commonly used by the lazy programmers who needed 2 values but didn't think why these values where needed together.