迭代器 iterator->second含义

转自stackoverflow




down vote accepted

I'm sure you know that a std::vector<X> stores a whole bunch of X objects, right? But if you have a std::map<X, Y>, what it actually stores is a whole bunch of std::pair<const X, Y>s. That's exactly what a map is - it pairs together the keys and the associated values.

When you iterate over a std::map, you're iterating over all of these std::pairs. When you dereference one of these iterators, you get a std::pair containing the key and its associated value.

std::map<std::string, int> m = /* fill it */;
auto it = m.begin();

Here, if you now do *it, you will get the the std::pair for the first element in the map.

Now the type std::pair gives you access to its elements through two members: first and second. So if you have a std::pair<X, Y> called pp.first is an X object and p.second is a Y object.

So now you know that dereferencing a std::map iterator gives you a std::pair, you can then access its elements with first and second. For example, (*it).first will give you the key and (*it).second will give you the value. These are equivalent to it->first and it->second.


86 down vote accepted

I'm sure you know that a std::vector<X> stores a whole bunch of X objects, right? But if you have a std::map<X, Y>, what it actually stores is a whole bunch of std::pair<const X, Y>s. That's exactly what a map is - it pairs together the keys and the associated values.

When you iterate over a std::map, you're iterating over all of these std::pairs. When you dereference one of these iterators, you get a std::pair containing the key and its associated value.

std::map<std::string, int> m = /* fill it */;
auto it = m.begin();

Here, if you now do *it, you will get the the std::pair for the first element in the map.

Now the type std::pair gives you access to its elements through two members: first and second. So if you have a std::pair<X, Y> called pp.first is an X object and p.second is a Y object.

So now you know that dereferencing a std::map iterator gives you a std::pair, you can then access its elements with first and second. For example, (*it).first will give you the key and (*it).second will give you the value. These are equivalent to it->first and it->second.

给下面每一行代码给上注释#include "pch.h" #include "FinBudgetFlowManager.h" int FinBudgetFlowManager::C(double _value, time_t _time, CString _remark, FlowType _type) { FinBudgetFlow* pFBD = new FinBudgetFlow(); pFBD->setValue(_value); pFBD->setTime(_time); pFBD->setRemark(_remark); pFBD->setType(_type);; this->FlowMap.insert(std::map<int, FinBudgetFlow*>::value_type(this->getSize(), pFBD)); return this->getSize()-1; } bool FinBudgetFlowManager::U(int _index, double _value, time_t _time, CString _remark, FlowType _type){ std::map<int, FinBudgetFlow*>::iterator iter; iter = this->FlowMap.find(_index); if (iter != this->FlowMap.end()) { delete iter->second; iter->second = NULL; this->FlowMap.erase(iter); FinBudgetFlow *pFBD = new FinBudgetFlow(); pFBD->setValue(_value); pFBD->setTime(_time); pFBD->setRemark(_remark); pFBD->setType(_type); this->FlowMap.insert(std::map<int, FinBudgetFlow*>::value_type(_index, pFBD)); return true; } else { throw _index; } } FinBudgetFlow* FinBudgetFlowManager::R(int _index) { std::map<int, FinBudgetFlow*>::iterator iter; iter = this->FlowMap.find(_index); if (iter != this->FlowMap.end()) { return iter->second; } else { throw _index; } } bool FinBudgetFlowManager::D(int _index) { std::map<int, FinBudgetFlow*>::iterator iter; iter = this->FlowMap.find(_index); if (iter != this->FlowMap.end()) { delete iter->second; iter->second = NULL; //防止野指针这一步是否真的需要? this->FlowMap.erase(iter); return true; } else { throw _index; } } int FinBudgetFlowManager::getSize() { return FlowMap.size(); }
06-10
麻烦优化一下下列C++代码 void PIN_FAST_ANALYSIS_CALL onRead(THREADID threadid, ADDRINT memoryAddr){ ThreadData* t = get_tls(threadid); t->readCounter++; // get latest version value of this memory location map<ADDRINT, std::pair<vector<UINT32>, std::pair<THREADID, UINT32> > >::iterator it = t->shadowRead.find(memoryAddr); if (it != t->shadowRead.end()){ // if its in the thread's local memory /*(implementation of the last one value predictor)*/ // if it already exists. update the counter for the thread by 1 // for the location. it->second.first[threadid]++; } else { // if hasn't been read by current thread before //insert record into memoryMap vector <UINT32> temp(8,0); t->shadowRead[memoryAddr] = std::make_pair(temp, std::make_pair(0,0)); // insert pair of vector and another pair t->shadowRead[memoryAddr].first[threadid] = 1; } // Get last write to memoryAddr and save order with read in execution log PIN_GetLock(&writeLock, threadid + 1); rdOps++; unordered_map<ADDRINT, std::pair<vector<std::pair<THREADID, UINT32> > ,bool> >::iterator itt = memoryMap.find(memoryAddr); map<ADDRINT, std::pair<vector<UINT32>, std::pair<THREADID, UINT32> > >::iterator ita = t->shadowRead.find(memoryAddr); if ((itt != memoryMap.end()) && (itt->second.first.size() > 0) && (itt->second.first.back().first != t->tid) && (itt->second.first.back().first != ita->second.second.first && itt->second.first.back().second != ita->second.second.second)){ // optimize to weed out intra-thread dependencies on shared memory locations //EXECUTION LOG FORMAT WRITE-READ: WRITETHREAD WRITECOUNTER READ traceFileReads << itt->second.first.back().first << "," << itt->second.first.back().second << "," << t->tid << "," << t->readCounter << endl; } PIN_ReleaseLock(&writeLock);
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值