我尝试了许多不同版本的 C++,但编译器由于某种原因不接受我的代码,我也没有看到明显的语法错误,请帮忙。还请告诉我哪个版本的 c++ 支持
#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
bool comp(pair<int ,int> a, pair<int ,int> b)
{
return (a .first<b .first);
}
int main()
{
int n;
(input n) //for some reason stack overflow wont let me input
vector <int ,int> laptops;
for(int z=0; z<n; z++)
{
int a ,b;
cin>> a>> b;
pair <int ,int> p;
p= make _pair(a ,b);
laptops . push _back (p);
}
sort( laptops .begin(), laptops .end(),comp());
bool flag=false;
for (auto& it : arr)
{
if(a .second>b.second)
flag=true;
}
if(flag==true)
cout<<"Happy Alex";
else
cout<< "Poor Alex" ;
return 0;
}
这是我在programiz c++编译器上遇到的错误。
ERROR!
g++ /tmp/v6DZdO2ElD.cpp
In file included from /usr/local/include/c++/12.2.0/vector:64,
from /tmp/v6DZdO2ElD.cpp:3:
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'struct std::_Vector_base<int, int>':
/usr/local/include/c++/12.2.0/bits/stl_vector.h:423:11: required from 'class std::vector<int, int>'
/tmp/v6DZdO2ElD.cpp:18:21: required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:88:28: error: 'int' is not a class, struct, or union type
88 | rebind<_Tp>::other _Tp_alloc_type;
| ^~~~~~~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:90:9: error: 'int' is not a class, struct, or union type
90 | pointer;
| ^~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'class std::vector<int, int>':
/tmp/v6DZdO2ElD.cpp:18:21: required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:513:20: error: '_M_allocate' has not been declared in 'std::vector<int, int>::_Base'
513 | using _Base::_M_allocate;
| ^~~~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:514:20: error: '_M_deallocate' has not been declared in 'std::vector<int, int>::_Base'
514 | using _Base::_M_deallocate;
| ^~~~~~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:516:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<int, int>::_Base'
516 | using _Base::_M_get_Tp_allocator;
| ^~~~~~~~~~~~~~~~~~~
In file included from /usr/local/include/c++/12.2.0/bits/stl_algobase.h:59,
from /usr/local/include/c++/12.2.0/algorithm:60,
from /tmp/v6DZdO2ElD.cpp:1:
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = int; _Alloc = int]':
/tmp/v6DZdO2ElD.cpp:18:21: required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:137:24: error: 'int' is not a class, struct, or union type
137 | _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
| ^~~~~~~~~~~~~~~~~~~~
/tmp/v6DZdO2ElD.cpp: In function 'int main()':
/tmp/v6DZdO2ElD.cpp:25:26: error: no matching function for call to 'std::vector<int, int>::push_back(std::pair<int, int>&)'
25 | laptops.push_back(p);
| ~~~~~~~~~~~~~~~~~^~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1276:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = int; value_type = int]'
1276 | push_back(const value_type& __x)
| ^~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1276:35: note: no known conversion for argument 1 from 'std::pair<int, int>' to 'const std::vector<int, int>::value_type&' {aka 'const int&'}
1276 | push_back(const value_type& __x)
| ~~~~~~~~~~~~~~~~~~^~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1293:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = int; _Alloc = int; value_type = int]'
1293 | push_back(value_type&& __x)
| ^~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1293:30: note: no known conversion for argument 1 from 'std::pair<int, int>' to 'std::vector<int, int>::value_type&&' {aka 'int&&'}
1293 | push_back(value_type&& __x)
| ~~~~~~~~~~~~~^~~
/tmp/v6DZdO2ElD.cpp:27:18: error: 'class std::vector<int, int>' has no member named 'begin'
27 | sort(laptops.begin(),laptops.end(),comp());
| ^~~~~
/tmp/v6DZdO2ElD.cpp:27:34: error: 'class std::vector<int, int>' has no member named 'end'
27 | sort(laptops.begin(),laptops.end(),comp());
| ^~~
/tmp/v6DZdO2ElD.cpp:27:44: error: too few arguments to function 'bool comp(std::pair<int, int>, std::pair<int, int>)'
27 | sort(laptops.begin(),laptops.end(),comp());
| ~~~~^~
/tmp/v6DZdO2ElD.cpp:8:6: note: declared here
8 | bool comp(pair<int,int> a,pair<int,int> b)
| ^~~~
/tmp/v6DZdO2ElD.cpp:29:21: error: 'arr' was not declared in this scope
29 | for (auto& it : arr)
| ^~~
/tmp/v6DZdO2ElD.cpp:31:12: error: 'a' was not declared in this scope
31 | if(a.second>b.second)
| ^
/tmp/v6DZdO2ElD.cpp:31:21: error: 'b' was not declared in this scope
31 | if(a.second>b.second)
| ^
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = int; _Alloc = int]':
/usr/local/include/c++/12.2.0/bits/stl_vector.h:312:7: required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:139:26: error: 'int' is not a class, struct, or union type
139 | : _Tp_alloc_type()
| ^
您的代码中有很多错误:
(input n)
应该是什么?std::vector
的第二个模板参数是它的分配器类型,我假设你的意思是std::vector<std::pair<int, int>>
make _pair
和 push _back
arr
大概应该是 laptops
std::sort
时,不要调用它,只需传递函数名称(std::sort(laptops.begin(), laptops.end(), comp)
)if(a .second>b.second)
应该是什么,a
和 b
都不存在。<bits/stdc++.h>
不是 C++ 的一部分,只是 libstdc++ 的标头,所以应该避免。