0

我只想将我的 wchar 数组转换为字符串并将其推送到我的字符串向量中。我的解决方案被注释掉了,因为它不起作用。我收到一个错误,提示我的向量超载。代码如下:

  vector<wstring> vec;
  string tmp;
  int n;
  int m_device_id = 0;
  do {
    m_device_id++;
    tmp="";
    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);

wostringstream tmp;
    for (int i = 0; i < 6; i++)
    {
     tmp << m_device_info.Address.rgBytes [i];

    // Append the colon, but not after the last
    if (i < 5)
    tmp << L':';
   }
    vec.push_back(tmp.str());

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


  vector<wstring>::iterator it = find(vec.begin(), vec.end(), wstring(L"18:22:85:d8:03:98"));
  if(it != vec.end()) {
  cout << "Found it" << '\n';
  } else {
  cout << "Not found" << '\n';
 }
  BluetoothFindDeviceClose(m_bt_
 }while(BluetoothFindNextRadio(&m_bt_find_radio, &m_radio));

================================================

设备信息结构

typedef struct _BLUETOOTH_DEVICE_INFO {
 DWORD             dwSize;
 BLUETOOTH_ADDRESS Address;
 ULONG             ulClassofDevice;
 BOOL              fConnected;
 BOOL              fRemembered;
 BOOL              fAuthenticated;
 SYSTEMTIME        stLastSeen;
 SYSTEMTIME        stLastUsed;
 WCHAR             szName[BLUETOOTH_MAX_NAME_SIZE];
} BLUETOOTH_DEVICE_INFO;
4

1 回答 1

2

首先,如果您询问有关编译错误的问题,请始终将错误放在问题中。

其次,你得到的错误是因为你有一个vector包含wstring,但尝试推送一个类型的变量string

第三,根据您的代码判断,您要从中创建字符串的数组不是字符数组,而是数字数组。您可以使用例如创建一个字符串std::wostringstream

std::wostringstream tmp;
for (int i = 0; i < 6; i++)
{
    tmp << std::hex << std::setw(2) << std::setfill(L'0')
        << m_device_info.Address.rgBytes[i];

    // Append the colon, but not after the last
    if (i < 5)
        tmp << L':';
}

vec.push_back(tmp.str());
于 2012-07-27T12:43:24.983 回答