gpt4 book ai didi

c++ - 将 vector 中的 wchar 字符串与其他指定的 wchar 字符串进行比较

转载 作者:行者123 更新时间:2023-11-30 17:59:08 28 4
gpt4 key购买 nike

@pmr:看下面的代码

@singeroftheall:我对这两件事都有问题。插入和搜索。我认为搜索部分已经完成。现在如何在 vector 中插入我的 mac 地址?我的地址保存在变量 m_device_info.Adress.rgBytes[] 中;见下文。

    wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);

可以混合使用 C 和 C++ 语言吗?因为 vector 输出是用 C++ 编写的?

================================================== =====================================我编写了一段代码,显示所有可用的蓝牙设备。我只想将可用设备的所有 MAC 地址(类型:wchar)保存在 vector 列表中。之后,我想搜索指定的 wchar(如果它在 vector 列表中)。

我不是 c/c++ 方面的专业程序员,只是需要这个来比较演示并看看我的想法是否有效。如果有人能帮助我,我会非常高兴和高兴。我也搜索过谷歌并写过其他常见问题解答,但没有找到对我有帮助的好的解决方案。

搜索设备的代码:

#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <bthdef.h>
#include <BluetoothAPIs.h>
#include <iostream>
#include <vector>

using namespace std;
#pragma comment(lib, "Irprops.lib")

BLUETOOTH_FIND_RADIO_PARAMS m_bt_find_radio = {
sizeof(BLUETOOTH_FIND_RADIO_PARAMS)
};

BLUETOOTH_RADIO_INFO m_bt_info = {
sizeof(BLUETOOTH_RADIO_INFO),
0
};

BLUETOOTH_DEVICE_SEARCH_PARAMS m_search_params = {
sizeof(BLUETOOTH_DEVICE_SEARCH_PARAMS),
1,
0,
1,
1,
1,
15,
NULL
};

BLUETOOTH_DEVICE_INFO m_device_info = {
sizeof(BLUETOOTH_DEVICE_INFO),
0
};

HANDLE m_radio = NULL;
HBLUETOOTH_RADIO_FIND m_bt = NULL;
HBLUETOOTH_DEVICE_FIND m_bt_dev = NULL;

int wmain(int argc, wchar_t **args) {
while(true) {
m_bt = BluetoothFindFirstRadio(&m_bt_find_radio, &m_radio);
int m_radio_id = 0;
do {
m_radio_id++;

BluetoothGetRadioInfo(m_radio, &m_bt_info);

wprintf(L"Radio %d:\r\n", m_radio_id);
wprintf(L"\tName: %s\r\n", m_bt_info.szName);
wprintf(L"\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_bt_info.address.rgBytes[1], m_bt_info.address.rgBytes[0], m_bt_info.address.rgBytes[2], m_bt_info.address.rgBytes[3], m_bt_info.address.rgBytes[4], m_bt_info.address.rgBytes[5]);
wprintf(L"\tClass: 0x%08x\r\n", m_bt_info.ulClassofDevice);
wprintf(L"\tManufacturer: 0x%04x\r\n", m_bt_info.manufacturer);

m_search_params.hRadio = m_radio;

::ZeroMemory(&m_device_info, sizeof(BLUETOOTH_DEVICE_INFO));
m_device_info.dwSize = sizeof(BLUETOOTH_DEVICE_INFO);

m_bt_dev = BluetoothFindFirstDevice(&m_search_params, &m_device_info);

vector <string> macadresse;
int n;
int m_device_id = 0;
do {
m_device_id++;
wprintf(L"\tDevice %d:\r\n", m_device_id);
wprintf(L"\t\tName: %s\r\n", m_device_info.szName);
wprintf(L"\t\tAddress: %02x:%02x:%02x:%02x:%02x:%02x\r\n", m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5]);
wprintf(L"\t\tClass: 0x%08x\r\n", m_device_info.ulClassofDevice);

//devicemc[m_device_id]= m_device_info.Address.rgBytes[0], m_device_info.Address.rgBytes[1], m_device_info.Address.rgBytes[2], m_device_info.Address.rgBytes[3], m_device_info.Address.rgBytes[4], m_device_info.Address.rgBytes[5];
//macadresse.push_back(string (&m_device_info.Address.rgBytes[0], &m_device_info.Address.rgBytes[1], &m_device_info.Address.rgBytes[2], &m_device_info.Address.rgBytes[3], &m_device_info.Address.rgBytes[4], &m_device_info.Address.rgBytes[5]));

} while(BluetoothFindNextDevice(m_bt_dev, &m_device_info));
BluetoothFindDeviceClose(m_bt_dev);

} while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));
BluetoothFindRadioClose(m_bt);
Sleep(10000);
}
return 0;
}

最佳答案

首先: string 中的字符不属于 wchar 类型,但是char 。您可能需要 vector<wstring> 。搜索一些wstringvector :

#include <algorithm>
#include <vector>
#include <string>
using namespace std;

vector<wstring> vec;
// insert something
vec.push_back(wstring("my_string"));

// search something
vector<wstring>::iterator it = std::find(vec.begin(), vec.end(), wstring("needle"));
if(it != vec.end()) {
cout << "Found it" << '\n';
} else {
cout << "Not found" << '\n';
}

关于c++ - 将 vector 中的 wchar 字符串与其他指定的 wchar 字符串进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11684933/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com