Vector -

Hi guys,

I am following exercise from Mr Stroustrup book Programming Principles and practice using C++ page 122, there is example about temperature.
however I need second pair of eyes, I am missing something.
I have custom header included. (I am trying not to use using namespace std;
and I typed code I think without any mistakes. However, my VS is complaining about problem with function sort(). it should have two parameters, were as in book there is only one temps.

where is my mistake?

thanks :)

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


int main()
{
	vector<double> temps;
	for (double temp; std::cin >> temp;)
		temps.push_back(temp);
	double sum = 0;
	for (double x : temps) sum += x;
	std::cout << "Average temperature: " << sum / temps.size() << '\n';
	
	std::sort(temps); 
	std::cout << "Median temperature: " << temps[temps.size()/2] << '\n';
	
	system("pause");
	return 0;
}
I have custom header included. (I am trying not to use using namespace std;

Do you realize that that custom header is "using namespace std;"?

However, my VS is complaining about problem with function sort(). it should have two parameters, were as in book there is only one temps.

That header also has a sort() function with only one parameter that is not in the std::namespace. I suggest, for now, you avoid using the std:: specifier.

Also be careful, that header also "modifies" std::vector and std::string to add range checking.

Lastly that header already #includes most, if not all, headers you should need for programs contained in the first dozen or so chapters.

Hi Jlb

I did remove most of the #include apart from custom library, however sort still is showing as error. I looked inside std_lib_facilities and there is nothing about sort().

any other clue?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "stdafx.h"
#include "std_lib_facilities.h"
//#include <iostream>
//#include <vector>


int main()
{
	vector<double> temps;
	for (double temp; cin >> temp;)
		temps.push_back(temp);
	double sum = 0;
	for (double x : temps) sum += x;
	cout << "Average temperature: " << sum / temps.size() << '\n';
	
	sort(temps);
	cout << "Median temperature: " << temps[temps.size()/2] << '\n';
	
	//system("pause");
	return 0;
}
Last edited on
IMO that header "std_lib_facilities.h" should be an all or nothing file. Either you #include it to use it's features, including the #include statements or you don't #include it and then you don't try to use any of the features from that header. Also IMO that header should be after any of the standard #include statements.

As I said the original header file defined a function named sort() with one parameter. If you removed that portion of the header you will need to find the documentation for and use std::sort instead if his "helper" function. By the way this is the code from the original header:

1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename C>
	// requires Container<C>()
void sort(C& c)
{
	std::sort(c.begin(), c.end());
}

template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
	std::sort(c.begin(), c.end(), p);
}


And if you decide to use that #include file, make sure you have the #include file that matches the book revision, there may be differences in this header for the different book revisions.



Last edited on
Thanks Jlb,

Normally I don't use that header file, at the beginning I didn't know how to use it (ie where to put it how to add to VS, later I didn't need to :D) however with this example sort function didn't work so I was trying other things and finally i gave it a go with header from website with address (or at least I tought it was ) from my book:

http://www.stroustrup.com/Programming/std_lib_facilities.h

Heh Jlb - thank you!! I just found solution to my problem, thanks to your quote about different versions of the file, i started looking on the site again and found this:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
inside that file there is actually sort() function :)

it definitley saved me a lot of time :D

thank you
Last edited on
Topic archived. No new replies allowed.