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 
BEGIN
    POPUP "&File"
    BEGIN
    	MENUITEM "STL vec Demo",            IDM_vec
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_VECDEMO		10101
#define 	IDD_ABOUTBOX	10102
#define     IDM_vec     40001

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章