溫馨提示×

C++ map iterator如何反向遍歷

c++
小樊
118
2024-07-30 11:15:08
欄目: 編程語言

要反向遍歷C++ map,可以使用rbegin()和rend()方法。以下是一個示例代碼:

#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> myMap = {{1, "one"}, {2, "two"}, {3, "three"}};

    // 使用反向迭代器反向遍歷map
    for (auto it = myMap.rbegin(); it != myMap.rend(); ++it) {
        std::cout << it->first << " : " << it->second << std::endl;
    }

    return 0;
}

在這個示例中,我們使用rbegin()方法獲取map的反向迭代器,rend()方法獲取map的結束迭代器。然后使用for循環來遍歷map,輸出每個鍵值對。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女