-1

There is no back() method in boost::numeric::ublas::vector, is it possible to emulate it in user code with preprocessor macro defining somehow array_name[array_name.size()-1]?

array_name[i].rbegin()->operator[] (i) = 1.0 or array_name[i][array_name[i].size()-1][i] = 1.0 hard to read, array_name[i].back()[i] = 1.0 easy to read, that why i thinking to emulate back() method.

morte
  • 325
  • 1
  • 10

2 Answers2

2

Consider to use std::array<> instead of C-arrays. Then you'll have array_name.back() as well. Note, that std::array<> comes with no overhead to C-arrays.

If it really has to be C-arrays with static size, then

array_name[(sizeof(array_name) / sizeof(array_name[0])) - 1]

should do the trick, though I have not yet tested. Dynamically sized arrays (those allocated with new or malloc()) don't carry any length information. You can not determine their last element, without having stored the length somewhere else.

BTW.: boost::numeric::ublas::vector does have size(), so you can do

vector_name[vector_name.size() - 1]

It also has reverse iterators. You can do

*(vector_name.rbegin())

to get the value of the last element.

cdonat
  • 2,748
  • 16
  • 24
  • Question in not actually about how to access last element, it is about how to do that with trim syntax – morte Oct 21 '15 at 10:02
  • 1
    @morte How about creating a free standing function and fill it with the suggestion of this answer? (don't use a macro for this) – Brandlingo Oct 21 '15 at 11:14
  • @morte is `*(vector_name.rbegin())` or `array_name.back()` still too complicated? – cdonat Oct 21 '15 at 18:28
  • @cdonat Yes, imagine accessing last elements in 3D-vector, there is no back() method. – morte Oct 23 '15 at 09:21
  • @morte what is the last element of a 3D-Vector? I guess it is the third one, so it is `vector_name[2]`. If you are writing generic code, that does not know the dimensions of the vector it works with, then `*(vector_name.rbegin())`, or `vector_name[vector_name.size() - 1]` should give you, what you need. To be more generic, you can of course overload a function, e.g. `last_element(std::array)` and `last_element(boost::numeric::ublas::vector)`. Then your generic code call `last_element()` and based on the type of vector/array/etc. you have, it will call the correct implementation. – cdonat Oct 23 '15 at 09:53
0

What you want is a free standing function which supplies the syntactic sugar you are asking for:

template <typename C>
typename C::const_reference back(const C & container)
{
    return *container.rbegin();
}

This will work for all classes with a rbegin() method and const_reference nested type. Add a second overload for non-const references if needed.

See this live on Coliru

Brandlingo
  • 2,817
  • 1
  • 22
  • 34