这是我的代码
#include <bits/stdc++.h>
using namespace std;
struct Cow {
int row, col;
bool operator<(const Cow& other) const {
return row < other.row || (row == other.row && col < other.col);
}
};
int main() {
int r, c;
cin >> r >> c;
int n;
cin >> n;
vector<Cow> cows;
for (int i = 0; i < n; i++) {
int row, col;
cin >> row >> col;
cows.push_back({row, col});
}
sort(cows.begin(), cows.end());
set<pair<int, int>> visited;
for (const auto& cow : cows) {
visited.insert({cow.row, cow.col});
if (is_sorted(visited.begin(), visited.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
})) {
cout << 1 << endl;
for (const auto& v : visited) {
if (v == make_pair(cow.row, cow.col)) {
cout << i + 1 << endl;
break;
}
}
return 0;
}
visited.erase(cow);
}
cout << -1 << endl;
return 0;
}
请问为什么会编译失败,以下是编译失败的程序
/tmp/compiler_4rteggt4/src: 在函数‘int main()’中:
/tmp/compiler_4rteggt4/src:31:29: 错误:‘i’在此作用域中尚未声明
31 | cout << i + 1 << endl;
| ^
/tmp/compiler_4rteggt4/src:37:26: 错误:no matching function for call to ‘std::set<std::pair<int, int> >::erase(const Cow&)’
37 | visited.erase(cow);
| ^
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/set:61,
from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:87,
from /tmp/compiler_4rteggt4/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_set.h:654:7: 附注:candidate: ‘std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(std::set<_Key, _Compare, _Alloc>::const_iterator) [with _Key = std::pair<int, int>; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, int> >; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<std::pair<int, int> >; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<int, int> >]’
654 | erase(const_iterator __position)
| ^~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_set.h:654:28: 附注: no known conversion for argument 1 from ‘const Cow’ to ‘std::set<std::pair<int, int> >::const_iterator’ {aka ‘std::_Rb_tree_const_iterator<std::pair<int, int> >’}
654 | erase(const_iterator __position)
| ~~~~~~~~~~~~~~~^~~~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_set.h:684:7: 附注:candidate: ‘std::set<_Key, _Compare, _Alloc>::size_type std::set<_Key, _Compare, _Alloc>::erase(const key_type&) [with _Key = std::pair<int, int>; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, int> >; std::set<_Key, _Compare, _Alloc>::size_type = long unsigned int; std::set<_Key, _Compare, _Alloc>::key_type = std::pair<int, int>]’
684 | erase(const key_type& __x)
| ^~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_set.h:684:29: 附注: no known conversion for argument 1 from ‘const Cow’ to ‘const key_type&’ {aka ‘const std::pair<int, int>&’}
684 | erase(const key_type& __x)
| ~~~~~~~~~~~~~~~~^~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_set.h:706:7: 附注:candidate: ‘std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(std::set<_Key, _Compare, _Alloc>::const_iterator, std::set<_Key, _Compare, _Alloc>::const_iterator) [with _Key = std::pair<int, int>; _Compare = std::less<std::pair<int, int> >; _Alloc = std::allocator<std::pair<int, int> >; std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<std::pair<int, int> >; std::set<_Key, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<int, int> >]’
706 | erase(const_iterator __first, const_iterator __last)
| ^~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/bits/stl_set.h:706:7: 附注: 备选需要 2 实参,但提供了 1 个