adrianmcmenamin
adrianmcmenamin

Reputation: 1129

C++: reference to a map

I have this code (apologies for the debug stuff stuck in it)

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;
}

This code generates output like this:

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

In other words, it correctly spots when an element in the map has an older time stamp, but does not seem to update the reference to refer to that instance.

What have I got wrong here?

Upvotes: 1

Views: 72

Answers (2)

R Sahu
R Sahu

Reputation: 206717

You have:

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

and a few lines later,

        pageToKill = itOld->second;

The above line replaces the value of the what pageToKill references -- the first value in the map.

You could try:

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

and later...

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

Upvotes: 0

fredoverflow
fredoverflow

Reputation: 263350

but does not seem to update the reference to refer to that instance.

You cannot update references in C++.

When you write:

pageToKill = itOld->second;

You mutate the object which was bound to the reference pageToKill during its initialization.

If you want a "reference" that can be rebound, use a pointer.

Upvotes: 3

Related Questions