zoukankan      html  css  js  c++  java
  • C++学习之STL容器vector

    学习知识点

    vector容器的添加、删除,遍历操作。

    练习代码

    // IPCT.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <vector>
    #include <shlwapi.h>
    #pragma comment(lib,"shlwapi.lib")
    using namespace std;

    BOOL find(char *ip);
    class MoveHotel
    {
    public:
    char ip[16];
    char mac[18];
    char mark[100];
    MoveHotel(char _ip[],char _mac[],char _mark[])
    {
    //ip=_ip;
    //mac=_mac;
    //mark=_mark;
    strcpy(ip,_ip);
    strcpy(mac,_mac);
    strcpy(mark,_mark);
    }
    };
    vector<MoveHotel *> hostlist;
    int _tmain(int argc, _TCHAR* argv[])
    {

    FILE *fp;
    fp=fopen("d:\\mac.txt","r");
    if (!fp)
    {
    return 0;
    }
    while(!feof(fp))
    {
    char ip[16],mac[18],info[100];
    fscanf(fp,"IP:%s\tMAC:%s\t备注:%s\n",ip,mac,info);
    printf("正在插入——%s\n",ip);
    hostlist.push_back(new MoveHotel(ip,mac,info));
    }
    printf("完成!\n");
    fclose(fp);

    while (1)
    {
    char read[20];
    scanf("%s",&read);
    if(StrCmp(read,"exit")==0)
    {
    break;
    }
    else
    {
    if (find(read))
    {
    printf("查找完成!\n");
    }
    else
    {
    printf("没有%s相关的内容!\n",read);
    }
    }
    }

    vector<MoveHotel *>::iterator i;
    for (i=hostlist.begin();i!=hostlist.end();i++)
    {
    printf("%s%s%s\n",(*i)->ip,(*i)->mac,(*i)->mark);
    }
    //vector<Sprite *>::iterator i; //指示器i:数组指针,保存元素地址的地址代号
    for (i=hostlist.begin();i!=hostlist.end();i++) //注意为:i!=m_vSprites.end()因为是保存着地址序列;而非整数序列
    {
    delete *i;
    }
    hostlist.clear();
    //vector<MoveHotel *>(hostlist).swap(MoveHotel); //创建新容器并无空复制原容器数据,再交换两个容器内的数据;达到压缩的目的;相当于ArrayList中的TrimToSize()
    return 0;
    }

    BOOL find(char *ip)
    {
    if (!hostlist.empty())
    {
    vector<MoveHotel *>::iterator i;
    for (i=hostlist.begin();i!=hostlist.end();i++)
    {
    if (StrCmp((*i)->ip,ip)==0)
    {
    printf("IP:%s\nMAC:%s\nMARK:%s\n",(*i)->ip,(*i)->mac,(*i)->mark);
    return TRUE;
    }

    }
    }
    return FALSE;
    }
  • 相关阅读:
    MMORPG大型游戏设计与开发(客户端架构 part14 of vegine)
    java线程与并发(一)
    HTTP学习笔记(五)
    http学习笔记(四)——HTTP报文
    http学习笔记(三)
    http学习笔记(二)—— 嘿!伙计,你在哪?(URL)
    http学习笔记(一)
    本地DNS安装
    SQL Server求解连续操作(登录)数量(次数)最大的记录(用户)问题
    一个有趣的SQL Server 层级汇总数据问题
  • 原文地址:https://www.cnblogs.com/shya/p/2334738.html
Copyright © 2011-2022 走看看