|
Thanks Stuart. I moved it to C / C++ / MFC. Hope that will help.
|
|
|
|
|
OK I found a sort algorithm on Wikipedia, that is done like the std::sort so I figured why not convert the who lot that I have to STL. Blah.
First the new sort
template<class ForwardIterator> void comb(ForwardIterator first, ForwardIterator last){
static const double shrink_factor = 1.247330950103979;
typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_type;
difference_type gap = std::distance(first, last);
bool swaps = true;
while ( (gap > 1) || (swaps == true) ){
if (gap > 1)
gap = static_cast<difference_type>(gap/shrink_factor);
swaps = false;
ForwardIterator itLeft(first);
ForwardIterator itRight(first); std::advance(itRight, gap);
for ( ; itRight!=last; ++itLeft, ++itRight ){
if ( (*itRight) < (*itLeft) ){
std::iter_swap(itLeft, itRight);
swaps = true;
}
}
}
}
So now the old bubble sort
template <typename T> void bubble(std::vector<T> &a) {
size_t size = a.size() - 1;
for (int pass = 0; pass < size; pass++) {
for (int n = 0; n < size; n++) {
if (a[n] > a[n+1])
std::swap(a[n], a[n+1]);
}
}
}
and this is as far as I have gone so far
template<class ForwardIterator> void bubble(ForwardIterator first, ForwardIterator last){
typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_type;
difference_type size = std::distance(first, last);
}
http://www.contract-developer.tk
|
|
|
|
|
How's this?
template <typename T>
void bubble(std::vector<T> &a)
{
typedef typename std::vector<T>::iterator Iter;
Iter last = a.end()-1;
for(Iter pass=a.begin();pass!=last;++pass)
for(Iter n=a.begin();n!=last;++n)
if (*n > *(n+1))
std::iter_swap(n, n+1);
}
or this, which doesn't keep re-sorting the items at the end of the vector that have already been sorted…
template <typename T>
void bubble(std::vector<T> &a)
{
typedef typename std::vector<T>::iterator Iter;
for(Iter last = a.end()-1;last!=a.begin();--last)
for(Iter n=a.begin();n!=last;++n)
if (*n > *(n+1))
std::iter_swap(n, n+1);
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
After a few moments looking over the book I figured out that I need to watch the pointers close when using the STL method of iterators.
This is not well covered in any of the books I have. No big deal, that is what forums are for.
Here is what I ended up with that does work properly.
template <typename iterator> void bubble(iterator first, iterator last) {
iterator i, j;
for (i = first; i != last; ++i)
for (j = first; j < i; ++j)
if (*i < *j)
std::iter_swap(i, j);
}
Now I have this other sort that is annoying me.
template <class iterator> void gnome(iterator first, iterator last){
iterator pos = first;
iterator temp;
while (pos <= last) {
if ((pos == first) || (*pos >= *(pos-1)))
++pos;
else {
std::iter_swap(pos, pos-1);
--pos;
}
}
}
This one does not work, not sure what the problem is.
http://www.contract-developer.tk
|
|
|
|
|
What iterators are you calling it with?
This sample program seems to sort the data OK…
#include <vector>
#include <iostream>
#include <iterator>
template <class iterator> void gnome(iterator first, iterator last){
iterator pos = first;
iterator temp;
while (pos <= last) {
if ((pos == first) || (*pos >= *(pos-1)))
++pos;
else {
std::iter_swap(pos, pos-1);
--pos;
}
}
}
int main(int, char**)
{
std::vector<int> vec;
vec.push_back(4);
vec.push_back(3);
vec.push_back(5);
vec.push_back(-10);
vec.push_back(13);
vec.push_back(2);
vec.push_back(1);
gnome(vec.begin(), vec.end()-1);
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
here is the call routine
void RunGnomeSort(void) {
size_t size = 10000;
std::vector<int> data;
data.resize(size);
for (int i = 0; i < size; i++)
data[i] = (int)rand();
sort::gnome(data.begin(),data.end());
data.resize(0);
}
http://www.contract-developer.tk
|
|
|
|
|
Right - it's this line: sort::gnome(data.begin(),data.end());
Like in my example, that should be sort::gnome(data.begin(),data.end()-1);
data.end() points to the element after the last element in the vector, and the sort routine explicitly accesses the last parameter.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Then I guess I need to change the function a tad, so that its a drop in comparable to std::sort.
I changed the code
while (pos < last) {
from <=
That has it working now, thanks for spotting that. Now I think I will post the code on Wilipedia as the algorithm is not super clear.
http://www.contract-developer.tk
|
|
|
|
|
hello
On WM_MOUSELEAVE message the below function OnMouseLeave() is not getting called... (however OnInitDialog() is getting called)
I have created a dialog box and wanted to call my function OnMouseLeave() on WM_MOUSELEAVE message. Declared a message map as below,
BEGIN_MSG_MAP(CMyClass)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave)
END_MSG_MAP()
any idea if am missing something ?
Thanks and Regards,
Supriya Tonape
|
|
|
|
|
|
Just for completeness, the best place where you could put the TrackMouseEvent function, is inside the handler of the WM_MOUSEENTER message
|
|
|
|
|
Thank you. Yes I am using the trackmouseevent in OnMouseLeave() method.. actually i just replaced message as "WM_MOUSEMOVE" and it started working.. but thanks much for the help.
Also I cud not find WM_MOUSEENTER message in respective .h file..
thanks and regards,
Supriya Tonape
|
|
|
|
|
also I used TME_LEAVE to check mouse actions in trackmouseevent structure initialization.. in case if somebody faces the prob as I had..
|
|
|
|
|
How can my CDerivedEdit respond WM_SHOWWINDOW?
(OnShowWindow)just like MFC.
class CMouseValueEdit
: public CWindowImpl<cmousevalueedit, cedit="">
{
public:
DECLARE_WND_CLASS(_T("CMouseValueEdit"));
BEGIN_MSG_MAP_EX(CMouseValueEdit)
MESSAGE_HANDLER(WM_SHOWWINDOW, OnShowWindow)
MSG_WM_SHOWWINDOW(OnShowWindow)
//MESSAGE_HANDLER(WM_CTLCOLOREDIT, OnCtlColorEdit)
//MSG_WM_PAINT(OnPaint)
//MESSAGE_HANDLER(OCM_CTLCOLOREDIT, OnCtlColorEdit)
MSG_OCM_CTLCOLOREDIT(OnCtlColorEdit)
//REFLECTED_NOTIFY_CODE_HANDLER( WM_SHOWWINDOW, OnShowWindow)
DEFAULT_REFLECTION_HANDLER()
//CHAIN_MSG_MAP(CWindowImpl<cmousevalueedit, cedit="">)
END_MSG_MAP_EX()
CMouseValueEdit& operator=(HWND hWnd)
{
//m_hWnd = hWnd;
SubclassWindow(hWnd);
return *this;
}
}
class CMainDlg : public CDialogImpl<cmaindlg>, public CUpdateUI<cmaindlg>
, public CMessageFilter, public CIdleHandler
, public CWinDataExchange<cmaindlg>
{
public:
CMainDlg();
virtual ~CMainDlg();
CMouseValueEdit m_editMouse;
enum { IDD = IDD_MAINDLG };
BEGIN_DDX_MAP(CMainDlg)
DDX_CONTROL_HANDLE(IDC_EDIT_MOUSE, m_editMouse)
END_DDX_MAP();
virtual BOOL PreTranslateMessage(MSG* pMsg)
{
return CWindow::IsDialogMessage(pMsg);
}
virtual BOOL OnIdle()
{
return FALSE;
}
BEGIN_UPDATE_UI_MAP(CMainDlg)
END_UPDATE_UI_MAP()
BEGIN_MSG_MAP_EX(CMainDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
COMMAND_ID_HANDLER(IDOK, OnOK)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
REFLECT_NOTIFICATIONS()
END_MSG_MAP_EX()
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();
//mp_editMouse->SubclassWindow(GetDlgItem(IDC_EDIT_MOUSE));
DoDataExchange();
// set icons
HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
SetIcon(hIconSmall, FALSE);
// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
UIAddChildWindowContainer(m_hWnd);
return TRUE;
}
}
OnShowWindow is not called.
I can't understand WTL...
|
|
|
|
|
Hi all ... I'm a 3rd year computer engineering student ... And i do have an idea but so far we learn how to make client-srever using " sockets " but the message queues is new method for me ... I'm required to do this project about how to develop authentication server that serves multiple clients at a time
the client gives the name and password as request and the server authenticates the request and returns the result ... ALL communications between client and server should use message queues techniques ...and i want it in c language ... Can u help me with the code ... appreciate it soo much ..
|
|
|
|
|
As a 3rd year student I am sure you understand the need to do your own research. We are not able to do your work for you but I will offer this link[^] to get you started. If and when you get a more specific problem then come back and ask for assistance.
It's time for a new signature.
|
|
|
|
|
ok thanx for the help .. appreciate it
|
|
|
|
|
Iam searching for one sample can any body help me out regarding this. I need to have 2 threads(Child Threads) acting on one ProgressBar(Main Thread) and have critical section Control on it for Syncronization. Please help me out regarding these.. or any sample... thanks in advance
|
|
|
|
|
If you are using the Windows ProgressBar[^] control then it is a simple matter of sending messages to the control to display what progress your application is making. No synchronisation is required except whatever is necessary between your threads.
It's time for a new signature.
|
|
|
|
|
Hi all,
Please let me know how to give the publisher name to my sample MFC application.
Currently it is displaying "Unknown" when i try to start it.
I am using Visual Studio 2008.
To see the publisher name -> Run an application as administrator in windows 7 machine and check the UAC dialog.
-Cvaji
|
|
|
|
|
You have already got the answer in the C/C++ forum...
Don't post the same question to multiple forums!
|
|
|
|
|
Hi everybody,
I am using shock wave flash control on a dialog. I able to play the movie file(.swf file) But I want to make the background as transparent while playing the swf file.There is an article named "transparent flash control in plain C++" that solved this problem, but the usage of cpu is too high.Does someone have a solution?
Thanks!
|
|
|
|
|
The performance of iceProjector is better than most other programs'. It's a good idea to transparent the flash with windows region.
|
|
|
|
|
I use ADO + MS SQL2000 ,I don't hope the window hang,when I read the records in the main thread.
So I use other thread to read the datebase.Just like this:
(if I didn't use thread,this record is the last one.)
void CxxDlg::UpdateListCtrl()
{
CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)StartRead,this,NULL,NULL);
Sleep(100);
}
DWORD WINAPI CxxDlg::StartRead(LPVOID lParam)
{
CoInitialize(NULL);
CxxDlg *pDlg = (CxxDlg *)lParam;
ASSERT( pDlg != NULL );
_variant_t vA,vB,vC,vD;
CString strTemp;
int nItem,nCount;
try
{
pDlg->m_List1.DeleteAllItems();
m_pRecordset.CreateInstance("ADODB.Recordset");
strTemp.Format("select * from db_somedate");
m_pRecordset->Open((_variant_t)strTemp,_variant_t((IDispatch*)
m_pConnection,TRUE),adOpenStatic,adLockOptimistic,adCmdText);
while(VARIANT_FALSE == m_pRecordset->adoEOF)
{
vA = m_pRecordset->GetCollect("A");
vB = m_pRecordset->GetCollect("B");
vC = m_pRecordset->GetCollect("C");
vD = m_pRecordset->GetCollect("D");
nItem = pDlg->m_List1.InsertItem(0,(_bstr_t)vA);
pDlg->m_List1.SetItem(nItem,1,1,(_bstr_t)vB,NULL,0,0,0);
pDlg->m_List1.SetItem(nItem,2,1,(_bstr_t)vC,NULL,0,0,0);
pDlg->m_List1.SetItem(nItem,3,1,(_bstr_t)vD,NULL,0,0,0);
m_pRecordset->MoveNext();
}
nCount = pDlg->m_List1.GetItemCount();
pDlg->m_List1.SetFocus();
pDlg->m_List1.SetItemState(nCount-1, LVIS_SELECTED, LVIS_SELECTED);
pDlg->m_List1.EnsureVisible(nCount-1,FALSE);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("Connect Datebase failed.\r\n\r\nError code:%s",e.ErrorMessage());
return DATE_FAILED;
}
return DATE_SUCCESS;
}
|
|
|
|
|
Hi,
I try to include
#include "stdafx.h"
class CSofphoneView : public CFormView ,public IDispEventSimpleImpl<1, CSofphoneView,&DIID__IUccEndpointEvents>
in App & View class its working
when I create new c++ class “CCallcontrol” without base class and include
#include "stdafx.h"
class CCallcontrol :public IDispEventSimpleImpl<1, CCallcontrol, &DIID__IUccEndpointEvents>
but its gets below error. Give me the suggestion .
c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlcom.h(4211) : error C2039: '_GetSinkMap' : is not a member of 'CCallcontrol'
1> d:\shanthakumar\sofphone\sofphone\callcontrol.h(9) : see declaration of 'CCallcontrol'
1> c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\atlcom.h(4210) : while compiling class template member function 'HRESULT ATL::IDispEventSimpleImpl<nid,t,pdiid>::Invoke(DISPID,const IID &,LCID,WORD,DISPPARAMS *,VARIANT *,EXCEPINFO *,UINT *)'
1> with
1> [
1> nID=1,
1> T=CCallcontrol,
1> pdiid=& DIID__IUccEndpointEvents
1> ]
1> d:\shanthakumar\sofphone\sofphone\callcontrol.h(11) : see reference to class template instantiation 'ATL::IDispEventSimpleImpl<nid,t,pdiid>' being compiled
1> with
1> [
1> nID=1,
1> T=CCallcontrol,
1> pdiid=& DIID__IUccEndpointEvents
1> ]
in “stdafx.h” included
#include "afxsock.h" // MFC socket extensions
#include "afxcview.h" // MFC declaration for CTreeView
#include "atlbase.h" // ATL Base
#include "atlcom.h" // ATL COM
#include "atlcoll.h" // for CAtlMap
#include "atlctl.h"
Thanks&Regards
shakumar
modified on Monday, July 5, 2010 3:43 AM
|
|
|
|
|