// #include "lib/sundry.hpp"
#include <future>
#include <iostream>
#include <vector>
using namespace std;
void test01() {
vector<int> v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
vector<int>::iterator it = v1.begin(); // 保存起始迭代器
for(; it!=v1.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << "test01 end ... " << endl;
}
void test02() {
vector<int> v1;
v1.reserve(500); // 直接预留500的空间.
cout << "容量: " << v1.capacity() << " , 大小: " << v1.size() << endl;
vector<int>::iterator it = v1.begin();
int i = 0;
int count = 0;
for(i=0;i<10000;i++) {
v1.push_back(i);
if (it != v1.begin()) {
count++;
cout << "第" << count << "次开辟空间, 容量: " << v1.capacity()
<< " , 大小: " << v1.size() << endl;
it = v1.begin();
}
}
cout << "test02 end ... " << endl;
}
void printVectorInt(vector<int> &v) {
vector<int>::iterator it = v.begin();
for(;it != v.end();it++) {
cout << *it << " ";
}
cout << endl;
}
void test03() {
vector<int> v1(5, 100); // 初始化5个100
printVectorInt(v1);
vector<int> v2(v1.begin(), v1.end());
printVectorInt(v2);
vector<int> v3;
v3.assign(10, 10);
printVectorInt(v3);
v3.swap(v2); // 交换两个vec
printVectorInt(v3);
printVectorInt(v2);
cout << "test03 end ... " << endl;
}
// 收缩空间
void test04() {
vector<int> v1;
v1.reserve(1000);
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
cout << "容量: " << v1.capacity() << " , 大小: " << v1.size() << endl;
// 通过v1初始化匿名变量,只会初始化实际内容部分,未使用的容量会被丢弃, 然后和v1交换.
// 达到收缩的结果.
vector<int>(v1).swap(v1);
cout << "vec收缩后容量: " << v1.capacity() << " , 大小: " << v1.size() << endl;
cout << "test04 end ... " << endl;
}
// 二维数组遍历
void test05() {
vector<int> v1(5, 10);
vector<int> v2(5, 100);
vector<int> v3(5, 1000);
// 定义一个vector容器,存放v1,v2,v3
vector< vector<int> > v;
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
// 遍历
vector< vector<int> >::iterator it = v.begin();
for(; it != v.end(); it++) {
vector<int>::iterator mit = (*it).begin();
for (; mit!=(*it).end(); mit++) {
cout << *mit << " ";
}
cout << endl;
}
cout << "test05 end ... " << endl;
}
// stl算法对vector容器排序
#include<algorithm>
void test06() {
vector<int> v1;
v1.push_back(10);
v1.push_back(3);
v1.push_back(49);
v1.push_back(2);
printVectorInt(v1);
// 排序算法
sort(v1.begin(), v1.end());
printVectorInt(v1);
cout << "test06 end 排序算法 sort ... " << endl;
}
#include<string>
class Person{
friend void printVectorPerson(vector<Person> &v);
friend bool comparePerson(Person ob1, Person ob2);
private:
int num;
string name;
float score;
public:
Person(){}
Person(int num, string name, float score) {
this->num = num;
this->name = name;
this->score = score;
}
};
// 打印自定义的vec
void printVectorPerson(vector<Person> &v) {
vector<Person>::iterator it = v.begin();
for (; it != v.end(); it++) {
cout << (*it).num << " " << (*it).name << " " << (*it).score << endl;
}
}
// 定义vec排序规则
bool comparePerson(Person ob1, Person ob2) {
return ob1.num < ob2.num;
}
void test07() {
vector<Person> v1;
v1.push_back(Person(100, "luck", 55.4f));
v1.push_back(Person(120, "luckee", 35.4f));
v1.push_back(Person(10, "luckdd", 3.4f));
v1.push_back(Person(20, "luckaa", 90.4f));
printVectorPerson(v1);
// 对自定义类型vec的排序需要指定排序规则.
sort(v1.begin(), v1.end(), comparePerson);
printVectorPerson(v1);
cout << "test07 end ... " << endl;
}
int main() {
test01();
test02();
test03();
test04();
test05();
test06();
test07();
return 0;
}
网友评论