tags:

views:

77

answers:

3

dear experts,

This question is an extension of this question I asked.

I have a std::vector vec_B.which stores instances of class Foo. The order of elements in this vector changes in the code.

Now, I want to access the value of the current "last element" or current 'nth' element of the vector. If I use the code below to get the last element using getLastFoo() method, it doesn't return the correct value.

For example, to begin with the last element of the vector has Foo.getNumber() = 9. After sorting it in descending order of num, for the last element, Foo.getNumber() = 0.

But with the code below, it still returns 9.. that means it is still pointing to the original element that was the last element.

What change should I make to the code below so that "lastFoo" points to the correct last element?

class Foo {      
      public:
             Foo(int i);
             ~Foo(){};
             int getNum();
      private:
           int num;    
};
Foo:Foo(int i){
   num = i;
}
int Foo::getNum(){
  return num;
}
class B {
      public:             
             Foo* getLastFoo();
             B();
             ~B(){};             
      private:
             vector<Foo> vec_B;
};

B::B(){

    int i;
    for (i = 0; i< 10; i++){
        vec_B.push_back(Foo(i));
    }
    // Do some random changes to the vector vec_B so that elements are reordered. For 
    // example rearrange elements in decreasing order of 'num'
    //...
    }

Foo* B::getLastFoo(){
    return &vec_B.back();
};
int main(){
    B b;
    Foo* lastFoo;   
    lastFoo = b.getLastFoo()
    cout<<lastFoo->getNumber();
    return 0;
}
A: 

just use vec_B.back() and not &vec_B.back() to get a reference to the last element in the vector (you don't need a pointer do you?)

Stéphane
@Stephane: actually I need a pointer to that instance of Foo. `Foo* lastFoo` is a pointer to that element. So, that I can use lasFoo->getNumber(). So just returning `vec_B.back()` gives errors
memC
@memC - if you had a reference, you would then use `lastFoo.getNumber()`
R Samuel Klatchko
A: 

Perhaps you want ’return &vec_B.back();’?

Managu
memC
Apologies on the formatting -- apparently iPads don't do back ticks...
Managu
A: 

This works for me:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Foo 
{
public:
    Foo(int n) 
        : num(n) 
    {}

    int num;
};

bool cmp(const Foo& a, const Foo& b)
{
    // sort in descending order
    return b.num < a.num;
}

int main()
{
    vector<Foo> v;
    for (int i=0; i<10; ++i) {
        v.push_back(Foo(i));
    }
    sort(v.begin(), v.end(), &cmp);

    Foo* vf = &v.back();
    cout << vf->num << endl;
    return 0;
}

should be equivalent to your sample, and prints 0 on my machine when run. Fix and check your code sample and compare it to your actual code, and you may see the problem.

One note: you mention you're checking the last element before and after you do operations on your vector. If you're holding onto a pointer into the vector and not calling v.back() again after mutating the vector, be very careful that you're not doing an operation (such as insertion) that would cause the vector to reallocate. If that happens, your pointer into the vector will be a dangling pointer, and who knows what you'll get when you dereference it!

Owen S.