C++11中vector的emplace_back用法及输入输出操作符的重载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jirryzhang/article/details/82959841
#include <vector>
#include <iostream>
#include <string>
using namespace std;

struct A{
    int x;
    double y;
    string z;

    A(int _x=0,double _y=0,string _z=""):x(_x),y(_y),z(_z){}
    A(const A &t):x(t.x),y(t.y),z(t.z){}//copy constructor
    friend ostream & operator<<(ostream &out, A &obj){
        out<<"x:"<<obj.x<<", y:"<<obj.y<<", z="<<obj.z<<endl;
        return out;
    }
    friend istream & operator >> (istream &in, A &obj){
        in>>obj.x>>obj.y>>obj.z;
        if(!in)
            obj=A();
        return in;
    }
};

void test()
{
    vector<A> v;
    A a;
    cin>>a;
    v.emplace_back(0,1.0,"2");

    v.emplace_back(a);
    for(auto i=0;i<v.size();++i){
        cout<<v[i];
    }

}

吐槽CSDN的标题字符限制,<<和>>不能作为标题内容,不然就提示输入有误。气哭。

猜你喜欢

转载自blog.csdn.net/jirryzhang/article/details/82959841
今日推荐