0

So, I have a vector that is either full of integers. Lets call this vector Vect. I have my code in main.cpp and VectorList.h, and cannot change that fact. In VectorList.h one of my functions is:

void insertAtFront( const int & );

Now where I'm encountering trouble, I know I can add the integer to the start of the vector using std::vector.insert() function. But, insertAtFront does not have access to the vector itself, however, this is the only data member in VectorList.h:

vector< int > *vList

So, my question is how can I add a value to the beginning of a vector Vect using only this pointer *vList?

My first idea was something like this:

&vList.insert(&vList.begin(), 1, &value)    // with value being the input integer

but that doesn't work :/ any suggestions?

Mysticial
  • 464,885
  • 45
  • 335
  • 332
Patrick Johnston
  • 101
  • 1
  • 1
  • 12

2 Answers2

3

If you have a pointer to a vector, then you would need to use the -> operator. Using the & operator in this case will give you the address of the return value of vList.begin(). That won't work too well, considering you can't use the . operator on a pointer to begin with. Instead, you need to dereference the pointer. Try:

vList->insert(vList->begin(), value);

Edit: I'm not sure why you would need the middle argument in this case. You should be fine omitting it. I have done so in the line of code I wrote here.

Chris Parton
  • 1,052
  • 9
  • 16
  • Actually `&` has lower precedence than `.` so it will give you the address of the return value of `insert` – Seth Carnegie Dec 02 '11 at 02:48
  • 2
    Good point, thanks for pointing that out. Now we're even haha. – Chris Parton Dec 02 '11 at 02:50
  • Oh thanks for pointing that out, I have it because in other functions I insert values at various points in the vector. – Patrick Johnston Dec 02 '11 at 02:55
  • 1
    @PatrickJohnston if you're only inserting one copy of `value`, you don't need the middle argument. You only ever need it if you want to insert a bunch of `value` at once (the number that you pass as the middle argument will be the number of copies that get inserted). – Seth Carnegie Dec 02 '11 at 02:56
2

Assuming the pointer points to a valid vector, just dereference the vector and call insert:

vList->insert(vList->begin(), value);
// same thing as: (*vList).insert(vList->begin(), value);
// same thing as: (*vList).insert((*vList).begin(), value);
// same thing as: vList->insert((*vList).begin(), value);
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • You need to dereference `vList` in `vList.begin()` also. – Chris Parton Dec 02 '11 at 02:47
  • 2
    `(&var) // get a pointer to var`, `(*var) // get what var points at`, `var.func() // call func() member of var`, `var->func() // call func on what-is-pointed-to-by-var`, `(*var).func() // equivalent to var->func()` – rvalue Dec 02 '11 at 02:53
  • 1
    @rvalue: Nice explanation. I just want to point out that `(*var)` will only work if `var` is a pointer. – Chris Parton Dec 02 '11 at 03:08