Vector MADNESS

Started by
8 comments, last by Khatharr 11 years, 1 month ago

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
Advertisement

Use std::vector::back().

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

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

J-GREEN

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

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().

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

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.

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 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.


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.

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.

This topic is closed to new replies.

Advertisement