-2

I want to sort the inputs and output the sum of the last 5's , but I've encounter an error in the following:

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));

The following is my code , how can i fix it?

#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;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 3
    Both the first and the second parameters to `std::sort` must be iterators. Your second parameter is not an iterator. – Sam Varshavchik Apr 12 '20 at 00:01
  • The usefulness of (a) `cows`, and (b) presizing `weights` is highly questionable. Get rid of `cows` entirely, then declare `std::vector weights;`. Use its `push_back` member to insert values on the end. If desired, use `weights.reserve(n);` *after* reading `n`, but before your insertion loop. Finally, you can now use `weights.begin(), weighs.end()` as your sort arguments. – WhozCraig Apr 12 '20 at 00:16
  • including bits/anything is a real red flag – pm100 Apr 12 '20 at 00:36
  • Regarding your newest MATLAB question that you have deleted: it's possible that the xlabel was being rendered by latex, where % is a comment. This would mean that you need to escape it: `'\%'` to get a literal percent when compiled with latex. – Andras Deak -- Слава Україні Oct 24 '20 at 18:36

1 Answers1

2

There are some issues with your code that should be addressed besides the sort issue.

This is a refactored piece of code with comments on changes made and chages needed:

Running code

#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;
}
  1. Why should I not #include <bits/stdc++.h>?
  2. Why is "using namespace std;" considered bad practice?
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • I'v got the correct one : https://ideone.com/JCOKBu but I can't tell why I have to change the TotalWeight to what its like now – TheNotoruous0307 Apr 12 '20 at 01:10
  • @江冠德 `weights.at(n)` accesses 1 past the size of the vector because index starts at(0), besides even fixing that, it's not a very dynamic method, for example, if `n` is `10` you would only sum a part of your vector. if it's `2` you would be accessing `at(2 - 4)` this is a negative index, error again. A range-based loop avoids all these issues, because it will never access memory ouside the vector. – anastaciu Apr 12 '20 at 01:15
  • See here https://ideone.com/X00oqf – anastaciu Apr 12 '20 at 01:19
  • Or here https://ideone.com/me7qOP, in this case you are summing nodes that have not been filled and have garbage values, so the result is not as expected. – anastaciu Apr 12 '20 at 01:21
  • the range of the input is define in a file.in but I just wanna test if the code is correct then I'll change the cin into ifstream,and the range of the input file is already defined – TheNotoruous0307 Apr 12 '20 at 01:31
  • @江冠德, I see, I also had missed the "last 5" part in your question, I added some code to the answer for that. If the values come from the file and are controled you can remove the `if-else`, or just use the code you posted on Ideone which is fine. – anastaciu Apr 12 '20 at 01:34