Serving the Quantitative Finance Community

 
User avatar
MaxCohen
Topic Author
Posts: 0
Joined: June 13th, 2007, 2:44 pm

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 11:23 am

I have created my own multi-dimensional array class. I then wanted to make it work with STL libraries like <algorithm> and <numeric>. It seems all I needed to to was make sure there is functions pointing to start and end of data that is stored (i.e. begin() and end()).1. Do I need to worry about creating STL typedef to create STL compliant classes. What is their benifit?2. I think I have created a bi-directional iterator? Why does STL work with and iterator classes. Is this something I should be doing i.e. what are the benifits? There might some errors on below as I did this from memorytemplate<typename _T, size_t _N>class Array{public: // STL typedefs typedef _T* pointer; typedef _T* iterator; ...private: // member variables pointer _data; size_t _length; size_t _extents[_N]; ...public: // iterators iterator begin() { return _data; } iterator end() { return _data + _length; } ...};#include <numeric>int main(){ Array<int,3> = A(1,2,3); // declares 3-dimensional array with dimensions 1x2x3 // fill array... int sum = std::accumulate(A.begin(),A.end(),0) // forward iteration for(Array<int,3>::iterator it=A.begin() ; it!=A.begin(), it++) { ... } // backward iteration for(Array<int,3>::iterator it=A.end()-1 ; it!=A.begin(), it--) { ... }}
Last edited by MaxCohen on October 23rd, 2011, 10:00 pm, edited 1 time in total.
 
User avatar
Polter
Posts: 1
Joined: April 29th, 2008, 4:55 pm

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 11:30 am

Quick remark about the notation: strictly speaking, to be standard-compliant you shouldn't declare or define your own names that begin with an underscore, as those are reserved: see http://stackoverflow.com/questions/2287 ... 17.4.3.2.1 Global names [lib.global.names] Certain sets of names and function signatures are always reserved to the implementation: Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use. Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.165 165) Such names are also reserved in namespace ::std (17.4.3.1).I'm not sure what you mean by "why does STL work with and iterator classes"?
 
User avatar
Etuka
Posts: 1
Joined: January 1st, 2002, 7:40 pm

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 11:34 am

I think you are asking why the iterator concept is important. When you implement an iterator according to one of the iterator specs (bidirectional, random access, forward, etc), you get to use algorithms within the std library that know what to do with these concepts. It also makes your code a bit more portable - if you decide to move from one container to another, you can take advantage of having used a standard interface to do so.
 
User avatar
MaxCohen
Topic Author
Posts: 0
Joined: June 13th, 2007, 2:44 pm

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 11:42 am

Polta and Etuka, thanks...I would have though algothim/numeric was example of one of that aspects of STL that would require/know what to do with the iterator class. But appears it works fine in my simple example that does not use an iterator class.So are the STL typedef's a requirement for any particular purpose?
 
User avatar
Etuka
Posts: 1
Joined: January 1st, 2002, 7:40 pm

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 11:57 am

OK - now I know what you are getting at (didn't read the code sample particularly). As long as your typedef structures have expected properties, iterator does not have to be a class - for example, boost::array::iterator is not a class, but a typedef. don't forget to implement const_iterator, reverse_iterator...
 
User avatar
MaxCohen
Topic Author
Posts: 0
Joined: June 13th, 2007, 2:44 pm

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 12:01 pm

what about typedef like pointer, reference, const_reference are these simply used for clearer programming purpose or are they required by some STL components. E.g. I think I read somewhere typedef "key" maybe required when you have an associative container
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

C++ STL algorithm/numeric compliant containers

October 24th, 2011, 12:32 pm

You have to decide what STL "Concept" to support and then implement the functionality to ensure that all of the expressions for this concept have the correct semantics with your class. For example you probably want to implement "Sequence" concept which is defined here:http://www.sgi.com/tech/stl/Sequence.htmlSee the section "valid expressions"