练习1:
二维vector操作:
#include<iostream>
#include <vector>
using namespace std;
int main()
{vector<vector<int>> a;vector<int> b;b.push_back(1);b.push_back(2);vector<int> c;c.push_back(11);c.push_back(22);a.push_back(b);a.push_back(c);for (int i = 0; i <size(a); i++){for (int j = 0; j < size(a[i]); j++){cout << a[i][j] << endl;}}system("pause");return 0;
}
运行结果:
求第k个数组中的最小值:
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{vector<vector<int>> a;int count;cout << "请输入数组个数:" << endl;cin >> count;for (int i = 0; i < count; i++){cout << "请输入第" << i+1 << "个数组元素个数:" << endl;int num;cin >> num;cout << "请分别输入元素:" << endl;vector<int> b;b.clear();for (int j = 0; j < num; j++){int element;cin >> element;b.push_back(element);}a.push_back(b);}for (int i = 0; i <size(a); i++){cout << "第" << i+1 << "个数组元素分别为:" << endl;for (int j = 0; j < size(a[i]); j++){cout << a[i][j] << " ";}cout << endl;}int k_temp,k = 0;cout << "请输入你需要查找的那个k值:" ;cin >> k_temp;k = k_temp - 1;if (k > a.size()){cout << "您输入的数超出数组个数范围,error!!!" << endl;return -1;}else if (k > a[k].size()){cout << "您输入的数超出数组元素范围,error!!!" << endl;return -1;}else{sort(a[k].begin(), a[k].end());//下标遍历cout << "下标遍历" << endl;for (int l = 0; l < a[k].size(); l++){cout << a[k][l] << " ";}//迭代器遍历cout << endl<<"迭代器遍历" << endl;for (vector<int>::iterator iter = a[k].begin(); iter != a[k].end(); ++iter){cout << *iter << " ";}cout << endl;cout << "您要找的元素为:";cout << a[k][0] << endl;}cout << endl;system("pause");return 0;
}
运行结果: