C++ STL 基本使用Win32 版

看一下STL vector 向量容器的基本使用;CFree 5.0,Win7;

创建工程时选择C++语言;STL是C++的;如果是写纯API程序也可以选择C语言;

C++ STL 基本使用Win32 版_第1张图片

看一下CFree的包含文件;包含对STL的支持;

C++ STL 基本使用Win32 版_第2张图片

#include 
#include "resource.h"
#include 
#include 

using namespace std;

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
 
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("vecDemo");
vector vec1;
vector::iterator itr1;
vector::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::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::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;
}

运行起来如下;

C++ STL 基本使用Win32 版_第3张图片

C++ STL 基本使用Win32 版_第4张图片

定义一个vector,然后往里面push数据,输出;排序;然后再输出;

vector需要包含 ;排序需要包含 

工程;

C++ STL 基本使用Win32 版_第5张图片

C++ STL 基本使用Win32 版_第6张图片

资源文件,头文件;

#include "resource.h"
#include 

/////////////////////////////////////////////////////////////////////////////
//
// 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

 

你可能感兴趣的:(VC++)