|
I would have to agree. The only problem with vector is that it is implemented as a contiguous block of memory, like an array. If you keep growing the vector, it becomes expensive to keep adding elements since it has to constantly keep allocating and moving things around to keep it contiguous. A list would solve that, but of course you lose the random access iterator. In addition, repeated sorts on a vector consume more CPU cycles than say a list since a vector has to shuffle data around whereas a list would just shuffle pointers.
Use reserve() right away with your vector to pre-allocate a specific number of elements. This will save time overall since it can immediately allocate its chunk in one fell swoop as opposed to constantly having to re-allocate and move the existing chunk every time you do a push_back as the vector begins to grow. If you have a rough idea of how many elements will be required, you can play with this reserved amount to get the right mix of as few resizes as possible during the life of the vector.
For example, if you grow your vector from 1 to 1024 elements, it would take 11 resizes if no initial reserve is set. If you set the reserve to 512, there would only be two resizes. The disadvantage is the "wasted" memory as the vector grows to reach 512 elements. Memory versus CPU time is the age-old quandary
Ty
"The significant problems we face cannot be solved at the same level of thinking we were at when we created them." -Albert Einstein
|
|
|
|
|
Thomas George wrote:
If it requires to be inserted somewhere else I push_back and call a sort. I get indexed access to the lis
Don't sort every time you add - this gives you O(n*log(n)) complexity, which will be a performance killer. Assuming that vector is sorted before insertion, you may find the right place for insert in O(n) time. std::upper_bound even will find the index for you.
Tomasz Sowinski -- http://www.shooltz.com
*** Vodka. Connecting people. ***
|
|
|
|
|
that is cool!
I never used upper_bound before because i never knew that I can use a predicate for comparison.
Thank you
Thomas
modified 29-Aug-18 21:01pm.
|
|
|
|
|
You could use a vector of pointers to your objects. That way resizing the array doesn't get to costly.
Todd Smith
|
|
|
|
|
They are already pointers.
But, Thanks for the reply
I lOVE CP. Always gets so many tips and suggestions.
Thomas
modified 29-Aug-18 21:01pm.
|
|
|
|
|
|
Hi,
I have a Visual C++ MFC Dialog based application. And I would like to include a .c and .h file in the project.
When I tried to compile this project I had the following error:
Unexpected end of file while looking for precompiled header directive, fatal error C1010.
If anyone could help me with regards including c-code files in my project.
Thanking you in anticipation
|
|
|
|
|
You need to use automatic use of precompliled headers. Select project/settings (or ALT+F7) then, in the Project Settings Dialog, select the C/C++ tab, then select the Precompiled Headers item in the combo box, and select the Automatic use of precompliled headers option.
Regards
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
The project has set the c file to use a precompiled header (through stdafx.h).
Since the c file doesn't have an #include "stdafx.h" the compiler gets an error searching for it.
You just need to turn this option off for the file, through the file properties.
Hope this helps,
Lan
|
|
|
|
|
also #include the "stdafx.h" file inside your c file.
Or turn off the use of precompiled headers.
|
|
|
|
|
It's working, thank you all!!!
|
|
|
|
|
Hi,
Is it possile to tell VC++ that I wish to save all debug file to ONE directory. I dont like deleting this files from Debug directories . Id like to delete only one with all files !!
Wiizi
|
|
|
|
|
You can delete the files, using the menu item Clear....
Regards
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
Where is it ? I cannot find this :/
Wiizi
|
|
|
|
|
i guess he wanted to say Clean, its under build menu
but i prefer to find every "debug" using the find files or folder then CTRL + A then SHIFT + DELETE!
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
He probably means Clean. UNder the Build menu look for Clean. It shoudl be in there.
Regards,
Brian Dela
|
|
|
|
|
I'm sorry, it's Build/Clean....
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
Is the windows message queue thread safe? That is, can I have two threads using PostMessage() into a windows message pump?
J
|
|
|
|
|
jamiehale wrote:
can I have two threads using PostMessage() into a windows message pump?
Yes. Just remember that thread can have its own message queue. Are your threads queue-less (worker threads) or do they have one (UI threads)?
Tomasz Sowinski -- http://www.shooltz.com
*** Vodka. Connecting people. ***
|
|
|
|
|
I'm trying to decide if I can use my main thread (dialog app) to process asynchronous messages from clients, or if I need to start another thread to handle them. I was curious as to whether or not it would be safe to have another thread posting messages to the main thread - or alternatively, several threads posting to a message queue in yet another thread.
I think, regardless, I'm going to start another thread and build my own message pump (custom, not windows) simply because of the nature of the app...
J
|
|
|
|
|
In
_ClRemoteButton p;
p.CreateDispatch("DllwithForm.Class1");
p.CheckingPB(CMHandle,0,500);
AfxMessageBox ("back from dll");
CheckingPB is a function in a dll and it Sendmessages (not Postmessages) say 100 times in a loop.
When does the messagebox fire? After the function returns? Or as soon as I have called
p.CheckingPB(CMHandle,0,500);
, the code advances to the messagebox? I dont have any threads or anything.
Will I get the 100 messages before the messagebox? I cant really tell in my sample and need to know for sure..
Thanks,
ns
|
|
|
|
|
You have coded:
p.CheckingPB(CMHandle,0,500);
AfxMessageBox ("back from dll");
So AfxMessageBox() is called after CheckingPB() has returned.
And as you have used SendMessage() your messages, you can be sure that they have been handled when the messagebox fires.
|
|
|
|
|
So I dont need to send a message from the dll saying its done! It'll do all its stuff , and then after that the code carries on. THis is good news!
Thanks,
ns
|
|
|
|
|
Correct.
However, if you'd used PostMessage , a hundred messages would be posted, the function would return, the message box would fire, and when Windows got a chance to (eventually) pump its message queue, the 100 posted messages would be handled.
So you're doing the right thing by using SendMessage .
/ravi
Let's put "civil" back in "civilization"
http://www.ravib.com
ravib@ravib.com
|
|
|
|
|
My progressCtrl needs to be cleared or hidden away after its done. I dont see any function that will do it in that class. Also is it possible to send a message with Sendmessage that will clear the ctrl?
I am sending PBM_STEPIT messages from a dll, and am wondering how to notify the control that the operation is finished. I cant set the range before hand so it cycles through a few times. Any ideas on how one would notify it that the process in the remote dll is done ? Basically each time the dll function steps through a for loop, it sends off a PBM_STEPIT to the calling apps progressbar. At the end of the loop the progressbar needs to know all is done, and magically clear or vanish.
Appreciate any ideas..
Thanks,
ns
|
|
|
|