85

I have a vector vec of structures. Such a structure has elements int a, int b, int c. I would like to assign to some int var the element c, from the last structure in a vector. Please can you provide me with this simple solution? I'm going circle in line like this:

var = vec.end().c;
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
berndh
  • 1,355
  • 4
  • 14
  • 19

8 Answers8

138

The immediate answer to your question as to fetching access to the last element in a vector can be accomplished using the back() member. Such as:

int var = vec.back().c;

Note: If there is a possibility your vector is empty, such a call to back() causes undefined behavior. In such cases you can check your vector's empty-state prior to using back() by using the empty() member:

if (!vec.empty())
   var = vec.back().c;

Likely one of these two methods will be applicable for your needs.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Though, I can see a possiblity to satisfy both of us. You could just propose `vec.back()`, but explain to him that it only works for non-empty vectors and if he isn't sure about this state of the vector at this point of the code, a check for `vec.empty()` would be neccessary. – Christian Rau Jan 11 '13 at 10:12
  • *"three lines of code reminding someone non-native to standard containers..."* - It doesn't remind him to think about indeterminate state, not if he is really a newbie. He'll just take that for granted and start to spam `std::vector::empty`s all over the place. Like said, the answer could be worded much more defensively. But Ok, it's your decision, granted. – Christian Rau Jan 11 '13 at 10:14
  • @ChristianRau thats not the indeterminate state avoidance i was talking about. the `var` preinitialize was what I was referring to. And as I said, you may well be right that neither are warranted for participation in his final usage, but imho, both are warranted as a reminder to consider. Thats all, nothing beyond that. – WhozCraig Jan 11 '13 at 10:16
  • 1
    @WhozCraig I understand that, but this is not a reminder, but a definite answer to his question. That is the only problem I have. It is good to teach such safety and cleannliness considerations to newbies, but likewise do you have to be especially careful to not indoctrinate them with dogmatic behaviour, but to think about the neccessity of such checks in each and every situation anew. – Christian Rau Jan 11 '13 at 10:19
  • 1
    @ChristianRau Now *that* I actually buy into. I'm always looking to improve answer-skills, and when you put it like that, It makes much more sense to me. So the code isn't the problem, its the assumption its always *the* solution. I can agree that it could be seen as such, and should be better explained. I shall update the answer in due course. – WhozCraig Jan 11 '13 at 10:22
  • @ChristianRau ok then. updated. hope it is more compelling. I'm going to start backing out comments I don't think are contributory to the OP's question. Thank you for your patience. – WhozCraig Jan 11 '13 at 10:31
  • 1
    Much better. +1 for not proposing a crystal ball answer anymore. And +1 for providing additional information not immediately relevant for the question but improving the OP's overall awareness. ;) – Christian Rau Jan 11 '13 at 10:32
21

vec.end() is an iterator which refers the after-the-end location in the vector. As such, you cannot deference it and access the member values. vec.end() iterator is always valid, even in an empty vector (in which case vec.end() == vec.begin())

If you want to access the last element of your vector use vec.back(), which returns a reference (and not iterator). Do note however that if the vector is empty, this will lead to an undefined behavior; most likely a crash.

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
10

Use back():

var = vec.back().c;
Veger
  • 37,240
  • 11
  • 105
  • 116
5

Try this: var = vec.back().c;

Also you may modify your code like:

var = vec.rbegin()->c;

In both versions first make sure that the vector is not empty!

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

var = vec.back().c; is what you want.

end() returns the iterator (not an element) past-the-end of the vector. back() returns a reference to the last element. It has a counterpart front() as well.

Chowlett
  • 45,935
  • 20
  • 116
  • 150
1

You can simply use back as it returns a reference to the last element.
var = vec.back().c

mkaes
  • 13,781
  • 10
  • 52
  • 72
0

You can use the std:vector<T>:back() function, e.g. vec.back().c. See http://www.cplusplus.com/reference/vector/vector/back/

Lars
  • 1,426
  • 10
  • 25
0

The following code works for me.

int var;
var =  *(std::end(bar)-1);
std::cout<<var<<std::endl;
jian
  • 4,119
  • 1
  • 17
  • 32