加文

希望是美好的……
随笔 - 0, 文章 - 209, 评论 - 0, 引用 - 0
数据加载中……

list push_front

#include <iostream>
#include <list>
#include <iomanip>
#include <string>
using namespace std;

template <class T>
class Name
{
    public:
        Name(T f, T l) : first(f), last(l) {}
        void print()
        {
            cout.setf(ios::left);
            cout << setw(15) << first.c_str()
                 << last << endl;
        }
    private:
        T first, last;
};

//==========================================
int main ()
{
    typedef Name<string> N;
    typedef list<N> L;
    L l;
    L::iterator It;

    N n1(string("Albert"),string("Johnson"));
    N n2("Lana","Vinokur");

    l.push_front(n1);
    l.push_front(n2);

    // unnamed object
    l.push_front(N("Linda","Bain"));

    It = l.begin();
    while ( It != l.end() )
        (It++)->print();
    cout << endl;

    return 0;
}

posted on 2012-04-12 11:44 加文 阅读(370) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理