⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testvec.cpp

📁 STL 容器 使用例子 MAP VECTOR LIST
💻 CPP
字号:
#include "testVec.h"

void testvec()
{
	cout<<"======================================"<<endl;
	cout<<"testing  vector" <<endl;
	cout<<"======================================"<<endl;

	int one=1;
	int third=3;
	vector<int> myVector;
	vector<int>::iterator vecCurr;
	//////////////////////////////////////////////////////////////////////////
	myVector.push_back(one);
	myVector.push_back(third);
	cout<<"the following is the vector container:"<<endl;
	//1  3
	for (vecCurr=myVector.begin();vecCurr!=myVector.end();vecCurr++) {
		cout<<*vecCurr<<" ";
	}
	cout<<endl;
	//////////////////////////////////////////////////////////////////////////
	int five=5;
	for (vecCurr=myVector.begin();vecCurr!=myVector.end();vecCurr++) {
		if (one==(*vecCurr)) {
			myVector.insert((vecCurr+1),five);	//在指针所在位置插入,原指针的元素及它后面的所有其它元素都往后移
			break;
		}
	}
	cout<<endl<<"after insert that the all are:"<<endl;
	for (vecCurr=myVector.begin();vecCurr!=myVector.end();vecCurr++) {
		cout<<*vecCurr<<" ";
	}
	
	cout<<endl;
	//////////////////////////////////////////////////////////////////////////	
	for (vecCurr=myVector.begin();vecCurr!=myVector.end();vecCurr++) {
		if (one==(*vecCurr)) {
			myVector.erase((vecCurr+1));	//删除当前指针所指向的元素,后面的所有其它元素都往前移
			break;
		}
	}
	cout<<"after eraser then:"<<endl;
	for (vecCurr=myVector.begin();vecCurr!=myVector.end();vecCurr++) {
		cout<<*vecCurr<<" ";
	}
	cout<<endl;
	//////////////////////////////////////////////////////////////////////////
	cout<<"use the function at(1):";
	cout<<myVector.at(1)<<endl;		//即返回数组中第几号位置的值.超出范围是有异常抛出的.所以这个东西后面要捕获异常.[]的话是不会有异常出现的
	//////////////////////////////////////////////////////////////////////////
	cout<<"逆序显示vector的内容:"<<endl;
	vector<int>::reverse_iterator revCurr;
	for (revCurr=myVector.rbegin();revCurr!=myVector.rend();revCurr++) {
		cout<<*revCurr<<" ";
	}
	cout<<endl;
	
	//////////////////////////////////////////////////////////////////////////
	cout<<endl;	//测试结束
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -