从set <a *,comparator>中获取成员到列出<a></a> </a *,比较器>

时间:2014-04-26 22:55:31

标签: c++ list stl set

我有一组A*指针指向堆上分配的对象,并用比较器排序(使用operator()中的struct PtrComparator),如下所示:set<A*,PtrComparator> < / p>

比较器根据A.m_timeStamp.timeStamp basic_string对其进行排序。 但是我强制使用两个字符串参数描述的范围提取list<A>并将其返回给调用者。 所以我需要使用这两个参数来获得一系列迭代器吗?然后填写清单?

我该怎么做?

我担心我创建的任何临时A对象在函数终止时都会被销毁,所以我怎么把它存储在list<A>?我不是被迫再次使用堆了吗?

我不允许将list<A>更改为list<A*>但我可以修改我的实现以适应list<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;
}

我错过了这个功能

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

CTimeStamp有一个成员basic_string timeStamp

有人可以告诉我摆脱这种混乱的方法吗?

1 个答案:

答案 0 :(得分:2)

改变这个:

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

到此:

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

并且神奇地说你的生活会变得更好。