Skip to content

Commit 680bc2c

Browse files
Use range-based for loops (C++11) when looping over map elements
Before this commit: for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { } After this commit: for (auto& x : y) { }
1 parent c633646 commit 680bc2c

13 files changed

+74
-74
lines changed

src/addrman.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,9 @@ int CAddrMan::Check_()
390390
if (vRandom.size() != nTried + nNew)
391391
return -7;
392392

393-
for (std::map<int, CAddrInfo>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
394-
int n = (*it).first;
395-
CAddrInfo& info = (*it).second;
393+
for (const auto& entry : mapInfo) {
394+
int n = entry.first;
395+
const CAddrInfo& info = entry.second;
396396
if (info.fInTried) {
397397
if (!info.nLastSuccess)
398398
return -1;

src/addrman.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,18 @@ class CAddrMan
313313
s << nUBuckets;
314314
std::map<int, int> mapUnkIds;
315315
int nIds = 0;
316-
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
317-
mapUnkIds[(*it).first] = nIds;
318-
const CAddrInfo &info = (*it).second;
316+
for (const auto& entry : mapInfo) {
317+
mapUnkIds[entry.first] = nIds;
318+
const CAddrInfo &info = entry.second;
319319
if (info.nRefCount) {
320320
assert(nIds != nNew); // this means nNew was wrong, oh ow
321321
s << info;
322322
nIds++;
323323
}
324324
}
325325
nIds = 0;
326-
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
327-
const CAddrInfo &info = (*it).second;
326+
for (const auto& entry : mapInfo) {
327+
const CAddrInfo &info = entry.second;
328328
if (info.fInTried) {
329329
assert(nIds != nTried); // this means nTried was wrong, oh ow
330330
s << info;

src/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
110110
int nBestReachability = -1;
111111
{
112112
LOCK(cs_mapLocalHost);
113-
for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
113+
for (const auto& entry : mapLocalHost)
114114
{
115-
int nScore = (*it).second.nScore;
116-
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
115+
int nScore = entry.second.nScore;
116+
int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
117117
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
118118
{
119-
addr = CService((*it).first, (*it).second.nPort);
119+
addr = CService(entry.first, entry.second.nPort);
120120
nBestReachability = nReachability;
121121
nBestScore = nScore;
122122
}

src/qt/bantablemodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class BanTablePriv
5555
#if QT_VERSION >= 0x040700
5656
cachedBanlist.reserve(banMap.size());
5757
#endif
58-
for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
58+
for (const auto& entry : banMap)
5959
{
6060
CCombinedBan banEntry;
61-
banEntry.subnet = (*it).first;
62-
banEntry.banEntry = (*it).second;
61+
banEntry.subnet = entry.first;
62+
banEntry.banEntry = entry.second;
6363
cachedBanlist.append(banEntry);
6464
}
6565

src/qt/transactiontablemodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ class TransactionTablePriv
8080
cachedWallet.clear();
8181
{
8282
LOCK2(cs_main, wallet->cs_wallet);
83-
for(std::map<uint256, CWalletTx>::iterator it = wallet->mapWallet.begin(); it != wallet->mapWallet.end(); ++it)
83+
for (const auto& entry : wallet->mapWallet)
8484
{
85-
if(TransactionRecord::showTransaction(it->second))
86-
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, it->second));
85+
if (TransactionRecord::showTransaction(entry.second))
86+
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, entry.second));
8787
}
8888
}
8989
}

src/rpc/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,11 +569,11 @@ UniValue listbanned(const JSONRPCRequest& request)
569569
g_connman->GetBanned(banMap);
570570

571571
UniValue bannedAddresses(UniValue::VARR);
572-
for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
572+
for (const auto& entry : banMap)
573573
{
574-
CBanEntry banEntry = (*it).second;
574+
const CBanEntry& banEntry = entry.second;
575575
UniValue rec(UniValue::VOBJ);
576-
rec.push_back(Pair("address", (*it).first.ToString()));
576+
rec.push_back(Pair("address", entry.first.ToString()));
577577
rec.push_back(Pair("banned_until", banEntry.nBanUntil));
578578
rec.push_back(Pair("ban_created", banEntry.nCreateTime));
579579
rec.push_back(Pair("ban_reason", banEntry.banReasonToString()));

src/rpc/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
160160
std::set<rpcfn_type> setDone;
161161
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
162162

163-
for (std::map<std::string, const CRPCCommand*>::const_iterator mi = mapCommands.begin(); mi != mapCommands.end(); ++mi)
164-
vCommands.push_back(make_pair(mi->second->category + mi->first, mi->second));
163+
for (const auto& entry : mapCommands)
164+
vCommands.push_back(make_pair(entry.second->category + entry.first, entry.second));
165165
sort(vCommands.begin(), vCommands.end());
166166

167167
JSONRPCRequest jreq(helpreq);

src/serialize.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,8 @@ template<typename Stream, typename K, typename T, typename Pred, typename A>
732732
void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
733733
{
734734
WriteCompactSize(os, m.size());
735-
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
736-
Serialize(os, (*mi));
735+
for (const auto& entry : m)
736+
Serialize(os, entry);
737737
}
738738

739739
template<typename Stream, typename K, typename T, typename Pred, typename A>

src/test/coins_tests.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class CCoinsViewCacheTest : public CCoinsViewCache
8181
// Manually recompute the dynamic usage of the whole data, and compare it.
8282
size_t ret = memusage::DynamicUsage(cacheCoins);
8383
size_t count = 0;
84-
for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) {
85-
ret += it->second.coin.DynamicMemoryUsage();
84+
for (const auto& entry : cacheCoins) {
85+
ret += entry.second.coin.DynamicMemoryUsage();
8686
++count;
8787
}
8888
BOOST_CHECK_EQUAL(GetCacheSize(), count);
@@ -189,15 +189,15 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
189189

190190
// Once every 1000 iterations and at the end, verify the full cache.
191191
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
192-
for (auto it = result.begin(); it != result.end(); it++) {
193-
bool have = stack.back()->HaveCoin(it->first);
194-
const Coin& coin = stack.back()->AccessCoin(it->first);
192+
for (const auto& entry : result) {
193+
bool have = stack.back()->HaveCoin(entry.first);
194+
const Coin& coin = stack.back()->AccessCoin(entry.first);
195195
BOOST_CHECK(have == !coin.IsSpent());
196-
BOOST_CHECK(coin == it->second);
196+
BOOST_CHECK(coin == entry.second);
197197
if (coin.IsSpent()) {
198198
missed_an_entry = true;
199199
} else {
200-
BOOST_CHECK(stack.back()->HaveCoinInCache(it->first));
200+
BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
201201
found_an_entry = true;
202202
}
203203
}
@@ -420,11 +420,11 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
420420

421421
// Once every 1000 iterations and at the end, verify the full cache.
422422
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
423-
for (auto it = result.begin(); it != result.end(); it++) {
424-
bool have = stack.back()->HaveCoin(it->first);
425-
const Coin& coin = stack.back()->AccessCoin(it->first);
423+
for (const auto& entry : result) {
424+
bool have = stack.back()->HaveCoin(entry.first);
425+
const Coin& coin = stack.back()->AccessCoin(entry.first);
426426
BOOST_CHECK(have == !coin.IsSpent());
427-
BOOST_CHECK(coin == it->second);
427+
BOOST_CHECK(coin == entry.second);
428428
}
429429
}
430430

src/validation.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,8 +3249,8 @@ void PruneOneBlockFile(const int fileNumber)
32493249
{
32503250
LOCK(cs_LastBlockFile);
32513251

3252-
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
3253-
CBlockIndex* pindex = it->second;
3252+
for (const auto& entry : mapBlockIndex) {
3253+
CBlockIndex* pindex = entry.second;
32543254
if (pindex->nFile == fileNumber) {
32553255
pindex->nStatus &= ~BLOCK_HAVE_DATA;
32563256
pindex->nStatus &= ~BLOCK_HAVE_UNDO;
@@ -3802,8 +3802,8 @@ bool RewindBlockIndex(const CChainParams& params)
38023802
// Reduce validity flag and have-data flags.
38033803
// We do this after actual disconnecting, otherwise we'll end up writing the lack of data
38043804
// to disk before writing the chainstate, resulting in a failure to continue if interrupted.
3805-
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
3806-
CBlockIndex* pindexIter = it->second;
3805+
for (const auto& entry : mapBlockIndex) {
3806+
CBlockIndex* pindexIter = entry.second;
38073807

38083808
// Note: If we encounter an insufficiently validated block that
38093809
// is on chainActive, it must be because we are a pruning node, and
@@ -4078,8 +4078,8 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams)
40784078

40794079
// Build forward-pointing map of the entire block tree.
40804080
std::multimap<CBlockIndex*,CBlockIndex*> forward;
4081-
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4082-
forward.insert(std::make_pair(it->second->pprev, it->second));
4081+
for (auto& entry : mapBlockIndex) {
4082+
forward.insert(std::make_pair(entry.second->pprev, entry.second));
40834083
}
40844084

40854085
assert(forward.size() == mapBlockIndex.size());

0 commit comments

Comments
 (0)