I have problems sorting two std::vector<t> lists into one. Currently I can add monitors, but if one if disconnected it will still be stored in the list (It needs to be deleted as it could be storing large unused data in the memory).
I send a struct through EnumDisplayMonitors
1 2 3 4 5 6
|
struct structMonitorList
{
int iCount = 0;
int iInactiveMonitors = 0;
std::vector<MONITORINFOEX> infoArray;
};
|
This will push_back MONITORINFOEX structure for every active monitor. This is the 'new' list and I presume will not create a memory problem as it is automatically allocated / destroyed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
structMonitorList tempList;
// Call EnumDisplayMonitors CALLBACK function
EnumDisplayMonitors(NULL, NULL, cbInfoProcedure, reinterpret_cast<LPARAM>(&tempList));
// If the previous list was empty (Usually when the program first runs)
if (iActiveMonitors == 0)
// If empty then copy entire list
{
for (int i = 0; i < iActiveMonitors; i++)
{
smiMonitorList.push_back(SingleMonitorInfo(tempList.infoArray[i]));
}
}
// If not empty then compare lists
// For all monitors in new list
for (int i = 0; i < tempList.iCount; i++)
{
int j = 0;
bool found = false;
// While all monitors in old list
while (j < iActiveMonitors && !found)
{
// If monitors are identical
if (compareMonitor(smiMonitorList[i], tempList.infoArray[j]))
{
// Replace RECT structures - As relative position might have changed, but Device, Monitor and Resolution are identical
smiMonitorList[i].rcMonitorArea = tempList.infoArray[j].rcMonitor;
smiMonitorList[i].rcWorkArea = tempList.infoArray[j].rcWork;
// Found old monitor in new list
found = TRUE;
}
else
{
// Increase old list position
j++;
}
}
// If new monitor not found append to old list
if (!found)
{
smiMonitorList.push_back(SingleMonitorInfo(tempList.infoArray[i]));
}
}
|
This is my sorting for the two lists. It doesn't remove missing monitors and to add another nested for / while loop to check seems wasteful. This check needs to be able to run a few times a second without affecting normal PC use. Any ideas?
If it is any help, here is my comments for compareMonitor()
1 2 3 4 5 6 7 8 9 10 11
|
/*
METHOD: compareMonitor
- Compares monitors (Device name, resolution and monitor name)
PARAMENTERS:
RETURNS: int
(>0) method failed
( 0 ) method succeeded, device name, resolution and monitor name are same
( 1 ) method succeeded, device name are different
( 2 ) method succeeded, device name are same, resolution are different
( 3 ) method succeeded, device name and resolution are same, monitor name are different
*/
|
EDIT: I just had to search for the correctly worded question on google. I need to use vector.insert.
EDIT #1: This is harder than I thought. Not sure where to begin, or if I can sort the vectors easily or at all through my compareMonitor method.
EDIT #2: I think the first if needs to be in a separate method run in the ctor. This would stop it having to check every time it updates the list. I have now done this.