C++学习随笔——使用map和迭代器iterator的简单范例

使用 :: 表示 iterator 是 std::map<std::string, double> 类的成员类型。

点击查看代码
#include <iostream>
#include <map>

int main() {
    // 创建一个 map,其中键是字符串,值是浮点数
    std::map<std::string, double> myMap;

    // 向 map 中添加一些元素
    myMap["apple"] = 1.2;
    myMap["banana"] = 0.8;
    myMap["orange"] = 1.5;

    // 声明一个迭代器
    std::map<std::string, double>::iterator iter;

    // 使用迭代器遍历 map
    for (iter = myMap.begin(); iter != myMap.end(); ++iter) {
        std::cout << iter->first << ": " << iter->second << std::endl;
    }

    return 0;
}

posted @   北宸于烁  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示