Click here to Skip to main content
15,911,786 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalstrtol and errno Pin
bob1697226-Apr-08 6:14
bob1697226-Apr-08 6:14 
GeneralRe: strtol and errno [modified] Pin
Ozer Karaagac26-Apr-08 11:54
professionalOzer Karaagac26-Apr-08 11:54 
GeneralRe: strtol and errno Pin
bob1697227-Apr-08 3:42
bob1697227-Apr-08 3:42 
GeneralRe: strtol and errno [modified] Pin
Ozer Karaagac27-Apr-08 5:09
professionalOzer Karaagac27-Apr-08 5:09 
GeneralRe: strtol and errno Pin
bob1697227-Apr-08 8:54
bob1697227-Apr-08 8:54 
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 
Hi Saurabh & thanks for replying. Actually, the assumption I was making,probably wrongly, was that only people familiar with the book would reply but perhaps I should have made that more clear; in any case apologies.

To make the question more general - we have a template function that takes a vector as an argument:

template <class T>
T median(vector<T> v)
{...}


The task is this: instead of passing the vector ('v')by value, rewrite the function to operate on iterators.

This is probably v. trivial but has me stumped.

Here's the code I have so far if anyone's interested


#include "stdafx.h"
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <vector>

using namespace std;

template <class T>
T median(vector<T> v)
{
typedef typename vector<t>::size_type vec_sz;
vec_sz size = v.size();

if (size == 0)
throw domain_error("median of an empty vector");

sort(v.begin(), v.end());
vec_sz mid = size/2;

return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];
}

int main()
{
vector <float> v1;
vector <int> v2;

for (int i=0; i < 10; i++)
{
v1.push_back(i+5);
v2.push_back(i+5);
}
int size = median(v1);
double sf = median(v1);

cout << "The median of the float vector v1 is " &lt;&lt; median(v1) &lt;&lt; endl;
cout << "The median of the int vector v2 is " &lt;&lt; median(v2)&lt;&lt; endl;
system ("pause");
return 0;
}


I think what I'm asking is that if the vector isn't present in the template function, how can iterators like begin() & end() possibly work on it?

Also, why does C++ have to be so awkward and obscure and frustrating (actually no reply needed to that - I know you all love it and I don't want to start a row).

Thanks
Si
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 
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 

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.