为解决spoj问题编写的我的c ++代码没有被编译。它在我的电脑上编译非常weint但在spoj或ideone.com上编译。我无法理解它显示的错误。请帮助!! http://ideone.com/3Wce5t
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct point{
int xx;int yy;int zz;
bool const operator < (point& b)
const {
return xx < b.xx;
}
};
bool myfun(const point& a,const point& b)
{
return a.yy < b.yy;
}
const int N = 1000001;
int tree[N],L[N],R[N];
int next;
vector <point> v;
void build(int ID,int l,int r)
{
tree[ID] = 0;
int m = (l+r)>>1;
if(l<r)
{
L[ID] = next++;
build(L[ID],l,m);
R[ID] = next++;
build(R[ID],m+1,r);
}
else
{L[ID] = -1;R[ID] = -1;}
}
void update(int ID,int id,int l,int r,int loc) //new ID and old id
{
int m = (l+r)>>1;
if(l==r)
{
tree[ID] = 1;
return;
}
L[ID] = L[id];
R[ID] = R[id];
if(l<=loc && loc<=m)
{L[ID] = next++;
update(L[ID],L[id],l,m,loc);
}
if((m+1)<=loc && loc<=r)
{R[ID] = next++;
update(R[ID],R[id],m+1,r,loc);
}
tree[ID] = tree[L[ID]] + tree[R[ID]];
}
int get(int id,int ID,int k,int l,int r)
{
if(l==r) return v[l].xx;
int mid = (l+r)>>1;
//cout<<tree[L[ID]] - tree[L[id]]<<' '<<k<<' '<<l<<' '<<r<<endl;
if(tree[L[ID]]-tree[L[id]] >= k)
return get(L[id],L[ID],k,l,mid);
else
return get(R[id],R[ID],k-(tree[L[ID]]-tree[L[id]]),mid+1,r);
}
int main()
{
int n,i,j,m;
cin>>n>>m;
v.resize(n);
for(i=0;i<n;i++)
{cin>>v[i].xx;
v[i].yy = i;
}
sort(v.begin(),v.end());
for(i=0;i<n;i++)
v[i].zz = i;
sort(v.begin(),v.end(),myfun);
next = 2;
build(1,0,n-1);
vector <int> ind(n+1);
ind[0] = 1;
for(i=1;i<=n;i++)
{
ind[i] = next++;
update(ind[i],ind[i-1],0,n-1,v[i-1].zz);
//ID and then location
}//we set original v[i]'s position in sorted as 1
int a,b,c;
sort(v.begin(),v.end());
for(i=0;i<m;i++)
{
cin>>a>>b>>c;
a--;
cout<<get(ind[a],ind[b],c,0,n-1)<<endl;
}
}
我得到的错误是::
/usr/include/c++/4.3/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = point]':
/usr/include/c++/4.3/bits/stl_algo.h:1919: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<point*, std::vector<point, std::aintocator<point> > >, _Size = int]'
/usr/include/c++/4.3/bits/stl_algo.h:4783: instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<point*, std::vector<point, std::aintocator<point> > >]'
prog.cpp:105: instantiated from here
/usr/include/c++/4.3/bits/stl_algo.h:93: error: no match for 'operator<' in '__a < __b'
prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
/usr/include/c++/4.3/bits/stl_algo.h:94: error: no match for 'operator<' in '__b < __c'
prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
/usr/include/c++/4.3/bits/stl_algo.h:96: error: no match for 'operator<' in '__a < __c'
prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
/usr/include/c++/4.3/bits/stl_algo.h:100: error: no match for 'operator<' in '__a < __c'
prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
/usr/include/c++/4.3/bits/stl_algo.h:102: error: no match for 'operator<' in '__b < __c'
prog.cpp:20: note: candidates are: const bool point::operator<(point&) const
答案 0 :(得分:0)
如错误中所述,编译器无法找到正确的比较运算符,因为它应该被定义为
bool const operator < (point b)
而不是
bool const operator < (point& b)
编辑:正如评注中所述,
bool const operator < ( point const & b)
可能是更好的方式
答案 1 :(得分:-1)
在struct:
之外移动重载运算符struct point
{
int xx;
int yy;
int zz;
};
bool const operator < (const point &a, const point &b) const
{
return xx < b.xx;
}