C++ 模板 模板是泛型編程的基礎,泛型編程即以一種獨立于任何特定類型的方式編寫代碼。 模板是創建泛型類或函數的藍圖或公式。庫容器,比如迭代器和算法,都是泛型編程的例子,它們都使用了模板的概念。
每個容器都有一個單一的定義,比如 向量,我們可以定義許多不同類型的向量,比如 vector <int> 或 vector <string>。
可以使用模板來定義函數和類。
函數模板 模板函數定義的一般形式如下所示:
template <typename type> ret-type func-name(parameter list) { ? ?// 函數的主體 } type 是函數所使用的數據類型的占位符名稱。這個名稱可以在函數定義中使用。
#include <windows.h>
#include "resource.h"
#include <string>using namespace std;LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst;
TCHAR szClassName[] = TEXT("tempDemo");template <typename T>
inline T const& Max (T const& a, T const& b)
{ return a < b ? b:a;
} int WINAPI
WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
{HWND hwnd;MSG messages;WNDCLASSEX wincl;hInst = hThisInstance;wincl.hInstance = hThisInstance;wincl.lpszClassName = szClassName;wincl.lpfnWndProc = WindowProcedure;wincl.style = CS_DBLCLKS;wincl.cbSize = sizeof (WNDCLASSEX);wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);wincl.hCursor = LoadCursor (NULL, IDC_ARROW);wincl.lpszMenuName = MAKEINTRESOURCE (IDC_TEMPDEMO1);wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl))return 0;hwnd = CreateWindowEx (0,szClassName,TEXT("C++函數模板Demo"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,300,200,HWND_DESKTOP,NULL,hThisInstance,NULL);ShowWindow (hwnd, nFunsterStil);while (GetMessage (&messages, NULL, 0, 0)){TranslateMessage(&messages);DispatchMessage(&messages);}return messages.wParam;
}LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{PAINTSTRUCT ps;HDC hdc;RECT rt; char dstr[20];char szBuffer[100]; int i = 99;int j = 60;double f1=17.98;double f2=77.09;string s1 = "Hello"; string s2 = "World";switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_temp: hdc=GetDC(hwnd);wsprintf(szBuffer, "Max(i, j): %d",Max(i, j));TextOut(hdc,80,30,szBuffer,lstrlen(szBuffer));//cout << "Max(f1, f2): " << Max(f1, f2) << endl;sprintf(dstr,"%.2lf",Max(f1, f2));wsprintf(szBuffer, "Max(f1, f2): %s",dstr);TextOut(hdc,80,60,szBuffer,lstrlen(szBuffer));wsprintf(szBuffer, "Max(s1, s2): %s",(Max(s1, s2)).c_str());TextOut(hdc,80,90,szBuffer,lstrlen(szBuffer));break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("tempDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("tempDemo"), MB_OK | MB_ICONINFORMATION);break;case IDM_EXIT:DestroyWindow(hwnd);break;default:return DefWindowProc(hwnd, message, wParam, lParam); }break;case WM_CREATE:break;case WM_PAINT:hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rt); EndPaint(hwnd, &ps);break;case WM_DESTROY:PostQuitMessage (0);break;default:return DefWindowProc (hwnd, message, wParam, lParam);}return 0;
}
運行;
?
這里還有一個問題;
第三次是比較兩個字符串的大小;代碼為:
wsprintf(szBuffer, "Max(s1, s2): %s",(Max(s1, s2)).c_str());
對于Max返回的string再調用c_str()函數;不如此,將出現錯誤:
cannot pass objects of non-POD type `const struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' through `...'; call will abort at runtime
因為;
1 printf函數輸出字符串是針對char *的,即printf只能輸出c語言的內置數據類型,而string不是c語言的內置數據類型。 2 string類型的對象不止包含字符串,還包含了許多用于操作的函數,所以&str并非字符串的首地址。 3 如需輸出string對象中的字符串,可以使用string的成員函數c_str(),該函數返回字符串的首字符的地址。
可參閱;
https://blog.csdn.net/li_l_il/article/details/84196587
工程;
資源文件,頭文件;
#include "resource.h"
#include <windows.h>/
//
// Menu
//IDC_TEMPDEMO1 MENU
BEGINPOPUP "&File"BEGINMENUITEM "C++ 函數模板Demo", IDM_tempMENUITEM "E&xit", IDM_EXITENDPOPUP "&Help"BEGINMENUITEM "&About ...", IDM_ABOUTEND
END
#define IDM_EXIT 10001
#define IDM_ABOUT 10002#define IDC_TEMPDEMO1 10101
#define IDD_ABOUTBOX 10102
#define IDM_temp 40001
?
《新程序員》:云原生和全面數字化實踐 50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔 為你收集整理的C++函数模板Demo - win32 版 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。