2012-07-27 71 views
0

@pmr:看看下面的代碼從比較的矢量的WCHAR字符串與其他指定的字符串WCHAR

@singeroftheall:我有兩件事的問題。插入和搜索。我認爲搜索部分已經完成。現在我怎樣才能將我的Mac地址插入向量中? 我的地址保存在varable 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++嗎?導致矢量輸出在C++?

============================================== ==================================== 我寫了一個代碼,顯示所有可用的藍牙設備。我只想將可用設備的所有MAC地址(類型:wchar)保存在向量列表中。之後,我想要搜索指定的wchar,如果它在矢量列表中。

我不是c/C++的專業程序員,只是需要這個來比較一下演示,看看我的想法是否有效。如果有人能幫助我,我會非常高興和開心。我也搜索谷歌和寫在其他常見問題,但沒有找到一個很好的解決方案,可以幫助我。

CODE搜索設備:

#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; 
} 
+0

你能削減代碼下你實際的問題:要在vector尋找一些wstring?藍牙的東西似乎沒有關係。 – pmr 2012-07-27 09:30:24

+0

你在向量中插入字符串時有問題嗎?或者只是在向量中搜索一些字符串? – SingerOfTheFall 2012-07-27 09:32:20

+0

我認爲你在正確的軌道上。如果你使用'wchar_t',你也應該使用'wstring'和'vector '。 – 2012-07-27 09:32:41

回答

0

首先:在string中的人物類型wchar,但char的不是。你可能需要一個vector<wstring>

#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'; 
} 
+0

感謝您的支持和不錯的回覆。 – Kipcak08 2012-07-27 10:55:38

+0

** @ pmr **:看下面的代碼 ** @ singeroftheall **:我有兩個問題。插入和搜索。我認爲搜索部分已經完成。現在我怎樣才能將我的Mac地址插入向量中? 我的地址保存在varable m_device_info.Adress.rgBytes [];見下文。 m_device_info.Address.rgBytes [0],m_device_info.Address.rgBytes()\t \t wprintf(L「\ t \ tAddress:%02x:%02x:%02x:%02x:%02x:%02x \ r \ n」 [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]); – Kipcak08 2012-07-27 11:03:01

+0

@ user1557170我添加了必要的包含和插入示例。請不要將評論編輯爲答案。使用評論。另外,請查看SO常見問題以瞭解該網站。 – pmr 2012-07-27 12:00:00