请教一个关于set_union的问题

mc883602 2003-04-20 11:44:30

//////////////////////////////////////////////////

// 这段可以通过
#include <algorithm>
#include <set>
using namespace std;

int main()
{
set<int> s1;
set<int> s2;

set<int> s;
s.insert(1);

set_union(s2.begin(), s2.end(), s.begin(), s.end(),
inserter(s1, s1.begin()) );


return 0;
}

/////////////////////////////////////////////////////

//这段通不过,why?

#include <algorithm>
#include <set>
#include "fun_obj.h"
using namespace std;

int main()
{
set<CID> s1;
set<CID> s2;

set<CID> s;
s.insert(CID(1, 2));

set_union(s2.begin(), s2.end(), s.begin(), s.end(),
inserter(s1, s1.begin()) );


return 0;
}


/////////////////////////////////////////////////////

<fun_obj.h>

#ifndef FUN_OBJ_H
#define FUN_OBJ_H

#include "id.h"

class CLessThan
{
public:

bool operator() (const CID & id1, const CID & id2)
{
return id1.GetI() < id2.GetI() ||
id1.GetI() == id2.GetI() && id1.GetJ() < id2.GetJ();
}
};

#endif

//////////////////////////////////////

<id.h>

#ifndef ID_H
#define ID_H

class CID
{
public:

CID () {;}

CID (int n_i, int n_j);
CID (const CID & id);

CID & operator= (const CID & id);
bool operator== (const CID & id) const;
bool operator!= (const CID & id) const {return !operator==(id);}

int GetI () const {return m_n_i;}
int GetJ () const {return m_n_j;}


private:

int m_n_i;
int m_n_j;
};

#endif

////////////////////////////////////////

<id.cc>

#include "id.h"

CID::CID (int n_i, int n_j):
m_n_i(n_i), m_n_j(n_j)

CID::CID (const CID & id):
m_n_i(id.m_n_i), m_n_j(id.m_n_j)
{
}

CID & CID::operator= (const CID & id)
{
m_n_i = id.m_n_i;
m_n_j = id.m_n_j;

return *this;
}

bool CID::operator== (const CID & id) const
{
return m_n_i == id.m_n_i && m_n_j == id.m_n_j;
}

////////////////////////////////////////

...全文
143 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mc883602 2003-04-21
  • 打赏
  • 举报
回复
不好意思,疏漏了。不过还是有问题:
Y:\>g++ -o test.exe test.cc -I"C:\Dev-C++\include\G++" -I"C:\Dev-C++\include"-I
-L"C:\Dev-C++\lib"
C:\Dev-C++\include\G++\stl_algo.h: In function `class insert_iterator<set<CID,CLessThan,allocator<CID> > > set_union<_Rb_tree_iter
ator<CID,const CID &,const CID *>, _Rb_tree_iterator<CID,const CID &,const CID *>, insert_iterator<set<CID,CLessThan,allocator<CID
> > > >(_Rb_tree_iterator<CID,const CID &,const CID *>, _Rb_tree_iterator<CID,const CID &,const CID *>, _Rb_tree_iterator<CID,cons
t CID &,const CID *>, _Rb_tree_iterator<CID,const CID &,const CID *>, insert_iterator<set<CID,CLessThan,allocator<CID> > >)':
test.cc:54: instantiated from here

C:\Dev-C++\include\G++\stl_algo.h:2315: no match for `const CID & < const CID &'
test.cc:54: instantiated from here

C:\Dev-C++\include\G++\stl_algo.h:2319: no match for `const CID & < const CID &'

其中的54行就是set_union那一行
fangrk 2003-04-21
  • 打赏
  • 举报
回复
由于set是自动排序的,所以构造set时候必须有排序准则,缺省采用Less<T>。你的第二个程序中没有排序准则,所以错了;况且,你定义了CLessThan也没有用到。
fangrk 2003-04-21
  • 打赏
  • 举报
回复
int main()
{
set<CID,CLessThan> s1;
set<CID,CLessThan> s2;

set<CID,CLessThan> s;
s.insert(CID(1, 2));

set_union(s2.begin(), s2.end(), s.begin(), s.end(),
inserter(s1, s1.begin()) );


return 0;
}
mc883602 2003-04-21
  • 打赏
  • 举报
回复
3x!
fangrk 2003-04-21
  • 打赏
  • 举报
回复
其实你为CID添加
bool operator<(const CID& id)const;就可以了不必写CLessThan了。


#include <algorithm>
#include <set>
using namespace std;
class CID
{
public:

CID () {;}

CID (int n_i, int n_j);
CID (const CID & id);

CID & operator= (const CID & id);
bool operator== (const CID & id) const;
bool operator!= (const CID & id) const {return !operator==(id);}
bool operator<(const CID& id)const
{ return m_n_i<id.m_n_i || m_n_i==id.m_n_i&&m_n_j<id.m_n_j;}

int GetI () const {return m_n_i;}
int GetJ () const {return m_n_j;}


private:

int m_n_i;
int m_n_j;
};
CID::CID (int n_i, int n_j):
m_n_i(n_i), m_n_j(n_j){}

CID::CID (const CID & id):
m_n_i(id.m_n_i), m_n_j(id.m_n_j)
{
}

CID & CID::operator= (const CID & id)
{
m_n_i = id.m_n_i;
m_n_j = id.m_n_j;

return *this;
}

bool CID::operator== (const CID & id) const
{
return m_n_i == id.m_n_i && m_n_j == id.m_n_j;
}


int main()
{
set<CID> s1;
set<CID> s2;

set<CID> s;
s.insert(CID(1, 2));

set_union(s2.begin(), s2.end(), s.begin(), s.end(),
inserter(s1, s1.begin()));


return 0;
}
fangrk 2003-04-21
  • 打赏
  • 举报
回复

set_union(s2.begin(), s2.end(), s.begin(), s.end(),
inserter(s1, s1.begin()),CLessThan() );

24,856

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧