Click here to Skip to main content
15,915,600 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionAcclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon26-Apr-08 5:49
oliversimon26-Apr-08 5:49 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg26-Apr-08 18:18
Saurabh.Garg26-Apr-08 18:18 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 2:09
oliversimon27-Apr-08 2:09 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question [modified] Pin
Saurabh.Garg27-Apr-08 2:49
Saurabh.Garg27-Apr-08 2:49 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 4:16
oliversimon27-Apr-08 4:16 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg27-Apr-08 6:54
Saurabh.Garg27-Apr-08 6:54 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 7:12
oliversimon27-Apr-08 7:12 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg27-Apr-08 7:57
Saurabh.Garg27-Apr-08 7:57 
Okay try this code.

template<typename InputIterator>
typename InputIterator::value_type median(InputIterator begin, InputIterator end)
{
	size_t _size = end - begin;
	if(_size == 0)
	{
		throw std::domain_error("median of an empty vector");
	}
	
	sort(begin, end);
	size_t mid = _size/2;
	return _size % 2 == 0 ? (*(begin+mid) + *(begin+mid-1)) / 2 : *(begin+mid);
}


sort function in STL needs that InputIterator be a model of RandomAccessIterator i.e. ith element in the array can be accessed using array[i]. Thus Inputiterator for Median should also be RandomAccessIterator. For random iterators we can determine size of array simply by subtracting last iterator from the first iterator (they behave exactly like pointers to array) Smile | :) Rest all is just like an array, simple pointer arithmetic. This function has a serious side effect that it sorts the original v2. It you don't want to sort it then try nth_element function in the <algorithm class="">.

-Saurabh
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 8:15
oliversimon27-Apr-08 8:15 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg27-Apr-08 8:25
Saurabh.Garg27-Apr-08 8:25 
Questionchange the program's entry point function Pin
LiYS26-Apr-08 3:56
LiYS26-Apr-08 3:56 
GeneralRe: change the program's entry point function Pin
Saurabh.Garg26-Apr-08 18:30
Saurabh.Garg26-Apr-08 18:30 
Questionfont problem Pin
trioum26-Apr-08 1:19
trioum26-Apr-08 1:19 
GeneralRe: font problem Pin
Hamid_RT26-Apr-08 4:16
Hamid_RT26-Apr-08 4:16 
GeneralRe: font problem Pin
trioum27-Apr-08 22:01
trioum27-Apr-08 22:01 
GeneralRe: font problem Pin
phanindra varma28-Apr-08 0:33
phanindra varma28-Apr-08 0:33 
GeneralRe: font problem Pin
Hamid_RT28-Apr-08 5:04
Hamid_RT28-Apr-08 5:04 
QuestionIs MSDN wrong? Pin
zengkun10026-Apr-08 1:09
zengkun10026-Apr-08 1:09 
GeneralRe: Is MSDN wrong? Pin
Mukesh Kumar26-Apr-08 2:26
Mukesh Kumar26-Apr-08 2:26 
GeneralRe: Is MSDN wrong? Pin
zengkun10026-Apr-08 2:42
zengkun10026-Apr-08 2:42 
GeneralRe: Is MSDN wrong? Pin
ThatsAlok1-Jul-09 0:02
ThatsAlok1-Jul-09 0:02 
GeneralRe: Is MSDN wrong? Pin
CPallini26-Apr-08 2:48
mveCPallini26-Apr-08 2:48 
GeneralRe: Is MSDN wrong? Pin
zengkun10026-Apr-08 2:54
zengkun10026-Apr-08 2:54 
GeneralRe: Is MSDN wrong? Pin
CPallini26-Apr-08 4:12
mveCPallini26-Apr-08 4:12 
GeneralRe: Is MSDN wrong? Pin
zengkun10026-Apr-08 6:06
zengkun10026-Apr-08 6:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.