C ++算法,如python的'groupby'
问题描述
是否有任何类似于 itertools.groupby()
的 C++ 转换?
Are there any C++ transformations which are similar to itertools.groupby()
?
当然,我可以轻松编写自己的代码,但我更喜欢利用惯用行为,或者从 STL 或 boost
提供的功能中组合一个.
Of course I could easily write my own, but I'd prefer to leverage the idiomatic behavior or compose one from the features provided by the STL or boost
.
推荐答案
用一行代码的算法来膨胀标准 C++ 库有什么意义?
What is the point of bloating standard C++ library with an algorithm that is one line of code?
另外,看看 std::multimap
,它可能正是你所需要的.
Also, take a look at std::multimap
, it might be just what you need.
更新:
当你的向量已经排序时,我提供的单行没有很好地优化.如果我们记住先前插入对象的迭代器,则可以减少许多映射查找,因此它是下一个对象的键",并且仅在键更改时才进行查找.例如:
The one-liner I have provided is not well-optimized for the case when your vector is already sorted. A number of map lookups can be reduced if we remember the iterator of previously inserted object, so it the "key" of the next object and do a lookup only when the key is changing. For example:
这篇关于C ++算法,如python的'groupby'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!