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

C / C++ / MFC

 
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 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question [modified] Pin
Saurabh.Garg27-Apr-08 2:49
Saurabh.Garg27-Apr-08 2:49 
Okay first lets consider an example using pointers. Lets say there is an array of ints.

int array[11];

Now let us pretend that this array is only of size 10. Last element is used to mark as end of the vector.

int* begin = &array[0];
int* end   = &array[10];

Now we can write a mean function as (I choose mean because its easier to show concept of iterators):
int Mean(int* begin, int* end)
{
   int _mean  = 0;
   int _count = 0;
   while(begin != end)
   {
       _mean += *begin;
       _count++;
       begin++;
   }
   return _mean/_count;
}

You will notice two things about this code. First, I have not passed array at all but I am able to compute mean. Second, the last location that we reserved in the array is used to find when to stop iterating an array. Thats it, this is the concept of STL iterators. Just imagine pointer is wrapped in an class and we call it iterator.

We can write mean function using generic container as:

template<typename InputIterator>
typename InputIterator::value_type Mean(InputIterator begin, InputIterator end)
{
   typename InputIterator::value_type _mean  = 0;
   int _count = 0;
   while(begin != end)
   {
       _mean += *begin;
       _count++;
       begin++;
   }
   return _mean/_count;
}

Now lets see the requirement in the type of InputIterator. begin and end are used in 3 places in Mean function - begin != end, *begin, and begin++. So the iterator class should should only support these operators. Notice all these three operations are supported by pointer as well. So you can call this mean function for arrays we well.

double points[11] = {0};
double mean = Mean(&points[0], &points[10]);


You can also do this:

std::vector<int> points;
points.push_back(1.0);
points.push_back(2.0);
points.push_back(3.0);
.
.
.
points.push_back(10.0);

double mean = Mean(points.begin(), points.end());


Hope this makes things clear.

-Saurabh

modified on Sunday, April 27, 2008 8:55 AM

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 
GeneralRe: Is MSDN wrong? Pin
ThatsAlok1-Jul-09 0:02
ThatsAlok1-Jul-09 0:02 

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.