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
Check for Installed Packages Before Running Install.Packages()
Data.Table with Two String Columns of Set Elements, Extract Unique Rows with Each Row Unsorted
Function to Calculate R2 (R-Squared) in R
Using Multiple Criteria in Subset Function and Logical Operators
Operator == Inconsistent in Logical Columns in Data.Table
How to Add a Ggplot2 Subtitle with Different Size and Colour
Plot 4 Curves in a Single Plot with 3 Y-Axes
How to Generate a Matrix of Combinations
How to Remove Unicode <U+00A6> from String
How to Wait for a Keypress in R
Dplyr Mutate Rowwise Max of Range of Columns
Using Rcpp Within Parallel Code via Snow to Make a Cluster
How to Read Only Lines That Fulfil a Condition from a CSV into R
Collapsing Data Frame by Selecting One Row Per Group
How to Deal with "'Somefunction' Is Not an Exported Object from 'Namespace:Somepackage'" Error
Ggplot2: Facet_Wrap Strip Color Based on Variable in Data Set
How to Place Grobs with Annotation_Custom() at Precise Areas of the Plot Region