6

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.

2
  • 3
    get a different book now
    – qwr
    Jun 13, 2015 at 3:10
  • I'd be very surprised that Bjarne would include "" for standard headers and not call std::sort properly.
    – chris
    Jun 13, 2015 at 3:11

3 Answers 3

10

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.

3
  • That book looks faulty is a few ways. #include "iostream" looks very wrong. Jun 13, 2015 at 2:55
  • 1
    I took it directly from the book. The author mentions nothing about .begin() or .end(). And they use std_lib_facilities.h instead of iostream, but I'm using the other ones because Code Blocks doesn't have it.
    – CoopTang
    Jun 13, 2015 at 3:21
  • 2
    @CoopTang, Well there's your problem. This overload of sort is in that header that you're not including. CodeBlocks doesn't have it because it's supporting material for the book. You can find it here. FWIW, you will be able to call std::sort with one argument when Eric's ranges proposal makes its way into implementations.
    – chris
    Jun 13, 2015 at 3:28
7

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"
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.