Commit 40fd93e0b67e39334ec9098763f4b6eae29b9615

Authored by Jordi Inglada
1 parent feb71130
Exists in master

ENH: add a function to get the last element of a Seq

Showing 1 changed file with 11 additions and 0 deletions   Show diff stats
include/cbutils.h
... ... @@ -24,6 +24,7 @@
24 24 #include <random>
25 25 #include <cassert>
26 26 #include <chrono>
  27 +#include <iomanip>
27 28 namespace cbutils
28 29 {
29 30 // ----------------- String manipulation ---------------------------------------
... ... @@ -38,6 +39,7 @@ std::vector&lt;std::string&gt; split(const std::string&amp; str,
38 39 boost::token_compress_on);
39 40 return tokens;
40 41 }
  42 +
41 43 } //end ns string
42 44 // ----------------- File IO ---------------------------------------------------
43 45 namespace file
... ... @@ -87,6 +89,15 @@ Seq tail(const Seq&amp; s)
87 89 std::copy(head, std::end(s), std::back_inserter(result));
88 90 return result;
89 91 }
  92 +//Returns the last element of a sequence
  93 +template <typename Seq>
  94 +auto last(const Seq& s)
  95 +{
  96 + auto element = s.cend();
  97 + --element;
  98 + return *element;
  99 +}
  100 +//python-like range function
90 101 template <typename T>
91 102 std::vector<T> range(T minvalue, T maxvalue, T step=1)
92 103 {
... ...