• Create Account

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1greenzone  Members

676
Like
0Likes
Like

Posted 17 March 2013 - 03:43 PM

Ok, what is the best way to access the last element of the vector i just populated? So in the same function I push a matrix on to that vector and I then want to use that matrix and multiply it against another matrix. what would be the best approach of doing this?

vector<Matrix> MatStack;

MatStack.push_back(newMatrix);

MatStack[MatStack.Size()] * differentMatrix;

or

MatStack[MatStack.Size() - 1] * differentMatrix;

J-GREEN

Greenpanoply

### #2Brother Bob  Moderators

10108
Like
8Likes
Like

Posted 17 March 2013 - 03:52 PM

POPULAR

Use std::vector::back().

MatStack.back() * differentMatrix


### #3SiCrane  Moderators

11528
Like
8Likes
Like

Posted 17 March 2013 - 04:09 PM

POPULAR

A vector can only be indexed from 0 to size() - 1. vec[vec.size()] is not a valid expression for a vector.

### #4greenzone  Members

676
Like
0Likes
Like

Posted 17 March 2013 - 04:14 PM

So in theory I could write vec[vec.size()-1]    ?

J-GREEN

Greenpanoply

### #5SiCrane  Moderators

11528
Like
8Likes
Like

Posted 17 March 2013 - 04:24 PM

POPULAR

That would be legal as long as size() is 1 or more.

### #6Paradigm Shifter  Members

5832
Like
16Likes
Like

Posted 17 March 2013 - 06:09 PM

POPULAR

Do this

if(!MatStack.empty())
{
someMatrix = MatStack.back() * differentMatrix;
}


That way you can change the underlying container to something other than a vector if you wish.

EDIT: And it's not really MADNESS is it. MADNESS would be calling your collection of vectors HectorTheVectorCollector and a function to select a collection of vectors HectorTheVectorCollectorSelector().

Edited by Paradigm Shifter, 17 March 2013 - 06:11 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #7ifthen  Members

820
Like
4Likes
Like

Posted 18 March 2013 - 01:23 AM

As has been said, use std::vector::back() or vec[vec.size()-1], but ALWAYS check if the vector is not empty. Using either of those without checking for non-emptiness results in undefined behaviour and it will most likely overwrite sizeof(vectype) bytes before the start of vector data, corrupting the memory or causing a segmentation fault.

vec.at(vec.size()-1) throws an exception if the vector is empty, but it sacrifices speed and does not solve the problem except for alerting you instead of corrupting memory.

Paradigm Shifter: You, sir, have made my day.

Edited by ifthen, 18 March 2013 - 01:24 AM.

### #8SiCrane  Moderators

11528
Like
2Likes
Like

Posted 18 March 2013 - 10:08 AM

I wouldn't go so far as to say always check (and certainly not with all caps). It's safe to access back() or vec[vec.size() - 1] right after using push_back() on the vector or using other operations that add elements or set the vector to a known non-zero size. Since std::vector uses exceptions to signal errors for things like insertions, control flow simply won't reach an illegal access in those cases. If feeling paranoid you can add a debug assertion for those cases when you do assume the vector isn't empty, but most standard library implementations already put a debug assertion in operator[] for vector.

### #9GuardianX  Members

1590
Like
0Likes
Like

Posted 18 March 2013 - 12:10 PM

I wouldn't go so far as to say always check (and certainly not with all caps). It's safe to access back() or vec[vec.size() - 1] right after using push_back() on the vector or using other operations that add elements or set the vector to a known non-zero size. Since std::vector uses exceptions to signal errors for things like insertions, control flow simply won't reach an illegal access in those cases. If feeling paranoid you can add a debug assertion for those cases when you do assume the vector isn't empty, but most standard library implementations already put a debug assertion in operator[] for vector.

I strongly agree with this.

It can be a little overhead in checking vector size validity in situations where you absolutely sure that it's size cannot actually be zero.

### #10Khatharr  Members

7718
Like
0Likes
Like

Posted 18 March 2013 - 11:20 PM

I wouldn't go so far as to say always check (and certainly not with all caps). It's safe to access back() or vec[vec.size() - 1] right after using push_back() on the vector or using other operations that add elements or set the vector to a known non-zero size. Since std::vector uses exceptions to signal errors for things like insertions, control flow simply won't reach an illegal access in those cases. If feeling paranoid you can add a debug assertion for those cases when you do assume the vector isn't empty, but most standard library implementations already put a debug assertion in operator[] for vector.

I strongly agree with this.

It can be a little overhead in checking vector size validity in situations where you absolutely sure that it's size cannot actually be zero.

This is true. I'd just add that since someone else (or you) may come along later and not notice the logical guarantee, it may be a good idea to add a comment in cases where it's not immediately apparent from looking at the code.

Edited by Khatharr, 18 March 2013 - 11:21 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.