#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 == 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);
}
#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 == 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);
}