C++之map、list操作

本文介绍了C++中使用标准模板库(STL)中的map容器进行数据存储和遍历的方法。通过实例展示了如何定义结构体作为map的value类型,并演示了如何插入数据及遍历map。此外还对比了map与list、vector的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include "map_struct.h"
#include <map>

using namespace std;

typedef struct real_data{
    int reg_addr;
    string desc;
}HistRecord;


int main()
{
    cout << "Hello world!" << endl;

    map<int, HistRecord> m_upmcsbConf;
    HistRecord histRecordData;
    histRecordData.reg_addr = 1;
    histRecordData.desc="aaaaaaaaaa";
    m_upmcsbConf.insert(pair<int,HistRecord>(1,histRecordData));

histRecordData.reg_addr = 2;
    histRecordData.desc="bbbbbb";
    m_upmcsbConf.insert(pair<int,HistRecord>(2,histRecordData));

    map<int, HistRecord>::iterator it;

    for ( it = m_upmcsbConf.begin(); it != m_upmcsbConf.end(); ++it) {
        cout << it->first  << " - > " << it->second.reg_addr << endl;
    }


    return 0;

}

Map遍历操作以及插入操作。

要点:insert插入要用pair, 遍历的时候it->first的first后面没有first()

map获取其中的元素值,则像数组一样直接操作:例: 

m_upmcsbConf[i]则取出real_data结构体。

list用法:

list是双向链表,list无法指定取某元素的值。没有list.at函数,

 

vector

 vector是开辟一段连续的内存空间,与list相比,随机访问速度快,但插入删除操作慢

 

转载于:https://www.cnblogs.com/hzijone/p/5479793.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值