|
Someone on the VCF forums posted this, which I found a little strange, and was wondering if anyone here knew anything more:
"Also, when iterating an STL iterator, pre-increment (++it) should be preferred to post-increment (it++) as the latter creates a copy of the iterator."
Is this really true?
If I have code like:
vector<int> vec;
vector<int>::iterator it = vec.begin();
while ( it != vec.end() ) {
int& i = *it;
i = i * 45;
it ++;
}
Should I change it to:
vector<int> vec;
vector<int>::iterator it = vec.begin();
while ( it != vec.end() ) {
int& i = *it;
i = i * 45;
++ it;
}
¡El diablo está en mis pantalones! ¡Mire, mire!
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
SELECT * FROM User WHERE Clue > 0
0 rows returned
|
|
|
|
|
Yes this is true. Similarly with the pre and post-decrement.
post-decrement makes a copy of the iterator.
Basically post-increment and post-decrement move the itterator by one position (forwards or backwards) and return an unmoved copy.
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
In addition to Antony's reply, I myself have gotten into the habit of using pre-increments as well, but as I recall from a previous post on this topic, you will only see any performance benefit (if at all), on complex structures. Basic data types such as integers, characters and possibly simple class structures may not improve the performance either way.
I Dream of Absolute Zero
|
|
|
|
|
Well, based on what Andrew Koenig says here[^], I would suggest you do this:
vector vec;
vector::iterator it = vec.begin();
while ( it != vec.end() )
{
int& i = *(it++);
i = i * 45;
}
This way you increment the iterator AND use the result of the post-increment in one step.
~Nitron.
ññòòïðïðB A start
|
|
|
|
|
That's a good example and I agree. I've always had a problem with those who say that you're going to get some huge benefit to using pre-increment. But I will admit, by default, I always think pre-increment first, but if a post increment allows me to combine two lines of code into one I'll do that. In the example, post increment is critical. If you did a pre increment in that example, the behavior would be totally wrong as the increment would occur before the multiplication!
It's such a crapshoot to try and figure out what the compiler is going to do for you with all of the advanced compiler options. Make your code work correctly, first and foremost and if your performance meters indicate problems you can always refactor a bit during testing.
Regards,
Shawn
|
|
|
|
|
In general it doesn't matter. But the problem is that you would have to know the implementation to be 100% sure.
Here is the p-code for a pre and post increment
post:
value v = ix;
ix = ix + 1;
return v;
pre:
ix = ix + 1;
return ix;
In 99.99% of the cases, the compiler isn't stupid and realizes it doesn't even need the return type and thus never even saves it. However, if your iterator is complex (something more than just a pointer), then the compiler might not be able to optimize away the extra constructor.
Get use to pre-increment. However, as someone pointed out, it isn't always the best thing either.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
When you use POD then its ok to use i++, but if you use objects that supports pre-increment its best to use ++i as this does not create a temporary copy while i++ does, like stl iterators are best suited to ++i.
|
|
|
|
|
|
That's OK!
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
yingkou wrote:
I have made a mistaken
Yeah that happens sometimes. I made one back in 1987.
"No matter where you go, there your are." - Buckaroo Banzai
-pete
|
|
|
|
|
Sorry to interupt you,it is said that two programs in different computer can communicate by named piped in the book of Programming Application For Windows,
can you tell me the details?
thanks a lot
|
|
|
|
|
yingkou wrote:
can you tell me the details?
Apart from the book, a good source is MSDN.
Also, Google can be a great help.
Bikram Singh
I believe we should all pay our tax with a smile. I tried - but they wanted cash.
|
|
|
|
|
I have a problem to use a DLL done in Vfp 8 with a code in VC++.
The source codes is below.
The message-> Warning: attempt to call Invoke with NULL m_lpDispatch!
is showed when the canal.processar routine is called.
Somebody can help me????
PauloTharso
ptharso@hotmail.com
***********************teleway.cpp -----------------------------------------------------
#include "stdafx.h"
#include "Teleway.h"
#include "canalfoxdll.h" // Added by ClassView
#include "k3l.h" // Added by Paulo
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinApp theApp; // The one and only application object
using namespace std;
Icanalfoxsrv canal; byte Info[20]; int key;
int32 Kstdcall Processador ( int32 Obj, K3L_EVENT *E ){ cout << "Processando eventos\n";
if (E->ObjectId == koiChannel || E->ObjectId == koiPlayer){
memcpy(Info,(char *)E + 28,E->ParamSize);
key = canal.processar(E->DeviceId,E->ObjectId,E->Code,E->AddInfo,E->ParamSize,(const char *)Info); // chamafox
if(!key) cout << "OK!"; else cout << "Falha no canal: " << E->ObjectId << endl;
}
return TRUE;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; int check=FALSE;
Icanalfoxsrv * canal = new Icanalfoxsrv;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}else{
CString strHello; strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
if (check!=TRUE) { char *Ret = k3lStart();
if (!Ret || !*Ret) k3lRegisterEventHandler( Processador );
check = TRUE;
}
// K3L_EVENT * wrk = new K3L_EVENT ; wrk->Code=1; wrk->ObjectId=1; wrk->ParamSize=8;
// Processador(2,wrk);
}
return nRetCode;
}
***********************canalfoxsrv.dll--------------------------------------------------
DEFINE CLASS canalfoxsrv as Session olepublic
PROCEDURE processar(p as Long,c as Long,e as Long,r as Long,l as Long,a as String ) as Integer
IF p<1 OR c>59 OR c<0 THEN
RETURN 1
ENDIF
INSERT INTO canalfoxtbl (placa,canal,evento,parametro,tamanho,adicional) VALUES (p,c,e,r,l,a)
RETURN 0
ENDPROC
PROCEDURE acionar(o as Long,c as Long, a as String @) as Integer
DECLARE INTEGER k3lSendCommand IN "K3LE1600" as Comandar STRING @
buffer=SPACE(50)
Comandar(@buffer)
RETURN 0
ENDPROC
PROCEDURE init
SET DEFAULT TO "c:\avisual\teleway"
SET DATE TO ANSI
ENDPROC
-----------------------------------------------------------------
|
|
|
|
|
i want to handle the situation when someone logs in to the system. how do i do so?
i know WM_ENDSESSION message is sent when the user logs out. but wat is the message when the user logs in....
|
|
|
|
|
Short of making your own msgina.dll, I know of no such message. Applications typically don't need to be notified when someone logs in, mainly because they probably aren't running. Services, however, might since they are running regardless of whether someone is logged in or not.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
|
Hey,
Can any one help me, how I can get Single Doc/View based application without menu bar? I don't want to switch to Dialog based application ......
Thanks for your help!
Suresh
|
|
|
|
|
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if(cs.hMenu!=NULL)
{
::DestroyMenu(cs.hMenu);
cs.hMenu = NULL;
}
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
return TRUE;
}
Michael
CP Blog [^]
|
|
|
|
|
Got this! Thanks
|
|
|
|
|
I am using a standard algorithm coded from Visual C++.net
The heart of it is a formula I need to modifyfrom time to time.
As I need flexibility regarding this core formula, I would like to write it in an Excel cell, take the string via VBA and transfer it the heart of my C++ code in order modify calculation inside my algorithm.
It looks like an "uninteresting question" but solving that one could much simplify my daily life...
(How ) Is it possible to do that ?
Guillaume
|
|
|
|
|
Well you could expose a COM interface as part of your app that is consumable by VBA. That would probably be quite a load of work. Another approach would be to add interfacing with the Excel Object Model from within your app. While that would likely be much less work I cannot tell if it meets the needs of your use model.
Good luck
"No matter where you go, there your are." - Buckaroo Banzai
-pete
|
|
|
|
|
Yesterday (or the day before, I can't remember )
I asked how to print a CString (with RTF codes)
Here's the basic solution.
create a 'dummy' CRichEditCtrl.
create a 'dummy' Window to contain this control.
set the RTF text in the CRichEditCtrl.
Print from there.
That's it (hope it's helpful to anybody)!
(PS: not possible to give the code, because it's to hard to strip)
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
Hi,
I'm new in using windows sockets.
when I try to use windows sockets in my MFC application, I get the error "Could not start WinSock.DLL, error=0x276b".
in the InitInstance method, I have initialized winsock like this:
...
WSADATA ws;
if (!AfxSocketInit(&ws))
{
return FALSE;
}
...
event if I use the WSAStartup function, I get the messagebox error.
did I forgot to initialize something?
thanks for the help.
best regards.
There is no spoon.
|
|
|
|
|
I have not used AfxSocketInit but the the error it is reporting is
WSAStartup cannot function at this time because the underlying system it uses to provide network services is currently unavailable.
So WSAStartup is failing in both cases.
This is only speculation but you do have a network card in your machine don't you?
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
Hi,
In fact, it's a problem of linking. when I link with ws2_32.lib and mswsock.lib instead of wsock32.lib , it works fine. so it seems that wsock32.lib does not work properly?
There is no spoon.
|
|
|
|
|