我对通过不重复的键向后循环特别感兴趣:
#include <map>
#include <iostream>
std::multimap<int,int> myMap = {
{1,2}, {1,2}, {2,2}, {2,2}, {3,2},
};
int main() {
using namespace std;
cout << "the keys backwards:" << endl;
typedef multimap<int, int> multimap_type;
typedef std::reverse_iterator<multimap_type::iterator> reverse_iterator;
for (auto it = myMap.rbegin(), end = myMap.rend(); it != end; it = reverse_iterator(myMap.lower_bound(it->first)))
{
cout << it->first << endl;
}
}
如您所见,我必须重复多图名称三遍。我可以编写自己的函数来处理所有这些,然后简单地调用 while 或 range for loop 吗?像那样:
while( (auto it = myIterFunc(myMap)) {
//...
}
for ( auto it : myIterFunc(myMap)) {
//...
}