C++STL:vector
#include<iostream>
#include<cstdlib>
using namespace std;
#include<vector> //动态数组
#include<algorithm>
void print_vector(int v)
{
cout << v << "\t" << endl;;
}
//STL 基本语法
void handleSTL()
{
//定义一个容器,并指定容器存放的元素类型int
vector<int> v1;
v1.push_back(23);
v1.push_back(45);
v1.push_back(76);
v1.push_back(87);
//通过STL提供的for_each算法
//容器提供的迭代器
vector<int>::iterator pBegin = v1.begin();
vector<int>::iterator pEnd = v1.end();
// 容器中可能存放基础的数据类型,也可以存放自定义数据类型
for_each(pBegin, pEnd, print_vector);
cout << endl;
}
//容器也可以存放自定义数据类型
class Person {
public:
int age, id;
public:
Person(int age, int id) :age(age), id(id) {};
};
void HandlePerson() {
vector<Person> v2;
Person p1(32, 34), p2(36, 39), p3(82, 64);
v2.push_back(p1);
v2.push_back(p2);
v2.push_back(p3);
//遍历
for (vector<Person>::iterator it = v2.begin(); it != v2.end(); it++) {
cout << (*it).age <<"\t\t"<< (*it).id << endl;
}
}
int main()
{
handleSTL();
HandlePerson();
system("pause");
}
posted on 2019-04-29 15:55 Indian_Mysore 阅读(82) 评论(0) 收藏 举报
浙公网安备 33010602011771号