#include <iostream>
#include <vector>
using namespace std;
//创建vector定义的数组
vector<int> chuang(vector<int>& arr)
{
int n;
cout << "输入要几个数:";
cin >> n;
for (int i = 1; i <= n; i++)
{
cout << "请输入第" << i << "个数:";
int val = 0;
cin >> val;
arr.push_back(val);
}
cout << "你输入的这串数是:" ;
for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
{
cout << *it << " ";
}
cout << endl << endl;
return arr;
}
//增加一个数
vector<int> zeng(vector<int>& arr)
{
cout << "你想把数插在谁后面(插在首位就输0):";
int n;
cin >> n;
cout << "你想插入的数是几:";
int m;
cin >> m;
vector<int>::iterator it = arr.begin();
for (int i = 0; i < n; i++)
{
it++;
}
arr.insert(it, m);
cout << "增加后的数组是:";
for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
{
cout << *it << " ";
}
cout << endl << endl;;
return arr;
}
//删除一个数
vector<int> shan(vector<int>& arr)
{
int index;
cout << "请输入要删除第几个数:";
cin >> index;
vector<int>::iterator it = arr.begin();
for (int i = 0; i < index; i++)
{
it++;
}
arr.erase(it);
cout << "删除后的数组是:";
for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
{
cout << *it << " ";
}
cout << endl << endl;;
return arr;
}
//改变一个数
vector<int> gai(vector<int>& arr)
{
int index, val;
cout << "请输入要修改第几个数:";
cin >> index ;
cout << "你想改成几:";
cin >> val;
arr[index-1]= val;
cout << "修改后的数组是:";
for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++)
{
cout << *it << " ";
}
cout << endl << endl;;
return arr;
}
//查找一个数
vector<int> cha(vector<int>& arr)
{
int val;
cout << "请输入要查找的数:";
cin >> val;
for (int i = 0; i < arr.size(); i++)
{
if (arr[i] == val)
{
cout << "要查找的数是数组中的第:" << i+1<<"个数" << endl << endl;;
return arr;
}
}
cout << "未找到要查找的数!" << endl << endl;;
}
int main()
{
vector<int> arr;
int num;
while (true)
{
cout << "选择你要的功能(万物始于1)" << endl;
cout << "1.首先创建一堆数" << endl;
cout << "2.增加某个数" << endl;
cout << "3.删除某个数" << endl;
cout << "4.改变某个数" << endl;
cout << "5.查找某个数" << endl;
cout << "6.退出系统" << endl;
cout << endl;
cin >> num;
switch (num) {
case 1:
chuang(arr);
break;
case 2:
zeng(arr);
break;
case 3:
shan(arr);
break;
case 4:
gai(arr);
break;
case 5:
cha(arr);
break;
case 6:
cout << "成功退出 ╰(*°▽°*)╯" << endl;
return 0;
default:
cout << "不要乱输" << endl << endl;;
break;
}
}
return 0;
}