Vector存储自定义数据类

#include <iostream>
#include <vector>
#include<algorithm>
#include<string>
using namespace std;

class Person
{
public:
    string m_Name;
    int m_Age;
    Person(string name, int age)
    {
        m_Name = name;
        m_Age = age;
    }
};

void test01()
{
    vector<Person> v;
    vector<Person*> v2;

    Person p1("Tom", 10);
    Person p2("Lisa", 15);
    Person p3("Richard", 12);
    Person p4("Mike", 28);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

    v2.push_back(&p1);
    v2.push_back(&p2);
    v2.push_back(&p3);
    v2.push_back(&p4);

    for (vector<Person>::iterator it = v.begin(); it != v.end(); it

猜你喜欢

转载自blog.csdn.net/qq_42817985/article/details/117305842