C++ C++;11:`error:在‘;之前应为主表达式)’;代码`中的标记,用于行扫描以查找最近的对
这是用C++11编写的针对的尝试O(NlogN)解决方案。我用过。比较器可能会混淆,原因如下:C++ C++;11:`error:在‘;之前应为主表达式)’;代码`中的标记,用于行扫描以查找最近的对,c++,c++11,C++,C++11,这是用C++11编写的针对的尝试O(NlogN)解决方案。我用过。比较器可能会混淆,原因如下:cmp首先按x然后按y对点进行排序,compare_y按y值对点进行排序 下面是我得到的错误,后面是我的代码 cp.cpp: In function ‘int main()’: cp.cpp:49:95: error: expected primary-expression before ‘)’ token for (set<point>::iterator
cmp
首先按x然后按y对点进行排序,compare_y
按y值对点进行排序
下面是我得到的错误,后面是我的代码
cp.cpp: In function ‘int main()’:
cp.cpp:49:95: error: expected primary-expression before ‘)’ token
for (set<point>::iterator it = s.lower_bound(s.begin(), s.end(), t1, compare_y); it < s.upper_bound(s.begin(), s.end(), t2, compare_y); ++it) {
^
cp.cpp:49:150: error: expected primary-expression before ‘)’ token
for (set<point>::iterator it = s.lower_bound(s.begin(), s.end(), t1, compare_y); it < s.upper_bound(s.begin(), s.end(), t2, compare_y); ++it) {
首先,不要使用
第二,你把成员函数和算法混淆了——它们采用不同的参数。成员函数只接受一个参数(其他三个参数是隐含的-集合的范围和比较器。尽管即使对于std::lower_bound()
,您也错误地提供了比较器,因为该参数需要是一个对象,而您只是提供了一个类型名)
第三,s
是一个std::set
,因此这些成员函数返回一个std::set::iterator
,而不是std::set::iterator
第四,
std::set::iterator
不是随机访问,因此没有操作符。为什么会有下一票?我的问题是否不适用于此站点?错误是因为您试图在需要函数参数的位置传递compare\y
,但compare\y
是类型的名称,而不是对象。不过,正如巴里解释的那样,你还需要解决其他问题。你的答案非常有效,我会在5分钟内接受。谢谢。我意识到这超出了问题的范围,但如果你能再给我一个提示的话:我的代码超过了时间限制(当我只是用行扫描强制执行时,我没有)。您是否看到任何会使此代码无法及时运行的内容?我不会让你为了正确的答案而回答这个问题;无论如何我都会接受的。只是想知道你能不能给点奖金。
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
// define x, y as real(), imag()
typedef complex<double> point;
#define x real()
#define y imag()
bool cmp(point a, point b) {
if (a.x < b.x) return true;
if (a.x - b.x < EPS && a.y < b.y) return true;
return false;
}
struct compare_y {
bool operator() (const point& lhs, const point& rhs) const{
if (lhs.y < rhs.y) return true;
}
};
int main() {
int n;
while (scanf("%d", &n) && n != 0) {
vector<point> coord;
for (int i=0; i<n; ++i) {
double xi, yi;
scanf("%lf%lf", &xi, &yi);
point t(xi, yi);
coord.push_back(t);
}
sort(coord.begin(), coord.end(), cmp);
double h = 10000000;
set<point, compare_y> s;
s.insert(coord[0]);
for (int i=1; i<n; ++i) {
for (auto pt:s) {
if (abs(pt-coord[i])+EPS > h) { // bound by x
s.erase(pt);
}
}
point t1 = coord[i]; t1.imag(t1.y - h);
point t2 = coord[i]; t2.imag(t2.y + h);
for (set<point>::iterator it = s.lower_bound(s.begin(), s.end(), t1, compare_y); it < s.upper_bound(s.begin(), s.end(), t2, compare_y); ++it) {
h = min(h, abs(*it-coord[i]));
}
s.insert(coord[i]);
}
if (h + EPS >= 10000)
printf("INFINITY\n");
else
printf("%.4lf\n", h);
}
}
for (std::set<point, compare_y>::iterator it = s.lower_bound(t1); it != s.upper_bound(t2); ++it) {
for (auto it = s.lower_bound(t1); it != s.upper_bound(t2); ++it) {