Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no matching function for call to sort()

Tags:

c++

I'm teaching myself through Programming: Principles and Practice Using C++ (2nd Edition) and I'm at the vectors section. I can copy and paste their code and it still won't work. My code is as follows.

#include "iostream"
#include "vector"
#include "algorithm"



using namespace std;


int main()
{
    cout << "Please enter the temperatures for the last five days\n";
    vector<double> temperatures;

    for(double temp; cin >> temp;){
        temperatures.push_back(temp);
    }
    double sum = 0;
    for (double temp : temperatures){
        sum += temp;
    }
    cout << "Mean temperature is " <<(sum / temperatures.size()) << endl;

    sort(temperatures);
    cout << "Median temperature is " << temperatures[temperatures.size()/2];
}

Help is appreciated and an explanation is better.

like image 255
CoopTang Avatar asked Jun 13 '15 02:06

CoopTang


3 Answers

Template function std::sort expects a pair of iterators as its arguments (i.e. a range) and, optionally, a comparator. There's no sort function that can be applied directly to a container. Where did you get the idea to call it as sort(temperatures)? To sort the entire temperatures vector it should be called as std::sort(temperatures.begin(), temperatures.end()). I'm sure that book has quite a few examples that demonstrate it rather clearly.

Additionally, standard library headers should normally be included as #include <algorithm>, i.e. using <...> syntax, not "..." syntax.

like image 124
AnT Avatar answered Nov 16 '22 23:11

AnT


First of all you should correct this:

#include "iostream"
#include "vector"
#include "algorithm"

To this:

#include <iostream>
#include <vector>
#include <algorithm>

Now, there are two versions of std::sort as follows:

  1. void sort( RandomIt first, RandomIt last );
  2. void sort( RandomIt first, RandomIt last, Compare comp );

The first one taking two random iterators, example:

std::vector<int> vec {5,4,3,2,1};
std::sort(vec.begin(), vec.end());

This sorts the elements in Ascending Order by default.

The second version of sort has an additional parameter that allows you to use your own comparison function to sort the elements, function must return bool.

Example:

bool comp(const int &a, const int &b)
{
    return a > b;
}

And calling sort: std::sort(vec.begin(), vec.end(), comp); would sort them in descending order.

Lambdas are often used instead of compare functions, could be done like so:

std::sort(vec.begin(), vec.end(), [](const int &a, const int &b) {return a < b;});

By the way, to get "std_lib_facilities.h":

  1. http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
  2. Copy text into a text editor and save the file as std_lib_facilities.h, include it in your project:
  3. #include "std_lib_facilities.h"
like image 39
Andreas DM Avatar answered Nov 16 '22 22:11

Andreas DM


First of all, regarding the headers, the code from the book requires the Header "std_lib_facilities.h", which can be found at the support website for the book, and integration of the header in your project in the Appendix C of the book.

In the code, he did: #include "std_lib_facilities.h" which is correct way for User defined Headers in the Project, but including Standard Library Headers by that way is completely wrong. Standard Library Headers must be included using the #include <...> notation. He, in his book, never included Standard Library Headers with #include "..."notation.


Now, regarding the actual question of sort(temperatures), This version of sort() is available in his own header, "std_lib_facilities.h" to make it easy for beginners to use sort() without knowing the technicalities of Iterators at an early stage.

So, to use that version of sort, either #include "std_lib_facilities.h", or use the other versions of sort, one of them being:

sort(temperatures.begin(), temperatures.end());
like image 2
Anuj Aggarwal Avatar answered Nov 16 '22 21:11

Anuj Aggarwal