1

I have a vector of People object stored somewhere

std::vector<People> people;

Now I find some special people and get their reference, then get their pointer and store in a vector:

std::vector<People*> newlist;

The way I get this list:

People& specialPeople = getSomeSpecialGuyFrom(people); // get a referece 
newlist.push_back(&specialPeople);
// New I sort the newlist by some property, assuming it's not empty:
std::nth_element(newlist.begin(),
                 newlist.begin() + 1,
                 newlist.end(),
                 [](const People* p1,
                    const People* p1) {
                   return p1->reputation > p2->reputation;
                 });
newlist[0]->name = "Best one";

Note that I keep the original people vector not changed. The point of storing pointers is to make the sort cheap. Now my question is, the last assignment, is it changing the original people vector? From the way I read the code it should, but in practice I see opposite result? Sorry I can't use the real code here...

WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30
  • 4
    If your **practice** is different from what you expect to see, post them codes. Right now we have no idea what went wrong. On a side note, storing pointers to elements in vector is rarely a good idea, as those could become invalidated. – SergeyA Mar 21 '19 at 17:27
  • 1
    It should change `People` in the `people` vector. If this isn't happening then something outside of the scope of what you've shown is changing the result. You'll have to share a [MCVE]. – François Andrieux Mar 21 '19 at 17:44
  • 1
    Even if it would work for your given case, it is really bad practice to get a reference from `std::vector` and store its pointer in another vector. It could be ok if `std::vector` is `const` for the lifetime of `std::vector newlist;` so that it is guaranteed to not change. – t.niese Mar 21 '19 at 17:45
  • @WhatABeautifulWorld My comment still holds, the error doesn't appear to be in the code shown. Edit : Does `getSomeSpecialGuyFrom` take the vector by reference? – François Andrieux Mar 21 '19 at 17:45
  • @t.niese But the point is to find which `People` to change, so `people` can't be `const`. – François Andrieux Mar 21 '19 at 17:46
  • @FrançoisAndrieux The vector being const and the People being const are two different things. Ish. It is true that the OP's approach is arguably dangerous, though it "should work". – Lightness Races in Orbit Mar 21 '19 at 17:47
  • @LightnessRacesinOrbit But if `people` is `const` you can't get a mutable references to it's `People`, there would be no way to reach a point where `newlist[0]->name = "Best one";` is allowed. Edit : I mean, you could `const_cast` and it would be defined but then that's clearly not the intention. – François Andrieux Mar 21 '19 at 17:48
  • Is likely you are returning a reference which is not stored in std::vector. Don't know since you haven't posted more code. Compare the addresses stored in both vectors and make sure they match. – AdvSphere Mar 21 '19 at 19:05
  • @FrançoisAndrieux Oh yeah :D – Lightness Races in Orbit Mar 22 '19 at 11:18

1 Answers1

2

the last assignment, is it changing the original people vector?

It depends.

Answer is yes if and only if getSomeSpecialGuyFrom returns a reference to an object in the original people vector and the reference/pointer hasn't been invalidated.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    In my view, this question doesn't deserve an answer. OP is claiming "From the way I read the code it should, but in practice I see opposite result", but doesn't give us MCVE. – SergeyA Mar 21 '19 at 17:28