c++ vector 用法示例

#include "stdio.h"
#include 
<vector>
using namespace std;


/************************************************************************/
/* 结构体声明                                                           */
/************************************************************************/
typedef 
struct
{
    
int x;
    
char s[10];
}T_INFO;

typedef vector
<T_INFO> VEC_INFO;


/************************************************************************/
/* 打印Vector中的元素                                                   */
/************************************************************************/
void printVecElem(VEC_INFO &vInfo)
{
    VEC_INFO::iterator iter 
= NULL;
    
for (iter = vInfo.begin(); iter != vInfo.end(); iter++)
    {
        printf(
"x = %d, s = %s \n", (*iter).x, (*iter).s);
    }
}

/************************************************************************/
/* 打印Vector的大小                                                     */
/************************************************************************/
void printVecSize(VEC_INFO &vInfo)
{
    printf(
"size = %d \n", vInfo.size());
}

/************************************************************************/
/* 打印Vector中的第1个元素                                              */
/************************************************************************/
void printFirstElem(VEC_INFO &vInfo)
{
    printf(
"first elem = %d \n", vInfo.front());
}

/************************************************************************/
/* 打印Vector中的最后1个元素                                            */
/************************************************************************/
void printLastElem(VEC_INFO &vInfo)
{
    printf(
"last elem = %d \n", vInfo.back());
}

/************************************************************************/
/* 打印Vector相关的信息                                                 */
/************************************************************************/
void printVecInfo(VEC_INFO &vInfo)
{
    printVecElem(vInfo);
    printVecSize(vInfo);
    printFirstElem(vInfo);
    printLastElem(vInfo);
    printf(
"\n");
}

/************************************************************************/
/* 根据x返回Vector指定元素                                                   */
/************************************************************************/
VEC_INFO::iterator getElemByX(VEC_INFO 
&vInfo, int x)
{
    
for (VEC_INFO::iterator iter = vInfo.begin(); iter != vInfo.end(); iter++)
    {
        
if (iter->== x)
        {
            
return iter;
        }
    }
    
return NULL;
}

/************************************************************************/
/* 根据s返回Vector指定元素                                                   */
/************************************************************************/
VEC_INFO::iterator getElemByS(VEC_INFO 
&vInfo, char *s)
{
    
for (VEC_INFO::iterator iter = vInfo.begin(); iter != vInfo.end(); iter++)
    {
        
if (!strcmp(iter->s, s))
        {
            
return iter;
        }
    }
    
return NULL;
}

/************************************************************************/
/* 测试函数                                                             */
/************************************************************************/
void main()
{
    VEC_INFO vInfo;

    T_INFO tInfo1 
= {10"a"};
    T_INFO tInfo2 
= {20"b"};
    T_INFO tInfo3 
= {30"c"};
    T_INFO tInfo4 
= {40"d"};
    T_INFO tInfo5 
= {50"e"};

    
/* c.push_back(elem) appends a copy of elem at the end */
    vInfo.push_back(tInfo1);
    vInfo.push_back(tInfo2);
    vInfo.push_back(tInfo3);  printVecInfo(vInfo);

    
/* c.insert(pos,elem) Inserts at iterator position pos a copy of elem and returns the position of the new element */
    VEC_INFO::iterator pos 
= &vInfo.at(0);
    vInfo.insert(pos, tInfo4);    printVecInfo(vInfo);
    
/* c.insert(pos,n,elem) Inserts at iterator position pos n copies of elem (returns nothing) */
    vInfo.insert(pos, 
2, tInfo5); printVecInfo(vInfo);

    
/* c.erase(pos)  Removes the element at iterator position pos and returns the position of the next element */
    VEC_INFO::iterator pos1 
= getElemByX(vInfo, 10);
    
if (NULL != pos1)
    {
        vInfo.erase(pos1);
    }
    printVecInfo(vInfo);

    VEC_INFO::iterator pos2 
= getElemByS(vInfo, "d");
    
if (NULL != pos2)
    {
        vInfo.erase(pos2);
    }
    printVecInfo(vInfo);

    
/* c.pop_back()  Removes the last element (does not return it) */
    vInfo.pop_back();
    vInfo.pop_back();
    vInfo.pop_back();  printVecInfo(vInfo);

    
/* c.clear()  Removes all elements (makes the container empty) */
    vInfo.clear(); printVecInfo(vInfo);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值