c++ 容器类

#include <iostream>
#include <vector>
#include <list>
#include <map>
using namespace std;

void test_map();

struct Person{
    string name;
    int age;
};

int main()
{
    vector<Person> vs;
    //list<Person> vs;

    Person a, b;
    a.name = "xiaoa";
    a.age = 7;

    b.name = "xiaob";
    b.age = 9;

    vs.push_back(a);
    vs.push_back(b);

    vector<Person>::iterator it;
    for(it=vs.begin(); it!=vs.end(); it++)
    {
        cout << (*it).name << "\t" << (*it).age << endl;
        cout << it->name << "\t" << it->age <<endl;
    }
    cout << "#########################" << endl;
    typedef vector<Person>::iterator PI;
    for(PI i=vs.begin(); i!=vs.end(); i++)
    {
        cout << (*i).name << "\t" << (*i).age << endl;
        cout << i->name << "\t" << i->age <<endl;
    }
    cout << "#########################" << endl;
    test_map();
    return 0;
}


void test_map()
{

    map<string, Person> records;

    Person a, b;
    a.name = "xiaoa";
    a.age = 7;

    b.name = "xiaob";
    b.age = 9;

    records.insert(pair<string, Person>(a.name, a));
    records.insert(pair<string, Person>(b.name, a));

    cout << "#########################" << endl;

    map<string, Person>::iterator rec_iter;
    for(rec_iter=records.begin(); rec_iter!=records.end(); rec_iter++)
    {
        cout << rec_iter->first << "[" << rec_iter->second.name << rec_iter->second.age << "]" << endl;
    }
}

 

posted on 2015-03-24 15:49  雨渐渐  阅读(197)  评论(0编辑  收藏  举报

导航