There is no corresponding function to call sort ()

I'm a Programmer: Principles and Practice Using C ++ (2nd Edition) and I'm in the Vectors section. I can copy and paste my code and it still won't work. My code looks like this.

#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.

+3


source to share


3 answers


The template function std::sort

expects a pair of iterators as its arguments (i.e. range) and optionally a comparator. There is no function sort

that can be applied directly to the container. Where did you get the idea to name it sort(temperatures)

? To sort the entire vector temperatures

, it should be named as std::sort(temperatures.begin(), temperatures.end())

. I'm sure there are quite a few examples in this book that demonstrate this quite clearly.



In addition, standard library headers usually need to be included as #include <algorithm>

, that is, using syntax <...>

rather than "..."

.

+4


source


First of all, you must fix this:

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

      

For this:

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

      


There are now two versions std::sort

:

  • void sort( RandomIt first, RandomIt last );

  • void sort( RandomIt first, RandomIt last, Compare comp );

The first one, which takes two random iterators, for example:

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

      

This sorts the items 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, the 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);

will sort them in descending order.

Lambdas are often used instead of comparison functions, you can do this:

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

      


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

+4


source


First of all, in terms of titles, the code from the book requires the title "std_lib_facilities.h", which can be found on the support website for the book and the integration of the title into the project in Appendix C of the book.

In code he did: #include "std_lib_facilities.h"

which is the correct way for custom headers in the Project, but including the standard library headers, is completely wrong. Standard library headers must be included using the notation #include <...>

. He never included the standard library headers with the notation in his book #include "..."

.


Now, on the actual question of sorting (temperatures), This version of sort () is available in its own header "std_lib_facilities.h" to make it easier for beginners to use sort () without knowing the specifications of iterators early on.

So, to use this version of the sort, either #include "std_lib_facilities.h"

, or use other versions of the sort, one of which is:

sort(temperatures.begin(), temperatures.end());

      

+1


source







All Articles