c++迭代器常用的辅助函数
xxxxxxxxxx73 1#include 23LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);45int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR CmdLine, int iCmdShow)6{7    static TCHAR szAppName[] = TEXT(“MyWindows”);   // 注意使用TEXT包裹语句8    HWND hwnd;  // 窗口句柄9    MSG msg;    // 消息结构10    WNDCLASS wndclass;  // 窗口类, 定义窗口基本属性11    wndclass.style = CS_HREDRAW | CS_VREDRAW;   // CS->ClassStyle, H->horizontal 水平的, V->vertical 垂直的, redraw 重绘12    wndclass.lpfnWndProc = WndProc;             // 指定窗口过程(回调函数)13    wndclass.cbClsExtra = 0;14    wndclass.cbWndExtra = 0;                    // 预留的额外空间, 一般设为015    wndclass.hInstance = hInstance;16    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);   // 设定图标17    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);     // 设定光标18    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);   // 窗口背景色19    wndclass.lpszMenuName = NULL;       // 菜单名字20    wndclass.lpszClassName = szAppName; // 窗口类名2122    if (!RegisterClass(&wndclass))  // 注册窗口, 注册成功返回ATOM类型(对应了字符串在ATOM表的位置); 注册失败返回023    {24        MessageBox(NULL, TEXT(“这个程序需要在Windows NT才能执行!”), szAppName, MB_ICONERROR);25    }2627    hwnd = CreateWindow(szAppName,28        TEXT(“快乐小凳凳”),29        WS_OVERLAPPEDWINDOW,    // 窗口风格30        CW_USEDEFAULT,31        CW_USEDEFAULT,32        CW_USEDEFAULT,33        CW_USEDEFAULT,34        NULL,35        NULL,36        hInstance,37        NULL38    );3940    ShowWindow(hwnd, iCmdShow);41    UpdateWindow(hwnd);4243    while (GetMessage(&msg, NULL, 0, 0))44    {45        TranslateMessage(&msg);46        DispatchMessage(&msg);47    }4849    return msg.wParam;50}5152LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)53{54    HDC hdc;55    PAINTSTRUCT ps;56    char *str = TEXT(“大家好, 这是我的第一个窗口程序!”);57    RECT rect;5859    switch (message)60    {61    case WM_PAINT:62        hdc = BeginPaint(hwnd, &ps);63        GetClientRect(hwnd, &rect);64        DrawText(hdc, TEXT(“大家好, 这是我的第一个窗口程序!”), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);65        EndPaint(hwnd, &ps);66        return 0;67    case WM_DESTROY:68        PostQuitMessage(0); // 正式关闭窗口69        return 0;70    }7172    return DefWindowProc(hwnd, message, wParam, lParam);73}c 
STL 中有用于操作迭代器的三个函数模板,它们是:
advance(p, n):使迭代器 p 向前或向后移动 n 个元素
distance(p, q):计算两个迭代器之间的距离,即迭代器 p 经过多少次 + + 操作后和迭代器 q 相等。如果调用时 p 已经指向 q 的后面,则这个函数会陷入死循环
iter_swap(p, q):交换容器中两个迭代器指向的位置的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using namespace std;
int main()
{
int a[5] = { 1, 2, 3, 4, 5 };
list <int> lst(a, a + 5);
list <int>::iterator p = lst.begin();
advance(p, 2); //p向后移动两个元素,指向3
cout << "1)" << *p << endl; //输出 1)3
advance(p, -1); //p向前移动一个元素,指向2
cout << "2)" << *p << endl; //输出 2)2
list<int>::iterator q = lst.end(); // end指向的是最后一个元素的下一个位置
q--; // q 指向 5
cout << "3)" << distance(p, q) << endl; //输出 3)3
iter_swap(p, q); // 交换容器中两个位置的值
cout << "4)";
for (p = lst.begin(); p != lst.end(); ++p)
cout << *p << " ";
return 0;
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 快乐小凳凳!
 评论

