|
|
Gurus,
If I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order?
All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing.
Does anyone know the most processor efficient way of doing this please ?
thanks,
Neil.
cheers,
Neil
|
|
|
|
|
They hide that information in the documentation[^]
Lower_bound is a version of binary search: it attempts to find the element value in an ordered range [first, last) [1]. Specifically, it returns the first position where value could be inserted without violating the ordering.
However that might not be the "most efficient" for your application. There are several things to consider. If you use STL much you should have Scott Meyers book as it discusses efficiency issues.
led mike
|
|
|
|
|
Thanks for that answer,
Neil
cheers,
Neil
|
|
|
|
|
led mike wrote: However that might not be the "most efficient" for your application.
It probably won't be - I don't think the structure of std::list iterators is terribly conducive to binary searches - could be wrong, mind.
|
|
|
|
|
Stuart Dootson wrote: It probably won't be - I don't think the structure of std::list iterators is terribly conducive to binary searches - could be wrong, mind.
std::deque would probably be a better choice in most cases.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
neilsolent wrote: f I have an STL list (doesn't matter what of) and I sort it, how can I add one element to the list, whilst maintaining the sort order?
All I can think of to achieve this is to either: (1) add the new element to one end, and run sort() again, or (2) iterate through the entire list, checking the sort predicate against each element. Neither of these seem particulalrly appealing.
Does anyone know the most processor efficient way of doing this please ?
If you insert only one element (2) is probably faster. You can make it even faster if you know from which end to start.
|
|
|
|
|
Thanks for that answer.
Unfortunately there will be no way of knowing which end to start from.
Neil
cheers,
Neil
|
|
|
|
|
Best bet is to guess by seeing if it's closer in value to front() or back() . That way, the average search distance for random data will be N/2 where N is the list length.
|
|
|
|
|
Stuart Dootson wrote: Best bet is to guess by seeing if it's closer in value to front() or back(). That way, the average search distance for random data will be N/2 where N is the list length.
but only when the values are distributed equally.
|
|
|
|
|
I am instantiating a CoClass present in an MFC Local server. Things would work fine when the server is already running. When the server is not running and I instantiate the CoClass, it starts and does some initialization - starts some other servers etc. So in this scenario, I need to wait for some duration in my code before I invoke any methods. i am wondering how I can know the "wait time for intialization". any ideas?
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
As I see it there are three ways do accomplish this.
1. (Not very good and should be avoided until every other option has been rejected)
Poll some interface method that can inform the caller whether the server is up and running or not. Calls could be invoked by a timer.
2. (Presumably not suitable and requires a re-write of the server)
Have the server blocking while creating other servers so that it doesn't return until it's up and running.
3. (The correct way, if supported by the server)
Implement a callback, or connection point, that gets called when the server is up and running.
Technically there's a fourth way and that would be to cancel execution with ::Sleep(), but that is so very wrong that it doesn't even make the list above.
Don't even consider it!
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998 "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above
|
|
|
|
|
Roger,
Thanks for the quick response.
I had thought about the Answer#1 and 3 and I thought 3 would be the best option. Let me check if I can implement #3 in the server - it's not owned by me
I had put the ::Sleep() in my client code and it works fine but as you said its not a good way and thats the reason why I am seeking for some other solution.
If you see any other solutions, do let me know.
Thanks again.
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
I figured out a simple solution for this. Place the call to
COleTemplateServer::RegisterAll();
after your server intializes and before it shows its window/dialog, if any.
If I am wrong, please correct me.
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Ummm, not sure actually...
From MSDN Remarks COleObjectFactory::RegisterAll: Registers all of the application’s object factories with the OLE system DLLs. This function is usually called by CWinApp::InitInstance when the application is launched.
I guess the above would make sense if you've written an EXE-server and the call to RegisterAll was omitted. Then it should definitely be added if I've understood MSDN correctly.
If you haven't made an EXE-server, then I don't know in what way this could be applicable nor how/why it would solve your initial problem.
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998 "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above
|
|
|
|
|
I have an EXE server written in MFC!
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: I have an EXE server written in MFC!
Ok, then it makes sense to add the call to RegisterAll() .
So it's all good then, right?
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"No one remembers a coward!" - Jan Elfström 1998 "...but everyone remembers an idiot!" - my lawyer 2005 when heard of Jan's saying above
|
|
|
|
|
The call to RegisterAll() was present but before the server started other servers, inshort before it completely initialized its state. So I moved the RegisterAll() call after the initialization just before its main window is shown. The call to RegisterAll() informs the OLE System Dll's that the coclasses inside this server are ready to handle calls.
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
what is the use/function of manifest file in WTL
|
|
|
|
|
The manifest file is embedded into the executable to support the XP Themes.
|
|
|
|
|
hi i had found a project but this line give me confusion,if any body can help me i will be thankful to him.
std::vector<handle> vThreads;
HANDLE hThread = CreateThread(NULL,0,CMyService::Serve,(void*) pServe,CREATE_SUSPENDED,&dwServe);
vThreads.push_back(hThread);//i m talking about this line,
will that thread will be executed in backgroundor not,what will happen to that thread.
Tasleem Arif
|
|
|
|
|
The CREATE_SUSPENDED flag means the thread will not run until you call ResumeThread() on its handle.
|
|
|
|
|
tasleem143 wrote: vThreads.push_back(hThread);//i m talking about this line,
will that thread will be executed in backgroundor not,what will happen to that thread.
This line stores the handle returned by the CreateThread API.Which is helpful for controlling the thread.
And thread is created in suspended mode.
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
Thanks both of u for kind help.
Regards.
Tasleem Arif
|
|
|
|
|
i create an ATL project and insert an full control named IMyClient .the class is
CMyClient.Then i add other C++ class CDealEvent .Now i want to use the function 'onidle'
that in CMyClient ,how should i do? the code like this:
///////////////////////////////////
//MyClient.h
///////////////////////////////////
#include DealEvent.h;
class ATL_NO_VTABLE CMyClient:
public ......
{
...........
///////////////////////////////
//here i add a function onIdle
///////////////////////////////
public:
HRESULT onIdle(void);
CDealEvent* pEvent;
HRESULT FinalConstruct()
{
pEvent=new CDealEvent();
}
}
///////////////////////////////////
//MyClient.cpp
///////////////////////////////////
HRESULT CMyClient::onIdle(void)
{
//some code here
}
CMyClient::CMyClient():pEvent(NULL)
{
}
//////////////////////////////////
//add other c++ class DealEvent.h
/////////////////////////////////
class CDealEvent
{
public:
callonIdle();
}
//////////////////////////////////
//add other c++ class DealEvent.cpp
/////////////////////////////////
void CDealEvent::callonIdle()
{
//here i want to call ' onIdle ' that in CMyClient
//how should i do?
}
|
|
|
|