中map怎樣逆序輸出?
反向迭代器 #include using namespace std; int main() { map mp; // map是紅黑排序樹, 遍歷的時(shí)候自然就有序了 for (int i = 0; i < 10; ++i) { mp[i] = i + 1; } // 正向遍歷 map::iterator it = mp.begin(); while (it != mp.end()) { cout << it->first << ' ' << it->second << " , "; ++it; } cout << endl; // 反向遍歷 map::reverse_iterator rIt = mp.rbegin(); while (rIt != mp.rend()) { cout << rIt->first << ' ' << rIt->second << " , "; ++rIt; } cout << endl; }