并查集c++代码(windows上利用C++语言查询设备 (windows,vs2010))

代码功能将windows设备遍历一次,然后将结果直接存放到map容器中,需要清楚其中一个设备的个数,直接通过map可以查询

#include <Windows.h>

#include <setupapi.h>

#include <string>

#include <vector>

#include<map>

#include<stdio.h>

#pragma comment(lib, "setupapi.lib")

using namespace std;

class QueryCard{

public:

map<string,int>Card;

map<string,int>::iterator it;

HDEVINFO hDevInfo;

SP_DEVINFO_DATA DeviceInfoData;

DWORD i;

void QueryCardCount();

};

void QueryCard::QueryCardCount(){

hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES);

if (hDevInfo == INVALID_HANDLE_VALUE)

return ;

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++)

{

char szDescBuf[MAX_PATH] = { 0 };

if (!SetupDiGetDeviceRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC, NULL, (PBYTE)szDescBuf, MAX_PATH - 1, NULL))

continue;

string str=szDescBuf;

it = Card.find(str);

if(it == Card.end())

Card.insert(make_pair(str, 1));

else

并查集

{

Card[str]++;

}

}

SetupDiDestroyDeviceInfoList(hDevInfo);

}

int main(){

QueryCard QueryCardTest;

QueryCardTest.QueryCardCount();

int Drive=QueryCardTest.Card["磁盘驱动器"];

printf("DRIVE:%dn",Drive);

return 1;

}


代码运行图

您可以还会对下面的文章感兴趣

暂无相关文章

使用微信扫描二维码后

点击右上角发送给好友