|
Hi Saurabh,
Yes indeed that makes things crystal clear! - thank you very much for a brilliant answer. In my head I somehow equated iterators with variables. But, then, I am an idiot so that's not particularly surprising. What do iterators do? They iterate, of course: 1,2,3,4,etc. etc. So if iterators are passed you aren't passing fixed values but the ability to process through a range that they contain. How could I be so thick??
Thanks again for your patience and a really clear and concise answer.
Si
|
|
|
|
|
Hey Oliver,
You are most welcome. You got it correctly. Iterators iterate over a container and more importantly they iterate through a given range. So using iterators you can process only a part of array which is not possible when passing array itself and its size without extra arguments.
-Saurabh
|
|
|
|
|
Hi Saurabh,
OK here's the code I've knocked together as a solution:
#include "stdafx.h"
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <vector>
using namespace std;
template<typename inputiterator>
typename InputIterator::value_type median(InputIterator begin, InputIterator end)
{
vector<typename> v;
typedef typename vector<typename>::size_type vec_sz;
//After the while loop, below, begin == end so can't use the while loop again.
//So chuck everything in a vector and work on that
//Does this save processing over simply passing a vector into
// the template?
//Is this the way it should be done??
//Who knows - all I know is it works and I can finally move on
//Some people do this for fun, you know...
while (begin != end)
{
v.push_back(*begin);
begin++;
}
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 <int> v2;
v2.push_back(3);v2.push_back(13);v2.push_back(7);v2.push_back(5);v2.push_back(21);v2.push_back(23);
v2.push_back(23);v2.push_back(40);v2.push_back(23);v2.push_back(14);v2.push_back(12);
v2.push_back(56);v2.push_back(23);v2.push_back(29);
//median of v2 shuld be 22. See http://www.mathsisfun.com/median.html
cout << "The median of the int vector v2 is " << median(v2.begin(),v2.end())<< endl;
system ("pause");
return 0;
}
The only niggle is that after using the while loop to get the size, 'begin' equalled 'end' and so I couldn't iterate through again to find the mid point. In the end I created a vector and put everything in there. This kind of made sense but I can't help feeling I'm missing something by using another vector... perhaps there is a way to reset the iterators after they have been used once (i.e. in the while loop)
Cheers
Si
|
|
|
|
|
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) 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
|
|
|
|
|
Of course - begin and end reference the place in the vector as well as the contents of that place. Superb - I think that covers that question pretty completely.
Thanks once again, Saurabh - you've saved me a whole load of time trying to sort this one out.
Question 8-4 next...what's the betting I'll be back on here tomorrow !!
Cheers,
Si
|
|
|
|
|
You are most welcome Oliver.
-Saurabh
|
|
|
|
|
Hi,
I was just trying to get one of the sample programs compiled. It has customized entry point function configured using directive
#pragma comment (linker, "/entry:\"something\"") which the compiler didn't seem to recognize, it returned something like "error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup LIBCMTD.lib". How can I get this right? I'm using VS 2005.
Thanks,
|
|
|
|
|
I think the problem is in the syntax. There is no need for quotes for the function name. Try this.
#pragma comment(linker, "/entry:something")
-Saurabh
|
|
|
|
|
how to write other language font(text) in edit control not english
Trioum
|
|
|
|
|
|
I need code in c++ not in .net
Trioum
|
|
|
|
|
Hi...
Install new font(what u would like to see in u r edit control)
1) Create variable for Edit control m_font.
CEdit m_ctrlEdit;
2) Create varible for CFont
CFont m_font;
3)Initialize with new Font..
m_font = CreateFont(,.....,....,m_fontName) /* see in MSDN
m_edit.SetFont(&m_font);
Try this..
I am able to display "xxxxxxx.ttf" with this appproach..
|
|
|
|
|
Oh it was my mistake anyway you can use of CFont class of MFC and then use of SetFont.
|
|
|
|
|
Here is a code snippet about CListCtrl from MSDN. And I don't think MSDN give correct code.
int index = m_myListCtrl.GetTopIndex();<br />
int last_visible_index = index + m_myListCtrl.GetCountPerPage();<br />
if (last_visible_index > m_myListCtrl.GetItemCount())<br />
last_visible_index = m_myListCtrl.GetItemCount();<br />
<br />
while (index <= last_visible_index)<br />
{<br />
<br />
index++;<br />
}
IMO, variable last_visible_index is more than one. So the While statement should be written like this:
while (index < last_visible_index)
Am I wrong? Or MSND?
Thank you all!
A Chinese VC++ programmer
|
|
|
|
|
Your Question:
Is MSDN wrong?
You asked:
Am I wrong? Or MSND?
What is wrong? you...?
Anyway, Did you implement this code?
Mukesh Kumar
Software Engineer
|
|
|
|
|
Yes, I didn't run this code. But look at this :
last_visible_index = m_myListCtrl.GetItemCount();
The max index of a CListCtrl is m_myListCtrl.GetItemCount() - 1 not m_myListCtrl.GetItemCount().
Any way ,thank you!
A Chinese VC++ programmer
|
|
|
|
|
Mukesh Kumar wrote: What is wrong? you...?
Nope thats why asking the question!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Could you please, post the link to the MSDN page?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
|
Well I think you're right and MSDN example is wrong. Unfortunately at the moment I have no time to test it, but I'll do in a day or two.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
O,yeah!
Thank you!
I'll test it tomorrow.
A Chinese VC++ programmer
|
|
|
|
|
It might actually be right.
I assume that GetCountPerPage includes only complete items to be shown, however the click might occur at a partially visible additional item.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist
|
|
|
|
|
Thank you peterchen
You're right, GetCountPerPage only returns the items that completely visible. But this has nothing to do with this bug!
I've test this code, and I'm sure that this code is bugsome.GetItemRect will return FALSE when index is GetItemCount(); I've reported this to MS.
A Chinese VC++ programmer
|
|
|
|
|
you are right, either the limiting condition should be changed to
if (last_visible_index > =GetItemCount())
last_visible_index = GetItemCount()-1;
or the return value of GetItemRect should be checked.
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist
|
|
|
|
|
Hi all,
I am Savtri here.I am having 3 field in table,Slnumber,Name, and PhoneNumber.Slnumber is of type Autonumber, and Name and phonenumber are text.when I add New element then it is incrementing.but when i am deleting it is not decrementing it means if i have 5 values like 1,2,3,4,5.if delete 3rd value then my table cantains 4 values.so please tell me How Shall i do it.I am doing Updation also.But I want ot Update the both the fields name and Phonenumber and in where clause i want to check slnumber.Like SqlString="UPDATE ADDRESBOOK SET name="" AND Phonenumber="" WHERE SlNumber=""";But Not getting How TO GET THAT SLNUMBER FROM THE TABLE into the variable.
PLEASE TELL ME.HELP ME.
Thanks in Advance,
savitri
|
|
|
|