📄 vector_test.h
字号:
//--------------------------------------------------------------------------------
/*
文件名:Vector_Test.h
目标:模板向量容器及其运算的测试类
*/
#ifndef VECTOR_TEST_H_
#define VECTOR_TEST_H_
//--------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include "Vector_Input.h"
using namespace std;
//--------------------------------------------------------------------------------
// 实际的模板向量测试类
template<class T>
class VectorTest
{
private:
// 空向量
vector<T> V;
// 选择号及插入、删除数据
int Selected;
int pos,pos_last;
T value;
int num;
public:
// 构造器:初始化空向量
VectorTest(Vector_DATA<T> vinfo);
// 选择功能号
int SelectNo();
// 置向量迭代器的指向
vector<T>::iterator SetIterator(int pos);
// 访问向量
void print_V();
// 输入数据并执行选择号的功能
void do_operations(int Selected);
};
//--------------------------------------------------------------------------------
// 实现
template<class T>
VectorTest<T>::VectorTest(Vector_DATA<T> vinfo)
{
for(int i=0; i<vinfo.n; i++)
V.push_back(vinfo.arr[i]);
}
template<class T>
int VectorTest<T>::SelectNo()
{
cout << "********向量容器主菜单***********" << endl;
cout << " 1 插入一个元素" << endl;
cout << " 2 插入多个元素" << endl;
cout << " 3 删除一个元素" << endl;
cout << " 4 删除多个元素" << endl;
cout << " 5 向量信息" << endl;
cout << "********************************" << endl;
cout << " 输入选择号:";
cin >> Selected;
return Selected;
}
template<class T>
void VectorTest<T>::do_operations(int Selected)
{
switch (Selected) {
case 1: // 插入操作
cout << "输入插入位置和插入数据:pos value = ";
cin >> pos >> value;
V.insert(SetIterator(pos),value);
print_V();
break;
case 2: // 多插入操作
cout << "输入插入位置、插入数据和个数:pos value num = ";
cin >> pos >> value >> num;
V.insert(SetIterator(pos),num,value);
print_V();
break;
case 3: // 删除操作
cout << "输入删除位置:pos = ";
cin >> pos;
if(pos<0 || (size_t)pos>=V.size())
cout << "删除位置错!" << endl;
else
V.erase(SetIterator(pos));
print_V();
break;
case 4: // 多删除操作
cout << "输入删除位置1和位置2:pos pos_last = ";
cin >> pos >> pos_last;
V.erase(SetIterator(pos),SetIterator(pos_last));
print_V();
break;
case 5:
print_V();
break;
}
}
template<class T>
vector<T>::iterator VectorTest<T>::SetIterator(int pos)
{
vector<T>::iterator it;
if(pos>(int)V.size()) it=V.end();
else {
it = V.begin();
for(int i=0; i<pos; i++) ++it;
}
return it;
}
template<class T>
void VectorTest<T>::print_V()
{
vector<T>::iterator it;
for(it=V.begin(); it!=V.end(); it++)
cout << *it << " ";
cout << endl;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -