C++ STL 基本使用Win32 版
生活随笔
收集整理的這篇文章主要介紹了
C++ STL 基本使用Win32 版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看一下STL?vector 向量容器的基本使用;CFree 5.0,Win7;
創建工程時選擇C++語言;STL是C++的;如果是寫純API程序也可以選擇C語言;
看一下CFree的包含文件;包含對STL的支持;
#include <windows.h> #include "resource.h" #include <vector> #include <algorithm>using namespace std;LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);HINSTANCE hInst; TCHAR szClassName[] = TEXT("vecDemo"); vector<int> vec1; vector<int>::iterator itr1; vector<int>::iterator itr2;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_VECDEMO);wincl.cbClsExtra = 0;wincl.cbWndExtra = 0;wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);if (!RegisterClassEx (&wincl))return 0;hwnd = CreateWindowEx (0,szClassName,TEXT("vecDemo"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,250,250,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;int y=0;char szBuffer[100]; switch (message){case WM_COMMAND:switch (LOWORD(wParam)){case IDM_vec: hdc=GetDC(hwnd); vec1.push_back(7);vec1.push_back(2);vec1.push_back(9); // vec1: {7, 2, 9}itr1 = vec1.begin();itr2 = vec1.end();for (vector<int>::iterator itr = itr1; itr!=itr2; ++itr){wsprintf(szBuffer, "%d",*itr);TextOut(hdc,60,25+y*20,szBuffer,lstrlen(szBuffer)); y=y+1; //y坐標增加一行 }sort(itr1, itr2); // vec1: {2, 7, 9}y=0;for (vector<int>::iterator itr = itr1; itr!=itr2; ++itr){wsprintf(szBuffer, "%d",*itr);TextOut(hdc,120,25+y*20,szBuffer,lstrlen(szBuffer)); y=y+1; //y坐標增加一行 }break;case IDM_ABOUT:MessageBox (hwnd, TEXT ("vecDemo v1.0\nCopyright (C) 2020\n by bo"),TEXT ("vecDemo"), 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; }運行起來如下;
定義一個vector,然后往里面push數據,輸出;排序;然后再輸出;
vector需要包含 <vector>;排序需要包含?<algorithm>;
工程;
資源文件,頭文件;
#include "resource.h" #include <windows.h>/ // // Menu //IDC_VECDEMO MENU BEGINPOPUP "&File"BEGINMENUITEM "STL vec Demo", IDM_vecMENUITEM "E&xit", IDM_EXITENDPOPUP "&Help"BEGINMENUITEM "&About ...", IDM_ABOUTEND END #define IDM_EXIT 10001 #define IDM_ABOUT 10002#define IDC_VECDEMO 10101 #define IDD_ABOUTBOX 10102 #define IDM_vec 40001?
總結
以上是生活随笔為你收集整理的C++ STL 基本使用Win32 版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WPF布局控件Grid的基本使用 - 使
- 下一篇: 图的邻接矩阵简单实现Win32版本