求助,set_intersection处报错error C2678!!
刚学stl,写了一个关于set的小程序,结果就是编译不过,报错:
二进制“=”: 没有找到接受“const st”类型的左操作数的运算符(或没有可接受的转换)
\teststl\teststl.cpp(16): 可能是“st &st::operator =(const st &)”
按理说,出现这个错误需要重构一个 = 运算符了,并且需要是const型的,但一旦是const的,又不能对结构体里的变量赋值了啊!怎么破???
代码如下:
#include<iostream>
#include<set>
#include<string>
#include<algorithm>
using namespace std ;
typedef struct st{
string str ;
int i ;
}NODE, *PNODE;
struct LTNode
{
bool operator()( const NODE &nod1, const NODE &nod2 ) const
{
return nod1.str<nod2.str ;
}
};
bool set_eq( NODE nod1, NODE nod2 )
{
return nod1.str < nod2.str ;
}
void print( const NODE &nod )
{
cout<<nod.str<<" "<<nod.i<<endl ;
}
int main()
{
set<NODE, LTNode> sa, sb, sc ;
NODE nod ;
nod.str = "A5" ;nod.i = 5 ;
sa.insert( nod ) ;
nod.str = "A1" ;nod.i = 1 ;
sa.insert( nod ) ;
nod.str = "A3" ;nod.i = 3 ;
sa.insert( nod ) ;
for_each( sa.begin(), sa.end(), print ) ;
nod.str = "A4" ;nod.i = 4 ;
sb.insert( nod ) ;
nod.str = "A2" ;nod.i = 2 ;
sb.insert( nod ) ;
for_each( sb.begin(), sb.end(), print ) ;
set_intersection( sa.begin(), sa.end(), sb.begin(), sb.end(), sc.begin(), set_eq ) ;
for_each( sc.begin(), sc.end(), print ) ;
system( "pause" ) ;
return 0 ;
}