How to Access the Last Value in a Vector

How can I get a field from the last element of a vector in C++?

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.

How to get the second to last element in a Vector in R

It seems you want to leave one element from the vector A. You can simply write B=tail(A,-1)
where -1 leaves the first element.

How do I get the last element of a list?

some_list[-1] is the shortest and most Pythonic.

In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)], which gives you the first element.

You can also set list elements in this way. For instance:

>>> some_list = [1, 2, 3]
>>> some_list[-1] = 5 # Set the last element
>>> some_list[-2] = 3 # Set the second to last element
>>> some_list
[1, 3, 5]

Note that getting a list item by index will raise an IndexError if the expected item doesn't exist. This means that some_list[-1] will raise an exception if some_list is empty, because an empty list can't have a last element.

Extract last value from vector in data frame

A base R solution with aggregate() and tail()

aggregate(VALUE~ ID, DATA_TEST, tail, 1)

# ID VALUE
# 1 01380926325248 300
# 2 03740270423222 200

or by dplyr package.

library(dplyr)

option 1: summarise() + last()

DATA_TEST %>%
group_by(ID) %>%
summarise(VALUE = last(VALUE))

option 2: slice_tail() <=> slice(n())

DATA_TEST %>%
group_by(ID) %>%
slice_tail()

Access second to last element of array

I'm also surprised that doesn't work. Are you sure you tried it on an array with at least two elements?

You could also try using the length() function:

last = array[length(array)]
secondToLast = array[length(array)-1]

Vector of structure returns only last value

NA& del(*new NA);
while(q.nextquery())
dels.insert(&del);

Obviously, you are creating only one del object, but you insert it many times in the same vector. At the end, all you vector's entries will point to the same object.

What you want is probably to create a new del object for each entry in your vector. therefore, put the creation statement inside the loop.

func(Vector<NA> &dels)
{
while(q.nextquery())
{
while(q.nextrow())
{
NA& del(*new NA); // <-- Here
q.bind(del.element1);
q.bind(del.element2);
dels.insert(&del);
}
return dels.entries()
}

Accessing the last element of a Vec or a slice

And just after posting the question, the answer appears to be obvious:

fn top (&mut self) -> Option<&f64> {
match self.len() {
0 => None,
n => Some(&self[n-1])
}
}

I.e. the usize was never the problem - the return type of top() was.



Related Topics



Leave a reply



Submit