I have a set of A* pointers pointing to objects allocated on the heap sorted with a comparator (using the operator() in a struct PtrComparator ) like this: set<A*,PtrComparator>

The comparator sorts it according to A.m_timeStamp.timeStamp which is a basic_string. However I am forced to extract a list<A> using a range described by two string parameters and return it to the caller. So I need to use those two parameters to get a range of iterators I guess? And then fill the list?

How do I do this?

I am afraid that any temporary A object I create will be destroyed when the function terminates so how do I even store it in the list<A>? Am I not forced to use the heap again?

I am not allowed to change the list<A> to list<A*> but I can modify my implementation to suite the list<A> return type, however how do I get around the problem with temporary objects when trying to fill the set somewhere in a constructor. Could I have an implemantation with a set<A>?

struct PtrComparator;
class A
 {
   public:
                   A           ( const CTimeStamp & timeStamp,
                                 const string     & from,
                                   const ABody  & body,
                                 const Attach    * attach );
    A(const A&);
    ~A();
    const string     & From      ( void ) const;
    const ABody  & Body         ( void ) const;
    const CTimeStamp & TimeStamp  ( void ) const;
    const Attach* Attachment     ( void ) const;
    // will do the above getters later
    friend ostream & operator <<   ( ostream & os, const A & x );
    //friend bool operator< (const A&,const A&);
    friend struct PtrComparator;

   private:
    CTimeStamp m_timeStamp;
    string m_from;
    ABody m_body;
    const Attach * m_ptr_to_attach;
 };
    //normal constructor
 A::A(const CTimeStamp & timeStamp,const string & from,
      const ABody & body,const Attach * attach)
 :m_timeStamp(timeStamp),m_from(from),m_body(body),m_ptr_to_attach(attach)//,count_(0)
 {
    attach->AddRef();
 }
 //copy constructor
{
//similar to normal constructor
}

//destructor
A::~A()
{
    m_ptr_to_attach->Release();
}
 ostream & operator<< (ostream & os,const A & x) 
 {
     os << "body:" <<x.m_body <<" , from: " << x.m_from << " , attachment: " 
     <<  (x.m_ptr_to_attach) <<" , timestamp:" << x.m_timeStamp;

     return os;
 }

struct PtrComparator{
 bool operator()(const A* first,const A* second)
 {
     return first->m_timeStamp.timestamp < second->m_timeStamp.timestamp;
 }
};
class Box_of_A
 {
   public:
                   Box_of_A ();
                   ~Box_of_A();
    bool           Delivery       ( const A      & a );
    bool           NewFolder     ( const string     &  folderName );

    **list<A>    ListOfA      ( const string     & folderName,
                               const CTimeStamp & from,
                               const CTimeStamp & to );**
    typedef map<string,set<A*,PtrComparator> * >::const_iterator  folders_c_it;
    typedef map<string,set<A*,PtrComparator> * >::iterator  folders_it;
   private:

    map<string,set<A*,PtrComparator> * > folders;
 };

bool Box_of_A::Delivery(const A & a)
{
    folders_it it = folders.find("index");
    if(it == folders.end())return false;
    set<A*,PtrComparator>* & folder = it->second;
    A * newA = new A(a);
    folder->insert(newA);

    return true;
}

I am missing the function

list<A>    ListOfA      ( const string     & folderName,
                            const CTimeStamp & from,
                             const CTimeStamp & to );

CTimeStamp has a member basic_string timeStamp.

Could somebody show me a way out of this mess?

有帮助吗?

解决方案

change this:

map<string,set<A*,PtrComparator> * > folders

to this:

map<string,set<A,Comparator> > folders

and magically your life will become better.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top