C++中的iterator->second

iterator->second意思

转自 http://lib.csdn.net/article/cplusplus/36005
std::map<X, Y>实际储存了一串std::pair<const X, Y>

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

这里,如果你用*it,那么你将得到map第一个元素的std::pair

现在你可以接收std::pair的两个元素:

(*it).first会得到key,

(*it).second会得到value。

这等同于it->firstit->second

  • 80
    点赞
  • 126
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
麻烦优化一下下列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
以下是对给定代码进行优化的建议: 1. 减少锁的使用:在当前代码,使用了两个锁 `writeLock` 和 `threadLock`。可以考虑将锁的范围缩小到仅在需要保护共享资源的关键部分使用,以减少锁的开销。 2. 使用 auto 关键字简化迭代器声明:可以使用 `auto` 关键字来自动推导迭代器类型,从而简化代码并提高可读性。 3. 减少无效的查找操作:在代码多次查找相同的 `memoryAddr`,可以考虑将查找结果保存到临时变量,以避免重复查找。 4. 减少不必要的显式创建对象:在代码创建了多个临时对象,可以考虑使用局部变量或引用来代替,以减少不必要的对象创建和销毁。 5. 使用迭代器访问容器元素:在代码使用了下标访问容器元素,可以考虑使用迭代器来访问容器元素,以提高效率。 6. 使用更合适的容器:在代码使用了 `unordered_map` 和 `map`,可以根据具体需求选择更合适的容器。例如,如果只需要快速的查找操作,可以考虑使用 `unordered_map`。 以下是优化后的代码示例: ```cpp void PIN_FAST_ANALYSIS_CALL onRead(THREADID threadid, ADDRINT memoryAddr) { ThreadData* t = get_tls(threadid); t->readCounter++; auto it = t->shadowRead.find(memoryAddr); if (it != t->shadowRead.end()) { it->second.first[threadid]++; } else { vector<UINT32> temp(8, 0); t->shadowRead[memoryAddr] = {temp, {0, 0}}; t->shadowRead[memoryAddr].first[threadid] = 1; } PIN_GetLock(&writeLock, threadid + 1); rdOps++; auto itt = memoryMap.find(memoryAddr); auto 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)) { traceFileReads << itt->second.first.back().first << "," << itt->second.first.back().second << "," << t->tid << "," << t->readCounter << endl; } PIN_ReleaseLock(&writeLock); } ``` 在上述代码,我将锁的范围缩小到仅在需要保护共享资源的关键部分使用,使用了 `auto` 关键字简化了迭代器声明,并且避免了不必要的查找操作和对象创建。此外,还可以根据具体需求进行进一步的优化和调整。请确保对共享数据的访问是线程安全的,并避免出现数据竞争和死锁等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值