LibTorch与MFC
#include <afxwin.h>
#include "resource.h"
#include<opencv2/opencv.hpp>
#include <torch/torch.h>
#include <torch/script.h>
#include <afxwin.h>
using namespace std;
class MyFrameWnd :public CFrameWnd {
// 类内声明宏
DECLARE_MESSAGE_MAP()
public:
LRESULT OnCreate(WPARAM wParam, LPARAM IParam);
};
// 类外实现宏
BEGIN_MESSAGE_MAP(MyFrameWnd, CFrameWnd)
ON_MESSAGE(WM_CREATE, OnCreate)
END_MESSAGE_MAP()
class MyWinApp :public CWinApp {
public:
MyWinApp() {
}
virtual BOOL InitInstance() {
MyFrameWnd* pFrame = new MyFrameWnd;
pFrame->Create(NULL, "Test");
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
return TRUE;
}
};
MyWinApp mainApp;
// 测试
LRESULT MyFrameWnd::OnCreate(WPARAM wParam, LPARAM IParam) {
torch::Tensor b = torch::tensor({ 999 });
int a = b[0].item().toInt();
string str = to_string(a);
AfxMessageBox(str.c_str());
return 0;
}
补充
int转string
std::string s = std::to_string(42);
tensor转换成int c++/libtorch
torch::Tensor b = torch::tensor({ 999 });
int a = b[0].item().toInt();
string转CString
string str = "test"
c_str = str.c_str(); //注意这里使用的是 =运算符的重载
CString转string
多字节编码下:
string str = c_str.GetBuffer();
Unicode编码下:
CString c_str = _T("li");
//CT2A将Unicode转换为多字符集ASCII
std::string str = CT2A(c_str.GetString());