|
Thank you DavidCrow!
Your method is great! I am wondering whether I can do some further improvements. I think we can check the size of each set, then we can use the elements in smaller set to find the matched ones in the bigger set (or vice versa?), which can improve the performnace -- but I am not sure about it. Do you have any comments?
regards,
George
|
|
|
|
|
George_George wrote: I think we can check the size of each set, then we can use the elements in smaller set to find the matched ones in the bigger set (or vice versa?), which can improve the performnace -- but I am not sure about it. Do you have any comments?
Just have your for /while loop check for remaining items in both sets. Something like:
while (there_are_items_in_set1 && there_are_items_in_set2)
...
"The largest fire starts but with the smallest spark." - David Crow
|
|
|
|
|
Use STL. The following calculates a set intersection:
Here's a complete example I had laying around:
----------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
const char* g_Names1[] = {"Joe", "Paul", "Simon", "Steve", "Karl"};
const char** g_Names1Begin = &g_Names1[0];
const char** g_Names1End = &g_Names1[sizeof(g_Names1)/sizeof(g_Names1[0])];
const char* g_Names2[] = {"Paul", "Kate", "Oleg"};
const char** g_Names2Begin = &g_Names2[0];
const char** g_Names2End = &g_Names2[sizeof(g_Names2)/sizeof(g_Names2[0])];
struct CharLess
{
bool operator()(const char* p1, const char* p2) const
{
return strcmp(p1, p2)<0;
}
};
int main(int argc, char* argv[])
{
typedef vector<const char*> VEC;
VEC v1(g_Names1Begin, g_Names1End);
VEC v2(g_Names2Begin, g_Names2End);
sort(v1.begin(), v1.end(), CharLess());
sort(v2.begin(), v2.end(), CharLess());
set_intersection(
v1.begin(), v1.end(),
v2.begin(), v2.end(),
ostream_iterator<const char*>(cout, "\n"),
CharLess()
);
return 0;
}
Steve
|
|
|
|
|
Thank you Steve,
I noticed that you are using STL built-in algorithm set_intersection. Do you have any ideas of how to implement it efficient without STL? I need to save footprint on my device. Thank you!
regards,
George
|
|
|
|
|
Your fears that using STL will bloat your program are not well founded. You can check out the source code for set_intersection by looking in the file <algorithm> : it contains a for loop and two if . With STL you only pay for what you use. Note that the algorithm requires the data to be sorted thus you must count the code for the sort algorithm also.
Steve
|
|
|
|
|
Hi Steve,
I am developing on embedded device. So if I need to use C++, I have to port C++ runtime library to this platform, right (actually, I prefer C)? And it will both,
- Increase my working time;
- Increase the footprint to hold C++ runtime library (for example .so files on Linux).
Have I answered your question?
regards,
George
|
|
|
|
|
None of the algorithms I used (sort or set_intersection ) use or have any need for the C runtime library. There is no distinct C++ runtime library. The only CRT function used is the one I introduced: strcmp (ignoring the functions I used to write the output to the console). None of what you say need be true. In general using STL will decrease your working time and will have not have a detrimental effect on the footprint of your EXE. Most of your fears are superstition.
Steve
|
|
|
|
|
Sorry Steve,
Maybe I have not make myself understood enough. I mean I prefer C. But in order to use sort or set_intersection, I have to use C++ runtime library. Correct?
regards,
George
|
|
|
|
|
George_George wrote: But in order to use sort or set_intersection, I have to use C++ runtime library. Correct?
This is not the case. Both sort and set_intersection are both just short inline functions with no dependencies on any runtime libraries. Have a look at the algorithm header file. In fact most or STL is like this. These are exceptions such as the stream and locale classes.
Steve
|
|
|
|
|
He's a version which sorts the array in-place without using a vector:
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
const char* g_Names1[] = {"Joe", "Paul", "Simon", "Steve", "Karl"};
const char** g_Names1Begin = &g_Names1[0];
const char** g_Names1End = &g_Names1[sizeof(g_Names1)/sizeof(g_Names1[0])];
const char* g_Names2[] = {"Paul", "Kate", "Oleg"};
const char** g_Names2Begin = &g_Names2[0];
const char** g_Names2End = &g_Names2[sizeof(g_Names2)/sizeof(g_Names2[0])];
struct CharLess
{
bool operator()(const char* p1, const char* p2) const
{
return strcmp(p1, p2)<0;
}
};
int main(int argc, char* argv[])
{
sort(g_Names1Begin, g_Names1End, CharLess());
sort(g_Names2Begin, g_Names2End, CharLess());
set_intersection(
g_Names1Begin, g_Names1End,
g_Names2Begin, g_Names2End,
ostream_iterator<const char*>(cout, "\n"),
CharLess()
);
return 0;
}
Steve
|
|
|
|
|
Hi Steve,
To me, it is the same -- since I am trying to remove STL/C++ and implement it by myself. I am looking for an efficient implementation -- there are a couple of sets and each contains a lot of data. Any comments or ideas?
regards,
George
|
|
|
|
|
If you really want to re-invent the wheel copy STL's code - all the source is there. After looking at the code in question you'll realise you should have just used it as is. You'll have a hard time matching STL's efficiency.
Steve
|
|
|
|
|
Thank you Steve,
STL's set_intersection is very efficient? Why? Does it have any special designs?
regards,
George
|
|
|
|
|
Which is the last Window Message when a Window is being destroyed?
WM_CLOSE<br />
WM_DESTROY<br />
WM_QUIT
Are there other messages when a Window is destroyed?
---
With best regards,
A Manchester United Fan
The Genius of a true fool is that he can mess up a foolproof plan!
-- modified at 3:14 Thursday 1st June, 2006
|
|
|
|
|
|
Thanks....
---
With best regards,
A Manchester United Fan
The Genius of a true fool is that he can mess up a foolproof plan!
|
|
|
|
|
I wonder if the two are the same in coding and result?
FILWY
|
|
|
|
|
|
the VC6 compiler was released before the last standard was official, so it is really not compliant to the C++ language standard.
if you have the choice between VC6 and VC2003, prefer VC2003
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
for mfc, they r almost same. but for atl, they r quite different,and not compatible. I perfer vc7.1, the compiling speed, the compiled file sizes r smaller. and especially, the codes look more perfect than doing it with vc6.
life is like a box of chocolate,you never know what you r going to get.
|
|
|
|
|
A thread created by using AfxBeginThread(RUNTIME_CLASS(CClientHandler));
can be end/destroyed by using AfxEndThread(..) , would it be called from inside of same thread.
If the AfxEndThread will end the thread then would the object created inside of that thread will also be destroyed?
Thanks
Regards.
|
|
|
|
|
AfxEndThread must be called from within the thread you wish to stop.
Steve
|
|
|
|
|
would this stop release all the memory that the thread and its members objects have occupied
Regards.
|
|
|
|
|
IMHO, the most cleaner way to exit a thread is simply to return from the thread. For example, if you have a loop within the thread, you can simply use a flag to signalate when the thread must exit. This flag can be set by another thread (by using thread-safe methods, like CCriticalSection).
Cédric Moonen
Software developer
Charting control
|
|
|
|
|
A thread can be terminated in four ways:
1)The thread function returns. (This is highly recommended.)
2)The thread kills itself by calling the ExitThread function. (Avoid this method.)
3)A thread in the same or in another process calls the TerminateThread function. (Avoid this method.)
4)The process containing the thread terminates. (Avoid this method.)
Option 1 ensures that all thread resources are cleaned up properly. Any and all C++ objects created in your thread function will be destroyed properly via their destructors.
The operating system will properly free the memory used by the thread's stack.
The system will set the thread's exit code (maintained in the thread's kernel object) to your thread function's return value.
The system will decrement the usage count of the thread's kernel object.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|