今天在代码里写了个goto,然后就报错了,有人帮忙看看吗
#include<bits/stdc++.h>
using namespace std;
struct _{
int t,price;
};
struct cmp{
bool operator()(_ a,_ b){
return a.t>b.t;
}
};
int n,m,price[100005],t[100005],ans;
priority_queue<_,vector<_>,cmp> ticket;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int v;
scanf("%d%d%d",v,price[i],t[i]);
if(v==0){
ans+=price[i];
ticket.emplace(t[i],price[i]);
}
else if(v==1){
lable:
goto lable;
}
}
return 0;
}
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/x86_64-w64-mingw32/bits/c++allocator.h 33 0 In file included from D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/x86_64-w64-mingw32/bits/c++allocator.h
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/new_allocator.h 0 -1 In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = _; _Args = {int&, int&}; _Tp = _]':
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/alloc_traits.h 537 17 required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = _; _Args = {int&, int&}; _Tp = _; allocator_type = std::allocator<_>]'
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/vector.tcc 117 30 required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int&, int&}; _Tp = _; _Alloc = std::allocator<_>]'
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_queue.h 756 18 required from 'void std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = {int&, int&}; Tp = ; Sequence = std::vector<_>; Compare = cmp]'
D:/用户文件/桌面/C++/临时/未命名1.cpp 20 18 required from here
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/new_allocator.h 187 11 [错误] new initializer expression list treated as compound expression [-fpermissive]
D:/杞欢/灏忕唺鐚獵++/RedPanda-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/new_allocator.h 187 11 [错误] no matching function for call to '::(int&)'
D:/用户文件/桌面/C++/临时/未命名1.cpp 3 8 [说明] candidate: '::()'
D:/用户文件/桌面/C++/临时/未命名1.cpp 3 8 [说明] candidate expects 0 arguments, 1 provided
D:/用户文件/桌面/C++/临时/未命名1.cpp 3 8 [说明] candidate: 'constexpr ::(const &)'
D:/用户文件/桌面/C++/临时/未命名1.cpp 3 8 [说明] no known conversion for argument 1 from 'int' to 'const &'
D:/用户文件/桌面/C++/临时/未命名1.cpp 3 8 [说明] candidate: 'constexpr ::(&&)'
D:/用户文件/桌面/C++/临时/未命名1.cpp 3 8 [说明] no known conversion for argument 1 from 'int' to '&&'
(中间的两个数字是行和列)
这命名, 我都怀疑能不能运行
struct cmp{
bool operator()(_ a,_ b) const
{
return a.t>b.t;
}
};
别的不说
lable:
goto lable;
这你不怕死循环么
6,无意义的写法,死循环了
【以下回答由 GPT 生成】
你遇到了编译错误,它是由于在你的代码中的一个无限循环导致的。具体来说,在你的代码中,你使用了一个无限循环标签和goto语句,它们相互调用,导致了无限循环。
回到错误信息,它告诉我们在C++标准库中的一些头文件(例如c++allocator.h和bits/new_allocator.h)内部发生错误。这些错误是由于无限循环导致的,C++编译器试图分配内存去构造一个无限的数据结构,但是由于无限循环,内存无法分配,因此导致了编译错误。
为了解决这个问题,你需要避免使用无限循环和goto语句,因为它们可能导致代码逻辑错误和无限循环。你可以考虑使用其他控制结构,如while循环或do-while循环来替代goto语句。具体来说,在你的代码中,你可以使用一个while循环来取代标签和goto语句,例如:
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int v;
scanf("%d%d%d",&v,price[i],t[i]);
if(v==0){
ans+=price[i];
ticket.emplace(t[i],price[i]);
}
else if(v==1){
while(true){
// 这里放置你想要循环执行的代码
}
}
}
return 0;
}
在这个示例中,while循环将无限循环执行代码,直到满足某个条件才会退出。你需要根据你的实际需求向循环中添加适当的条件判断语句。
综上所述,你应该避免使用无限循环和goto语句,使用其他控制结构来替代它们,以避免出现编译错误。希望这个解决方案对你有帮助!如果你对这个解释还有疑问,请随时追问。