STL hello word程序(对STL 容器 算法 迭代器 一个初步理解)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23859701/article/details/81489486
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
void show(int t)
{
    cout <<"t的结果是:"<< t << endl;

}
//存放自定义类型
class Person
{
public:
    Person(int age, int id)
    {
        this->age = age;
        this->id = id;

    }
public:
    int age;
    int id;

};
void show1(const Person &p)//Person类没有重载拷贝构造 故使用引用
{

    cout << "Person中的年龄是:"<<p.age << endl;
}

void test(void)
{
    vector<int> t;

    t.push_back(10);
    t.push_back(20);
    t.push_back(30);
    vector<int>::iterator pbegin = t.begin();
    vector<int>::iterator pend = t.end();
    //容器里可能存放基础的数据类型,也可以存放自定义类型
    for_each(pbegin,pend,show);


}
void test2(void)
{

    vector<Person> one;
    Person p1(10,20),p2(30, 40),p3(50, 60);
    one.push_back(p1);
    one.push_back(p2);
    one.push_back(p3);
    vector<Person>::iterator pbegin = one.begin();
    vector<Person>::iterator pend = one.end();
    for_each(pbegin,pend,show1);


}
int main(void)
{

    test();
    test2();
    system("pause");
    return 0;
}

运行结果图

猜你喜欢

转载自blog.csdn.net/qq_23859701/article/details/81489486