include <iostream>
include <vector>
include <algorithm>
using namespace std;
int compare(int a, int b)
{
return a > b;
}
int main(int argc, const char * argv[]) {
vector<int> a; //一维数组
int temp[10] = {1,5,20,19,4,3,87,555,12,10};
for (int j = 0; j < 10; j++) {
a.push_back(temp[j]); //数据插入
}
for (int j = 0; j < 10; j++) {
cout<<a[j]<<endl;
}
a.pop_back(); //移除末尾数据
for (int j = 0; j < a.size(); j++) {
cout<<a[j]<<endl;
}
//用迭代器
vector<int>::iterator t;
for (t = a.begin(); t!=a.end(); t++) {
cout<<*t<<endl;
}
//排序
sort(a.begin(), a.end()); //默认从小到大排序
cout<<"sort the vector asec"<<endl;
for (t = a.begin(); t!=a.end(); t++) {
cout<<*t<<endl;
}
cout<<"sort the vector by desc"<<endl;
sort(a.begin(), a.end(), &compare); //写comp方法,实现降序排列
for (t = a.begin(); t!=a.end(); t++) {
cout<<*t<<endl;
}
//二位int数组
vector< vector<int> > b(5,vector<int>(10));
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
b[i][j] = (i*j);
}
}
vector<vector<int>>::iterator t1;
vector<int>::iterator t2;
for(t1 = b.begin();t1 != b.end();t1++){
vector<int> temp = *t1;
for(t2 = temp.begin();t2 != temp.end();t2++){
cout<<*t2<<" "<<endl;
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++) {
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
网友评论