Topic: proposal: new auto_CONTAINER.


Author: cxl@volny.cz ("Mirek Fidler")
Date: Fri, 24 Oct 2003 21:16:54 +0000 (UTC)
Raw View
> auto_ptr does? Manage memory........... So, it can destroy it's
elements
> when it it's scope is over, and also the basic iterators work like a
> normal container, so that the client does not have to dereference then
> while using. Makes the code neater.

> int main ()
> {
>   nstd::auto_vector<int> avi;
>   avi.push_back (new int(23));
>   nstd::auto_vector<int>::iterator iter = avi.begin();
>   std::cout<<*iter;
> }
>

    Looks like NTL Array had some influence on you in the end :)

    (for others, this idea is very similiar to Array template class of
www.ntllib.org).

Mirek


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dhruvbird@gmx.net ("Dhruv")
Date: Mon, 27 Oct 2003 01:44:12 +0000 (UTC)
Raw View
On Fri, 24 Oct 2003 21:16:54 +0000, Mirek Fidler wrote:

>     Looks like NTL Array had some influence on you in the end :)
>
>     (for others, this idea is very similiar to Array template class of
> www.ntllib.org).

Yes, in fact one of your colleague has something very similar in the boost
sandbox, named ptr_container...


Regards,
-Dhruv.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dhruvbird@gmx.net ("Dhruv")
Date: Thu, 23 Oct 2003 20:25:17 +0000 (UTC)
Raw View
I was wondering whether if containers like auto_ptr<> can be designed and
used, because I have frequently required such functionality, and upto now,
I've been using a combination of the standard containers, and
boost::shared_ptr, but shared_ptr does have it's own overhead, and
efficiency glitches, so why not have a container that does exactly what
auto_ptr does? Manage memory........... So, it can destroy it's elements
when it it's scope is over, and also the basic iterators work like a
normal container, so that the client does not have to dereference then
while using. Makes the code neater.

I have here a rough sketch of what I have in mind. Any suggestions are
most welcome....... The begin(), end(), front(), back(), and all forms of
erase need to be overridden. Right now, I have only overridden the dtor to
show what use this container can be of. Also, the iterators will have to
be rewritten as shown, to change the behaviour of operator*, and
operator->.



//BEGIN CODE.


#include <vector>
#include <iostream>

namespace nstd {

template <class Type, class Allocator = std::allocator<Type*> >
class auto_vector : public std::vector<Type*, Allocator> {

  typedef typename std::vector<Type*, Allocator> Base;

  class ptr_iterator : public Base::iterator {
    typedef typename Base::iterator Iterator_Base;

  public:
    ptr_iterator () { }
    ptr_iterator (Iterator_Base IB_) : Iterator_Base (IB_) { }
    Type& operator* () { return *Iterator_Base::operator*(); }
  };

public:
  typedef typename Base::value_type value_type;
  typedef typename Base::const_reference const_reference;
  typedef ptr_iterator iterator;
//   void push_back (const_reference data)
//   {
//     std::cout<<"In the derived function"<<std::endl;
//     Base::push_back (data);
//   }

  iterator begin() { return static_cast<iterator> (Base::begin ()); }
  ~auto_vector ()
  {
    typename Base::iterator rover = Base::begin ();
    typename Base::iterator last = Base::end ();
    while (rover != last) {
      delete *rover;
      ++rover;
    }
  }
};
}


int main ()
{
  nstd::auto_vector<int> avi;
  avi.push_back (new int(23));
  nstd::auto_vector<int>::iterator iter = avi.begin();
  std::cout<<*iter;
}

//Container automatically destroyes the pointer's contents.





//END CODE.


Regards,
-Dhruv.






---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]