没有用于调用排序的匹配函数
no matching function for call to sort
我想对输入进行排序并输出最后 5 的总和,但遇到以下错误:
a.cpp:13:36: error: no matching function for call to ‘sort(std::vector<int>::iterator, __gnu_cxx::__alloc_traits<std::allocator<int> >::value_type&)’
sort(weights.begin(),weights.at(n));
以下是我的代码,我该如何修改?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
int TotalWeight;
int cows[100000];
vector<int> weights(100000);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> cows[i];
weights[i] = cows[i];
}
sort(weights.begin(), weights.at(n));
TotalWeight = weights.at(n - 4) + weights.at(n - 3) + weights.at(n - 2) + weights.at(n - 1) + weights.at(n);
cout << TotalWeight << endl;
return 0;
}
除了 sort
问题之外,您的代码还有一些问题需要解决。
这是一段重构的代码,其中包含对所做更改和需要更改的评论:
#include <bits/stdc++.h> //this is not good, you should include only the needed libraries (1)
using namespace std; //this is also not a good practice (2)
int main()
{
int n;
int TotalWeight = 0;
int cows; //an array is not really necessary
vector<int> weights; //no need to reserve memory, this is a dynamic container
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> cows;
weights.push_back(cows); //this is how you shoud insert values in a vector in order
}
//sort(weights.begin(), weights.begin() + n); //2nd member iterator
sort(weights.begin(), weights.end()); // since the vector now has the size it needs to have, you can use weights.end()
//this will cause out of bounds access in the vector, weights.at(n), at least
//TotalWeight = weights.at(n - 4) + weights.at(n - 3) + weights.at(n - 2) + weights.at(n - 1) + weights.at(n);
for(int i : weights){ //using a range-based loop will avoid the out-of-range exception
TotalWeight += i;
}
cout << TotalWeight << endl;
//for the last 5
TotalWeight = 0;
if(weights.size() >= 5){
for(int i = 0; i < 5; i++)
TotalWeight += weights[i];
cout << TotalWeight << endl;
}
else
cout << "vector too small";
return 0;
}
- Why is "using namespace std;" considered bad practice?
我想对输入进行排序并输出最后 5 的总和,但遇到以下错误:
a.cpp:13:36: error: no matching function for call to ‘sort(std::vector<int>::iterator, __gnu_cxx::__alloc_traits<std::allocator<int> >::value_type&)’
sort(weights.begin(),weights.at(n));
以下是我的代码,我该如何修改?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
int TotalWeight;
int cows[100000];
vector<int> weights(100000);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> cows[i];
weights[i] = cows[i];
}
sort(weights.begin(), weights.at(n));
TotalWeight = weights.at(n - 4) + weights.at(n - 3) + weights.at(n - 2) + weights.at(n - 1) + weights.at(n);
cout << TotalWeight << endl;
return 0;
}
除了 sort
问题之外,您的代码还有一些问题需要解决。
这是一段重构的代码,其中包含对所做更改和需要更改的评论:
#include <bits/stdc++.h> //this is not good, you should include only the needed libraries (1)
using namespace std; //this is also not a good practice (2)
int main()
{
int n;
int TotalWeight = 0;
int cows; //an array is not really necessary
vector<int> weights; //no need to reserve memory, this is a dynamic container
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> cows;
weights.push_back(cows); //this is how you shoud insert values in a vector in order
}
//sort(weights.begin(), weights.begin() + n); //2nd member iterator
sort(weights.begin(), weights.end()); // since the vector now has the size it needs to have, you can use weights.end()
//this will cause out of bounds access in the vector, weights.at(n), at least
//TotalWeight = weights.at(n - 4) + weights.at(n - 3) + weights.at(n - 2) + weights.at(n - 1) + weights.at(n);
for(int i : weights){ //using a range-based loop will avoid the out-of-range exception
TotalWeight += i;
}
cout << TotalWeight << endl;
//for the last 5
TotalWeight = 0;
if(weights.size() >= 5){
for(int i = 0; i < 5; i++)
TotalWeight += weights[i];
cout << TotalWeight << endl;
}
else
cout << "vector too small";
return 0;
}
- Why is "using namespace std;" considered bad practice?