SERVING THE QUANTITATIVE FINANCE COMMUNITY

  • 1
  • 2
  • 3
  • 4
  • 5
  • 15
 
User avatar
lballabio
Topic Author
Posts: 983
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- STL tricks

September 29th, 2005, 10:36 am

Lately it was discussed at length how to check in Excel that a sequence of numbers is decreasing.In C++, the code for checking whether a vector is (strictly) decreasing can be written with an explicit loop, as in the attached code.However, a truly generic solution would use iterators and have a signature such as:template <typename T> decreasing(T begin, T end);but in this case, writing an explicit loop is a bit more difficult. The case begin == end should be checked explicitly; moreover, increasing begin at each step introduces the need to keep track of the previous element (as referring to it as *(begin-1) is only possible for random access iterators, and we want the function to be more generic than that.)Fortunately, it is possible to use existing STL facilities to write our function. The result does not contain explicit loops and is only 1 or 2 lines long.Anyone willing to try?Luigi
 
User avatar
madmax
Posts: 1093
Joined: October 31st, 2003, 9:56 am

C++ quiz --- STL tricks

September 29th, 2005, 11:08 am

template <typename T> bool decreasing(T begin, T end){ return ( adjacent_find_if( begin, end, less_equal<T>() )==end ); }
 
User avatar
lballabio
Topic Author
Posts: 983
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- STL tricks

September 29th, 2005, 11:26 am

Close, but no cigar.less_equal<T> won't work---T is the iterator type, not the underlying value type.
 
User avatar
lballabio
Topic Author
Posts: 983
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- STL tricks

September 29th, 2005, 11:27 am

And there's no adjacent_find_if in the STL.
 
User avatar
madmax
Posts: 1093
Joined: October 31st, 2003, 9:56 am

C++ quiz --- STL tricks

September 29th, 2005, 12:00 pm

ok, ok template <typename T> bool decreasing(T begin, T end){ return ( adjacent_find( begin, end, less_equal<typename T::value_type>() )==end );}
 
User avatar
lballabio
Topic Author
Posts: 983
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- STL tricks

September 29th, 2005, 12:25 pm

Much closer, but what if T is a built-in? (say, a simple double*)
 
User avatar
madmax
Posts: 1093
Joined: October 31st, 2003, 9:56 am

C++ quiz --- STL tricks

September 29th, 2005, 12:30 pm

the standard provides specializations for iterator_traits<T*> and iterator_traits<const T*>
 
User avatar
lballabio
Topic Author
Posts: 983
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- STL tricks

September 29th, 2005, 1:03 pm

Yes, but you have to actually use iterator_traits in order to take advantage of their specialization
 
User avatar
madmax
Posts: 1093
Joined: October 31st, 2003, 9:56 am

C++ quiz --- STL tricks

September 29th, 2005, 1:23 pm

QuoteOriginally posted by: lballabioYes, but you have to actually use iterator_traits in order to take advantage of their specialization vector<double> myVec...decreasing(myVec.begin(),myVec.end());I don't need to use iterator_traits because it is instantiated automaticallySo you probably mean if I use it like this:double myArray[4];decreasing(myArray,myArray+4);so then template <typename T> bool decreasing(T begin, T end){return ( adjacent_find( begin, end, less_equal<typename iterator_traits<T>::value_type>() )==end );} should do the trick.You are right the algo should work on arrays as well. I didn't pay attention to that, I don't even remember the last time I used an array if I ever did.
Last edited by madmax on September 28th, 2005, 10:00 pm, edited 1 time in total.
 
User avatar
lballabio
Topic Author
Posts: 983
Joined: January 19th, 2004, 12:34 pm

C++ quiz --- STL tricks

September 29th, 2005, 1:44 pm

QuoteOriginally posted by: madmaxvector<double> myVec...decreasing(myVec.begin(),myVec.end());I don't need to use iterator_traits because it is instantiated automaticallyNo, you don't need to use it because vector iterators have a value_type typedef.There's no such thing as automatic iterator_traits invocation.QuoteSo you probably mean if I use it like this:double myArray[4];decreasing(myArray,myArray+4);so then template <typename T> bool decreasing(T begin, T end){return ( adjacent_find( begin, end, less_equal<typename iterator_traits<T>::value_type>() )==end );} should do the trick.Correct. You win the right to post next quiz QuoteYou are right the algo should work on arrays as well. I didn't pay attention to that, I don't even remember the last time I used an array if I ever did.I don't use them in production code, but they're useful in tests sometimes. If you want to test your classes or functions on some known sequence, it's easier to writedouble data[] = { 1.0, 2.0, 3.0, 2.0, 1.0 };thanvector<double> data;data.push_back(1.0);data.push_back(2.0);data.push_back(3.0);data.push_back(2.0);data.push_back(1.0);(although there's a proposal from Bjarne Stroustrup to allow syntax such asvector<double> data = { 1.0, 2.0, 3.0, 2.0, 1.0 };for vectors and any user-defined class which declares a special constructor)Luigi
 
User avatar
madmax
Posts: 1093
Joined: October 31st, 2003, 9:56 am

C++ quiz --- STL tricks

September 29th, 2005, 1:57 pm

Quotevector<double> data = { 1.0, 2.0, 3.0, 2.0, 1.0 };a similar syntax is available with the Blitz++ template Array that can be used for up to 12 dimensions.
 
User avatar
janzen
Posts: 21
Joined: May 3rd, 2005, 3:13 am

C++ quiz --- STL tricks

October 4th, 2005, 8:24 am

QuoteOriginally posted by: madmaxa similar syntax is available with the Blitz++ template Array that can be used for up to 12 dimensions....or with Boost, using Nicolai Josuttis's array container. But the need to specify the array size as a template argument is not exactly pretty, in my opinion:
 
User avatar
madmax
Posts: 1093
Joined: October 31st, 2003, 9:56 am

C++ quiz --- STL tricks

October 4th, 2005, 9:17 am

QuoteOriginally posted by: janzenQuoteOriginally posted by: madmaxa similar syntax is available with the Blitz++ template Array that can be used for up to 12 dimensions....or with Boost, using Nicolai Josuttis's array container. But the need to specify the array size as a template argument is not exactly pretty, in my opinion:That's a negligible price for superior performance.
 
User avatar
Cuchulainn
Posts: 62626
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

April 3rd, 2011, 11:08 am

Help Same with STL adapters.Quiz; what's up and how to resolve? Bug or undocumented feature?
Last edited by Cuchulainn on April 2nd, 2011, 10:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Posts: 62626
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C++ quiz --- STL tricks

April 11th, 2011, 5:36 am

QuoteOriginally posted by: CuchulainnHelp Same with STL adapters.Quiz; what's up and how to resolve? Bug or undocumented feature?http://www.progdoc.de/papers/ttp/psi-ttp/psi-ttp.html
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On