vector-back

4人阅读 评论(0) 收藏 举报
分类:
////////////////////////////////////////
//      2018/04/15 18:49:20
//      vector-back
#include <iostream>
#include <vector>
#include <string>
#include <iterator>

using namespace std;

template<class T, class D>
class Member
{
private:
    T name;
    D sal;
public:
    Member(T t, D d) :name(t), sal(d){}
    void print();
};

template<class T, class D>
void Member<T,D>::print(){
    cout << name << " " << sal << endl;
}
// =========================

int main(){
    typedef Member<string, double> M;
    vector<M> v;

    v.push_back(M("Robert", 6000));
    v.push_back(M("Linda", 7500));

    vector<M>::iterator it = v.begin();
    cout << "Enter vector:" << endl;
    while (it != v.end()){
        (it++)->print();
    }
    cout << endl;

    cout << "Return from back()" << endl;
    v.back().print();

    return 0;
}

// OUTPUT:
/*
    Enter vector:
    Robert 6000
    Linda 7500

    Return from back()
    Linda 7500
*/ 
查看评论
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 4万+
    积分: 1878
    排名: 2万+
    xxxxx