2014-09-28 71 views
1

我有此代碼(道歉卡在它的調試的東西)C++:參照地圖

PartialPage& DoubleTree::oldestPage() 
{ 
    PartialPage& pageToKill = pageTree.begin()->second; 
    long timeToKill = pageTree.begin()->second.getTime(); 
    map<long, PartialPage>::iterator itOld; 
    cerr << "Page " << pageToKill.getPageNumber() << " with time " << timeToKill << endl; 
    for (itOld = pageTree.begin(); itOld != pageTree.end(); itOld++) { 
     cerr << itOld->first << " : " << itOld->second.getTime() << ","; 
     if (itOld->second.getTime() < timeToKill) { 
      timeToKill = itOld->second.getTime(); 
      pageToKill = itOld->second; 
      cerr << endl << "Replaced with " << pageToKill.getPageNumber() << " with time " << timeToKill << endl; 
     } 
    } 
    cerr << "Tree has " << pageTree.size() << " elements and we picked page " <<  pageToKill.getPageNumber() << endl; 
    return pageToKill; 
} 

此代碼生成輸出是這樣的:

Page 32788 with time 1411932643167898 
32788 : 1411932643167898,32790 : 1411932640129861, 
Replaced with 32788 with time 1411932640129861 
32791 : 1411932640456679,32792 : 1411932643042340,32794 : 1411932640172263,32795 : 1411932640203512,32796 : 1411932640187651,32797 : 1411932640189676,32798 : 1411932640379028,32799 : 1411932640077458, 
Replaced with 32788 with time 1411932640077458 
32800 : 1411932639850996, 
Replaced with 32788 with time 1411932639850996 
32801 : 1411932641733754,32802 : 1411932640378079,32803 : 1411932640392161,32804 : 1411932640427125,32811 : 1411932639429403, 
Replaced with 32788 with time 1411932639429403 
32812 : 1411932639552400,32813 : 1411932640426173,32814 : 1411932639791659,32816 : 1411932640393999,32817 : 1411932643165226,32818 : 1411932640074872,32819 : 1411932640200932,32820 : 1411932640189102,32822 : 1411932640074734,32823 : 1411932643037665,32824 : 1411932641725923,32825 : 1411932639860631,32838 : 1411932643167861,32839 : 1411932643019133,32840 : 1411932639854076,32878 : 1411932640058203,32897 : 1411932639969865,32909 : 1411932639889327,32916 : 1411932640058292,32945 : 1411932640071503,32974 : 1411932639957135,32984 : 1411932639958741,32988 : 1411932639959924,32991 : 1411932639961069,32992 : 1411932639974983,32993 : 1411932639955969,33011 : 1411932639871191,33014 : 1411932639874551,33015 : 1411932639876698,33016 : 1411932639906830,33020 : 1411932639872242,33030 : 1411932640058627,33067 : 1411932640059679,33076 : 1411932640061764,33080 : 1411932640063085,33082 : 1411932640064271,33084 : 1411932640076206,33085 : 1411932640060746,33104 : 1411932643135052,33105 : 1411932643134970,33106 : 1411932640432242,33107 : 1411932640423850,33108 : 1411932640426904,33861 : 1411932643135173,33862 : 1411932640433903,33863 : 1411932643046850,33864 : 1411932643126930,33865 : 1411932641730018,37962 : 1411932643167875,38987 : 1411932643135268,38988 : 1411932643018577,38989 : 1411932641563440,38990 : 1411932641219821,38997 : 1411932640316266,40511 : 1411932643018354,40512 : 1411932639928912,40514 : 1411932643019102,40522 : 1411932640356638,41583 : 1411932643019015,41584 : 1411932640009668,41585 : 1411932640033429,41594 : 1411932642524801,41595 : 1411932643019820,41596 : 1411932643023008,41597 : 1411932642353290,41598 : 1411932642417264,41599 : 1411932642322596,41600 : 1411932643023238,41601 : 1411932642782395,41602 : 1411932641662080,41603 : 1411932642651784,41604 : 1411932642154134,41605 : 1411932643042188,41606 : 1411932641115901,41607 : 1411932641941591,41609 : 1411932641818136,41610 : 1411932642433396,41611 : 1411932642433411,41613 : 1411932642889449,41614 : 1411932642324828,41615 : 1411932641640108,41616 : 1411932642181100,41618 : 1411932641616281,41619 : 1411932641324974,41621 : 1411932642207235,41622 : 1411932643037599,41624 : 1411932642225616,41625 : 1411932642356076,41626 : 1411932642113908,41627 : 1411932642092238,41628 : 1411932641488112,41629 : 1411932641674993,41630 : 1411932641850897,41631 : 1411932641987161,41632 : 1411932642220932,41633 : 1411932642621807,41634 : 1411932643165195,41635 : 1411932641634666,41636 : 1411932641739212,41638 : 1411932643038514,41639 : 1411932643026740,41640 : 1411932642354417,41641 : 1411932640509806,41642 : 1411932640546388,41643 : 1411932640584662,41644 : 1411932640624924,41645 : 1411932640661431,41646 : 1411932640699992,41647 : 1411932640736103,41648 : 1411932640775231,41649 : 1411932640813379,41650 : 1411932640850822,41651 : 1411932640897932,41652 : 1411932640947271,41653 : 1411932640999481,41654 : 1411932641567729,41655 : 1411932643038330,43492 : 1411932641117114,43493 : 1411932640556557,43494 : 1411932640609545,43495 : 1411932640679172,43496 : 1411932640760872,43497 : 1411932640834402,43498 : 1411932641139833,43499 : 1411932643038068,43500 : 1411932640999450,43501 : 1411932641004273,43502 : 1411932641032261,43503 : 1411932641034647,33546237 : 1411932640172153,33546238 : 1411932643167893,33546239 : 1411932640284429,33546240 : 1411932639439851,33546241 : 1411932640431664,Tree has 150 elements and we picked page 32788 

換句話說,它正確地當map中的某個元素具有較早的時間戳記,但似乎沒有更新引用以引用該實例時,會出現斑點。

我在這裏弄錯了什麼?

回答

3

但似乎沒有更新引用來引用該實例。

您無法更新C++中的引用。

當你寫:

pageToKill = itOld->second; 

您變異這是必然的初始化過程中參考pageToKill的對象。

如果你想要一個可以被反彈的「引用」,可以使用一個指針。

+0

人,我的C++很生疏,因爲這顯示。謝謝 – adrianmcmenamin 2014-09-28 19:44:56

0

您有:

PartialPage& pageToKill = pageTree.begin()->second; 

和幾行後,

 pageToKill = itOld->second; 

上面一行替換什麼pageToKill參考價值 - 在地圖中的第一個值。

你可以嘗試:

map<long, PartialPage>::iterator pageToKillIter = pageTree.begin(); 

後來......

 pageToKillIter->second = itOld->second; // Replace the value of 
               // what pageToKillIter points to. 
     pageToKillIter = itOld; // Let pageToKillIter point to the new iterator 
           // location 
+0

@Galik,'cerr << endl <<「這行代替了」...'讓我想也許他們想。 – 2014-09-28 19:59:05