• Advertisement
Sign in to follow this  

Vector MADNESS

This topic is 1772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

 

 

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement