sort function not found

Hello. My compiler is telling me that it cannot find the sort function. What did I do wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
	
	vector<double> temps;
	for (double temp; cin >> temp;)
		temps.push_back(temp);

	double sum = 0;
	for (double x : temps) sum += x;
	cout << "AVEARGE TEMPERATURE: " << sum / temps.size() / 2 << "\n" << endl;

	sort(temps);
	cout << "MEDIAN TEMPERATURE: " << temps[temps.size() / 2] << "\n" << endl;
	return 0;
}
 
sort(temps.begin(), temps.end());

@leon4393 You may wonder why you need to use the form which dutch shows. When you basically understand how templates work, I suggest reading the basics about STL containers:

http://www.martinbroadhurst.com/stl/stl_introduction.html
The form used in @leon4393's post is like that in Stroustrup's "home made" include covered in his book "Principles and Practice", but of course such a call requires that header, which he advocates.

I think new students may tend to forget it comes from that header.
Oooh, good observation @Niccolo.

leon4393: If you're following Principles and Practice (2nd edition), then you'll probably want to download and #include this: http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Stroustrup wrote:
Note that different compilation systems and programmer communities have different conventions for where to put header files. The book assumes that a header file is in the same directory/folder as the .cpp files and uses "plain" #include "std_lib_facilities.h" . If that doesn't work, try #include "../std_lib_facilities.h" (one level up) and #include "../../std_lib_facilities.h" (two levels up).


-Albatross
Last edited on
It's all good about sort() ... but how does this loop come to an end?
1
2
	for (double temp; cin >> temp;)
		temps.push_back(temp);


And why is there a "/ 2" in finding the average?

Median is also a bit wrong for an even number of temperatures.


Last edited on
So operator>> (istream&, something_else) typically returns the passed-in istream to allow operator chaining. You probably knew that.

However! istreams implement operator bool as of C++11, which lets them be used in boolean contexts. In this case, it's effectively the same as using !(cin >> temp).fail().
More info: https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool

-Albatross
Mmm, but trying it in cpp.sh I couldn't stop the input ... other than hitting ctrl-C, which promptly stopped the program.

EDIT: Entering a letter, then hitting enter worked, but it doesn't seem a tidy way of concluding input, as the stream would have to be reset.
Last edited on
Normally, programs that loop on a seemingly infinite loop of user input are meant to be piped from other programs (e.g. grep).

cpp.sh doesn't let you enter the end-of-input key, which would be Ctrl + Z (and then Enter) on Windows, and Ctrl + D on *nix.
With cpp.sh you can select "Text" for standard input and type a few lines in the text box, which will act like an input file, complete with eof signal.
Ah, I didn't notice that option, thanks.
Topic archived. No new replies allowed.